Problem in reading a '\n' character from a file?
-
While i am reading a line from a file with text containing \n Character and storing it in a string of type Qstring. If i am using that Qstring object to set tooltip why i am not getting the tooltip with newline character?
-
I suspect you are using a readline approach, so you are getting the content of the line, not the formatted text of the line (i.e., with the \n). Otherwise please show your code.
-
[quote author="fluca1978" date="1323942903"]I suspect you are using a readline approach, so you are getting the content of the line, not the formatted text of the line (i.e., with the \n). Otherwise please show your code.[/quote]
ya i am using readline approach. So what should i use instead?
-
Place back a newline into the final qstring each time you end a readline.
-
[quote author="fluca1978" date="1323944412"]Place back a newline into the final qstring each time you end a readline.[/quote]
i didn't get it. Can you please specify in more detail?
-
When using readline to read text from a file, the actual end-of-line marker is stripped from the result you get. Either put it back yourself by simply appending a new end-of-line marker to the results of each of your readline calls, or read the file in a different way.
-
[quote author="Andre" date="1323945302"]When using readline to read text from a file, the actual end-of-line marker is stripped from the result you get. Either put it back yourself by simply appending a new end-of-line marker to the results of each of your readline calls, or read the file in a different way. [/quote]
"Read file in different way" means which other function i can use in place of that?
-
Yeah, for instance, read the whole file in one go (if it is not too big).
-
[quote author="Andre" date="1323946497"]Yeah, for instance, read the whole file in one go (if it is not too big). [/quote]
sorry, I can't do that because it is a big file. Is their any other way?
-
You should have something like:
@
QTextStream stream( &myFile );
while( ! stream.atEnd() )
line = line + stream.readLine() + "\n";
@ -
Hi Fluca,
Your code should look something like this:@QString str;
QFile file("file.txt");
if(file.open(QIODevice::ReadOnly | QIODevice::Text)) {
QTextStream in(&file);
do {
// Puts text into your string and adds the newline character
str += in.readLine() + "\n";
} while(!in.atEnd());
}@Regards,
Sebastian -
@
QTextStream stream( &myFile );
while( ! stream.atEnd() )
line = line + stream.readLine() + "\n";
@this code will handle newline character but if in my file i have also line like the following
@Conference scheduled on %s at %s with %s is cancelled@
This line is using formatted string. So, i want something which will read data as string. So that after
reading the data from file i can still do any string operation on it. -
Do you know that QString has an .arg() function for doing operations?
Check out the manual. -
[quote author="Seba84" date="1323946699"]Hi Fluca,
Your code should look something like this:@QString str;
QFile file("file.txt");
if(file.open(QIODevice::ReadOnly | QIODevice::Text)) {
QTextStream in(&file);
do {
// Puts text into your string and adds the newline character
str += in.readLine() + "\n";
} while(!in.atEnd());
}@Regards,
Sebastian[/quote]My code was a quick on-forum example to specify that at each iteration the "\n" can be added.
-
[quote author="pratik041" date="1323948661"]
this code will handle newline character but if in my file i have also line like the following
@Conference scheduled on %s at %s with %s is cancelled@
This line is using formatted string. So, i want something which will read data as string. So that after
reading the data from file i can still do any string operation on it. [/quote]It is not clear to me what you are trying to achieve. If you want to read the string as is (i.e., with %) then it is fine. If you need to read the line with interpolated values then you have to set values in a different way, not reading the line by file.
-
Sorry Fluca, I mistaken the name, I was talking to practik041. The post I did was practically at the same time as yours, I saw yours after posting it.
Practik,
You do not want to understand. Can you check the manual?
You can handle %s with the QString::arg() function, definition:@QString QString::arg(const QString & a, int fieldWidth = 0, const QChar & fillChar = QLatin1Char( ' ' ) ) @
Here it is an example from the QT Help:
@QString i; // current file's number
QString total; // number of files to process
QString fileName; // current file's nameQString status = QString("Processing file %1 of %2: %3").arg(i).arg(total).arg(fileName);@
See the manual for more info.
-
[quote author="Seba84" date="1323954519"]Sorry Fluca, I mistaken the name, I was talking to practik041. The post I did was practically at the same time as yours, I saw yours after posting it.
Practik,
You do not want to understand. Can you check the manual?
You can handle %s with the QString::arg() function, definition:@QString QString::arg(const QString & a, int fieldWidth = 0, const QChar & fillChar = QLatin1Char( ' ' ) ) @
Here it is an example from the QT Help:
@QString i; // current file's number
QString total; // number of files to process
QString fileName; // current file's nameQString status = QString("Processing file %1 of %2: %3").arg(i).arg(total).arg(fileName);@
See the manual for more info.[/quote]
I have seen the manual. It is clear to me.