December 8, 2011

pratik041 pratik041
Lab Rat
346 posts

If i am reading a line from a file and comparing it with the same text why i am not getting exact output?

 

Suppose i have read a line from file. ex-

  1.     string = file.readline();

suppose now string contains “framework”.

if i am comparing by using following code

  1. i = string.compare("framework", Qt::CaseSensitive);

Here i should get zero but i am not getting that output.

 Signature 

Pratik Agrawal

9 replies

December 8, 2011

Andre Andre
Area 51 Engineer
6076 posts

How sure are you that the string actually is “framework”? Note that contains means that it could also read “Qt is an excellent framework” or something like that.
Did you try to inspect the actual contents of string by making it debug output by setting a breakpoint?

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

December 8, 2011

pratik041 pratik041
Lab Rat
346 posts
Andre wrote:
How sure are you that the string actually is “framework”? Note that contains means that it could also read “Qt is an excellent framework” or something like that. Did you try to inspect the actual contents of string by making it debug output by setting a breakpoint?

Ya i have inspect it by taking the same string in file. I am getting the same string but followed by a character with some extra character with ascii value 127(DEL) or 255 i am not sure and then newline character.
So how can i remove that unwanted character?

 Signature 

Pratik Agrawal

December 8, 2011

Andre Andre
Area 51 Engineer
6076 posts

Don’t just remove it, figure out where it came from first.

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

December 8, 2011

pratik041 pratik041
Lab Rat
346 posts
Andre wrote:
Don’t just remove it, figure out where it came from first.

I am actually storing the text file in resource file. Is that is causing problem or any other thing.

  1. main.cpp
  2.     .............
  3.    ...........
  4.    QFile file(":/New.txt");
  5.    file.open (QIODevice::ReadOnly);
  6.    ......................
  7.    .............................
  8. res.qrc
  9.    ................
  10.    <file>New.txt</file>
  11.    .........................

I am not understanding why every line i am accessing is followed by the same unwanted character.

Another problem is there while i am trying to access the file as a general text file i am not able to access that file. ex-

  1.    QFile file("file1.txt");
  2.    file.open (QIODevice::ReadOnly);
  3.    QByteArray b = file.readAll ();
  4.    file.close ();

Here i am not able to read the file.

 Signature 

Pratik Agrawal

December 8, 2011

favoritas37 favoritas37
Ant Farmer
147 posts

By saying that you are not able to read the file, you mean you are getting an error? The QByteArray is null?

Also your problem with the extra character is most probably because of the file itself, not because of your application. Those extra characters are sometimes shown when reading a text file created in Windows and read in Linux.

So you could try to open it with a Notepad++ to see what goes wrong, or rewrite it and you will be fine.

December 8, 2011

sierdzio sierdzio
Area 51 Engineer
2544 posts

Be careful with

  1.  file.open (QIODevice::ReadOnly);
too. QFile opens files in “binary” mode by default. If you want text, add QIODevice::Text flag – this way, all line terminators will be converted from Windows-style to Unitx-style. That is, if you happen to have a Windows-terminated file, which happens to some people, as I am being told ;)

I recommend reading QFile documentation. And, as people above suggested, look into the file itself, your problem seems to be connected with that (maybe some strange encoding?).

EDIT: Fixed the code not being wrapped in ‘@’ properly.

 Signature 

(Z(:^

December 8, 2011

KA51O KA51O
Robot Herder
403 posts

You’re problem with not being able to access the file with

  1. file.readAll();
might also be related to the encoding. The content of the QByteArray might start with the Null termination character sequence (I’m not sure if that is the correct name for it?) and thus if you convert the QByteArray to a QString this string will be empty.

December 8, 2011

Andre Andre
Area 51 Engineer
6076 posts

Note that we’ve already been over the readAll() thing in another topic, and you were advised there not to use it given the fact that your file is quite large and in memory already.

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

December 8, 2011

pratik041 pratik041
Lab Rat
346 posts

Both the problem is now solved. Previously i was not able to read from file because the file path was under different user and the second problem of extra character was due to the missing of text flag as suggested by “sierdzio”.
Thanks all.

 Signature 

Pratik Agrawal

 
  ‹‹ Question: overlay QWidget on third party X11 graphics      One database table source for multiple models and views ››

You must log in to post a reply. Not a member yet? Register here!