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-
@
string = file.readline();
@
suppose now string contains "framework".if i am comparing by using following code
@
i = string.compare("framework", Qt::CaseSensitive);
@Here i should get zero but i am not getting that output.
-
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? -
[quote author="Andre" date="1323325693"]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?[/quote]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? -
Don't just remove it, figure out where it came from first.
-
[quote author="Andre" date="1323329750"]Don't just remove it, figure out where it came from first. [/quote]
I am actually storing the text file in resource file. Is that is causing problem or any other thing.
@main.cpp
.............
...........
QFile file(":/New.txt");
file.open (QIODevice::ReadOnly);
......................
.............................
res.qrc
................
<file>New.txt</file>
.........................@
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-
@
QFile file("file1.txt");
file.open (QIODevice::ReadOnly);
QByteArray b = file.readAll ();
file.close ();
@
Here i am not able to read the file. -
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.
-
Be careful with
@ 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.
-
You're problem with not being able to access the file with @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.
-
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.
-
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.