Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 1

Qt-interest Archive, March 2007
Re: File read problem


Message 1 in thread

I even tried that It doesn't work.I dont know what's the problem.
Duane Hebert wrote:
>> The file* is the basic . It doesn't even work with that.
>>     
>
> How about the fstream sample that I posted?  What is the format of your
> text?  Is it not ascii?
>
> --
> To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with "unsubscribe" in the subject or the body.
> List archive and information: http://lists.trolltech.com/qt-interest/
>
>
>   


-- 
 [ signature omitted ] 

Message 2 in thread

"Srikanth Bemineni" <sbemineni@xxxxxxxxxxxx> wrote in message 
news:45E6180B.5080807@xxxxxxxxxxxxxxx
>I even tried that It doesn't work.I dont know what's the problem.
> Duane Hebert wrote:
>>> The file* is the basic . It doesn't even work with that.
>>>
>>
>> How about the fstream sample that I posted?  What is the format of your
>> text?  Is it not ascii?

Well that's pretty simple code.  What's your debugger tell you?
At each line, you should be able to inspect
what getline() is returning.  Are you sure this is a straight
text file?  Are you trying this by itself or in the context of
some other code that may be trashing memory?

It's hard to offer more assistance without more information. 


--
 [ signature omitted ] 

Message 3 in thread

"Srikanth Bemineni" <sbemineni@xxxxxxxxxxxx> wrote in message 
news:45E6180B.5080807@xxxxxxxxxxxxxxx
>I even tried that It doesn't work.I dont know what's the problem.

OK, I fixed a couple of typoes and this actually compiles and
runs.  Copy your file to c:/test.txt (if your using windows
of change the filename if you're not)

I used it to open a 115000 line text file with
no problem.  If it doesn't display all of your lines,
put a break point on the push_back and find the
last line that it reads and start debugging it.

#include <string>
#include <vector>
#include <fstream>
#include <iostream>
using namespace std;
int main() {
ifstream infile("c:/test.txt");
if(!infile.is_open()) return 1;
string buffer;
vector<string> vec;
while(getline(infile,buffer)) {
vec.push_back(buffer);
}
int x = vec.size();
for(std::vector<string>::iterator it = vec.begin(); it != vec.end(); ++it)
cout << *it << "\n";
system("pause");

return 0;
} 


--
 [ signature omitted ] 

Message 4 in thread

Thank you very much.I found out the problem.The problem is in QString 
the databuffer which i was in using. what is the maximum size a QString 
can hold.

Duane Hebert wrote:
> "Srikanth Bemineni" <sbemineni@xxxxxxxxxxxx> wrote in message 
> news:45E6180B.5080807@xxxxxxxxxxxxxxx
>   
>> I even tried that It doesn't work.I dont know what's the problem.
>>     
>
> OK, I fixed a couple of typoes and this actually compiles and
> runs.  Copy your file to c:/test.txt (if your using windows
> of change the filename if you're not)
>
> I used it to open a 115000 line text file with
> no problem.  If it doesn't display all of your lines,
> put a break point on the push_back and find the
> last line that it reads and start debugging it.
>
> #include <string>
> #include <vector>
> #include <fstream>
> #include <iostream>
> using namespace std;
> int main() {
> ifstream infile("c:/test.txt");
> if(!infile.is_open()) return 1;
> string buffer;
> vector<string> vec;
> while(getline(infile,buffer)) {
> vec.push_back(buffer);
> }
> int x = vec.size();
> for(std::vector<string>::iterator it = vec.begin(); it != vec.end(); ++it)
> cout << *it << "\n";
> system("pause");
>
> return 0;
> } 
>
>
> --
> To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with "unsubscribe" in the subject or the body.
> List archive and information: http://lists.trolltech.com/qt-interest/
>
>
>   


-- 
 [ signature omitted ] 

Message 5 in thread

"Srikanth Bemineni" <sbemineni@xxxxxxxxxxxx> wrote in message 
news:45E63297.1050805@xxxxxxxxxxxxxxx
> Thank you very much.I found out the problem.The problem is in QString
> the databuffer which i was in using. what is the maximum size a QString
> can hold.

Not sure.  I thought you originally said
that you were loading around 1000 lines of text.
Even if the lines are long <g> it doesn't sound like
you'd be exceeding the size of a QString. 


--
 [ signature omitted ] 

Message 6 in thread

After you sending the technique of storing the buffer in a vector i was  
able to get back the whole accumulated buffer from the vector. In the 
process I also accumulated the QString buffer I had, now when i retrieve 
the QString buffer I got less number of values compared to the vector 
buffer.So it is obvious that the QString after some limit avoids to 
append any characters.

See the code below .

ifstream infile(fileName.ascii());
    if(!infile.is_open()) return 1;

     string buffer;
     vector<string> buffervec;

    while(getline(infile,buffer)) {
       buffervec.push_back(buffer);
     }

    for(std::vector<string>::iterator it = buffervec.begin(); it != 
buffervec.end(); ++it)
    {
        qDebug("%s", ((string)(*it)).c_str());  
        dataBuffer.append(*it);
    }
   
    
qDebug("------------------------------------------------------------------");
    qDebug("%s", dataBuffer.ascii());
    
qDebug("------------------------------------------------------------------");
    qDebug("databuffer count = %d", dataBuffer.length());




Duane Hebert wrote:
> "Srikanth Bemineni" <sbemineni@xxxxxxxxxxxx> wrote in message 
> news:45E63297.1050805@xxxxxxxxxxxxxxx
>   
>> Thank you very much.I found out the problem.The problem is in QString
>> the databuffer which i was in using. what is the maximum size a QString
>> can hold.
>>     
>
> Not sure.  I thought you originally said
> that you were loading around 1000 lines of text.
> Even if the lines are long <g> it doesn't sound like
> you'd be exceeding the size of a QString. 
>
>
> --
> To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with "unsubscribe" in the subject or the body.
> List archive and information: http://lists.trolltech.com/qt-interest/
>
>
>   


-- 
 [ signature omitted ] 

Message 7 in thread

On 01.03.07 10:57:03, Srikanth Bemineni wrote:
> After you sending the technique of storing the buffer in a vector i was  able 
> to get back the whole accumulated buffer from the vector. In the process I also 
> accumulated the QString buffer I had, now when i retrieve the QString buffer I 
> got less number of values compared to the vector buffer.So it is obvious that 
> the QString after some limit avoids to append any characters.

No, QString does not do this. The only limiting factor can be your
memory, see qstring.cpp:operator+=. It grows the QString memory and
copies the characters from the appended string. Nothing more.

Also as I said in the other thread I read in a 16MB text file into a
qstring, no problem. How large is your file?

Andreas

-- 
 [ signature omitted ] 

Message 8 in thread

Hi,

> After you sending the technique of storing the buffer in a vector i was  
> able to get back the whole accumulated buffer from the vector. In the 
> process I also accumulated the QString buffer I had, now when i retrieve 
> the QString buffer I got less number of values compared to the vector 
> buffer.So it is obvious that the QString after some limit avoids to 
> append any characters.

OK, then this has nothing to do with reading files, we should be able to 
reproduce without even reading a file.

I'm unable to reproduce anything using the following example, a modified 
version of yours which doesn't read a file:

#include <qstring.h>
#include <vector>
#include <string>
using namespace std;
int main() {
     const char data[] = "abcdefghijklmnopqrstuvwxyz0123456789";
     const int SIZE = 100;
     vector<string> buffervec;
     for (int i = 0; i < SIZE; ++i)
         buffervec.push_back(data);
     QString dataBuffer;
     for(vector<string>::iterator it = buffervec.begin(); it != 
buffervec.end(); ++it)
     {
         qDebug("%s", it->c_str());
         dataBuffer.append(QString(*it));
     }
 
qDebug("------------------------------------------------------------------");
     qDebug("%s", dataBuffer.ascii());
 
qDebug("------------------------------------------------------------------");
     qDebug("databuffer count = %d", dataBuffer.length());
}


Can you try this example? What do you see? What do you expect? If it 
works for you, could you modify it so that it doesn't work?

Also what sort of file are you reading? What is its encoding? How big is it?

--
 [ signature omitted ] 

Message 9 in thread

"Dimitri" <dimitri@xxxxxxxxxxxxx> wrote in message
news:es6va8$i4m$1@xxxxxxxxxxxxxxxxxxxxx
>      for(vector<string>::iterator it = buffervec.begin(); it !=
> buffervec.end(); ++it)
>      {
>          qDebug("%s", it->c_str());
>          dataBuffer.append(QString(*it));
>      }
>

But he's doing:

 qDebug("%s", ((string)(*it)).c_str());
  dataBuffer.append(*it);


i.e. using a c style cast to cast *it to a string.
This is not necessary.
qDebug("%s",*it.c_str());   should work no?
But more probably the problem, is the next
line where he's appending a std::string to a QString.
Does that actually work?

Maybe changing that to:
databuffer.append(*it.c_str());  would be better.
Or will this cause problems with nulls?
Well he said it was a text file but...


--
 [ signature omitted ] 

Message 10 in thread

Hi,

> But he's doing:
> 
>  qDebug("%s", ((string)(*it)).c_str());
>   dataBuffer.append(*it);
> 
> 
> i.e. using a c style cast to cast *it to a string.
> This is not necessary.

Indeed, "*it" is already a string. The cast is a no-op, which is why I 
removed it. That shouldn't be an issue.

> qDebug("%s",*it.c_str());   should work no?
> But more probably the problem, is the next
> line where he's appending a std::string to a QString.
> Does that actually work?
> 
> Maybe changing that to:
> databuffer.append(*it.c_str());  would be better.
> Or will this cause problems with nulls?
> Well he said it was a text file but...

Exactly, this is why I asked for the file or a self-contained example. I 
suspect the file contains '\0' characters or that there are encoding 
issues involved.

--
 [ signature omitted ] 

Message 11 in thread

>After you sending the technique of storing the buffer in a vector i was
able to get back the whole accumulated >buffer from the vector. In the
process I also accumulated the QString buffer I had, now when i retrieve the
>QString buffer I got less number of values compared to the vector buffer.So
it is obvious that the QString after >some limit avoids to append any
characters.

Have you debugged this yet to see where the problem is?
Do you have some nulls in the file that may be causing a problem
when converting from string to qstring?

No one seems to be able to duplicate your problem so you
may need to do some debugging to find the specific error.
Try inspecting each buffer that you're copying.

--
 [ signature omitted ] 

Message 12 in thread

On 28.02.07 19:55:35, Srikanth Bemineni wrote:
> Thank you very much.I found out the problem.The problem is in QString the 
> databuffer which i was in using. what is the maximum size a QString can hold.

There's no limitation documented, so I think the limits are set by your
machine. Now I'm not 100% sure on this, because internally the QString
class uses a d-pointer, which should be allocated on the heap, to store
the text, but it might be worthwhile to create the QString on the heap
and not on the stack...

Anyway, in your original post you talked about a file of about 938
lines. So your lines are something like thousands of characters long?
How large is the whole file? 

I just tried the Qt code you posted earlier on a 16 Meg testfile and it
was read perfectly well with a QString created on the stack. Also I
noticed that the code you posted was for Qt4, not Qt3 as you said you
are using...

Andreas

-- 
 [ signature omitted ] 

Message 13 in thread

Hi,

I was busy with some other so was not able to go through this problem.

This is the code I am using to read and display the file which I have 
already shared with you guys. Lets go through each one of them.Read I am 
using the traditional c++ method to read the file. and it is working 
absolutely fine. Now lets come to the display. Instead of using the the 
normal cout I used the QT qDebug . I really don't know what is the 
internal functionality of the qDebug is, but if the string is to large 
then qDebug is not able to write the string to the out put buffer.I 
tested this with some other string still it is the same case qDebug will 
not display the complete buffer if it large. I feel like qDebug was 
meant to be like that or it was designed keeping a short string in mind.

Somebody please confirm this.

Srikanth Bemineni wrote:
> The file* is the basic . It doesn't even work with that.
> Duane Hebert wrote:
>> FILE* s give me a headache but in either
>> case, what's databuffer and how is it initialized?
>> Which version of Qt are you using?
>>
>>   
>>> QFile read_file(fileName);
>>> if (!read_file.open(QIODevice::ReadOnly))
>>>    return;
>>> QString line
>>> QTextStream in(&fileName);
>>> while (!in.atEnd())
>>> {
>>>     line = in.readLine();
>>>     
>>
>> What if you debug line?
>>
>>   
>>>      databuffer += line;
>>> }
>>>     
>> qDebug("------------------------------------------------------------------")
>> ;
>> qDebug("%s", dataBuffer.ascii());
>> qDebug("------------------------------------------------------------------")
>> ;
>> qDebug("databuffer count = %d", dataBuffer.length());
>> return true;
>>
>> debug's for the referenceonly.In first the count and length are same.
>>
>>
>> --
>> To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with "unsubscribe" in the subject or the body.
>> List archive and information: http://lists.trolltech.com/qt-interest/
>>
>>
>>   
>
>
> -- 
> With Regards
> Srikanth Bemineni
> Geotrace Technologies
> 281-497-8440 extn 228
>                                                                                                                                      
>                                                                                                                    
>                                                                                                                                      
>                                                                                                                                    
>   


-- 
 [ signature omitted ] 

Message 14 in thread

Hi,

> I was busy with some other so was not able to go through this problem.
> 
> This is the code I am using to read and display the file which I have 
> already shared with you guys.

Are you certain you have shared it with us? I can't find any reference 
to this file in your posts.

I'm certain that the problem is that you're attempting to read a file 
containing the character '\0' or something like that. If so it should be 
considered as a binary file, not a text file.

If you do make the file available on some site, we could check it for you.

> [...] 
> not display the complete buffer if it large. I feel like qDebug was 
> meant to be like that or it was designed keeping a short string in mind.
> 
> Somebody please confirm this.

The documentation should be able to confirm that:
	http://doc.trolltech.com/4.2/qtglobal.html#qDebug

--
 [ signature omitted ]