Home > Back-end >  Wifstrem problems?
Wifstrem problems?

Time:05-20

WCHAR ch1 [256].
WCHAR ch2 [256].
WCHAR ch3 [256].

Wifstream CI_File;
CI_File. Open (" 1. TXT ", the ios: : app);
CI_File. Imbue (locale (" CHS "));
Int I=5;
while (! CI_File. Eof ())
{
CI_File & gt;> Ch1;
CI_File & gt;> Ch2;
CI_File & gt;> Ch3;

TextOut (HDC, 5, 5, ch1, lstrlen (ch1));
TextOut (HDC, 5, 25, ch2, lstrlen (ch2));
TextOut (HDC, 5, 45, ch3, lstrlen (ch3));


/* TextOut (HDC, 5, and I, ch1, lstrlen (ch1));
I +=20; */
}

The code above is a read 1. TXT file content

The contents of the file:
Hello Sir: hello, ms hello children
China's good

If use the above code to read and print out the result is:

China's good woman: hello, child hello

Mr The ch1 first read "hello", and the second read "China's good", this should be a "hi" still exists in the cache before, reading "China's good", the "Mr You" three words before covers, resulting in a "good" still exists, so a "China take" situation,

The right should be: China's good woman: hello, child hello



But if I change the code to the following with a variable to the operation, it is no problem, I do not know what reason be?

while (! CI_File. Eof ())
{
CI_File & gt;> Ch1;

TextOut (HDC, five, I, ch1, lstrlen (ch1));

I +=20;
}

Please give directions!

Under the console project with char ch1 [256].
Char ch2 [256].
Char ch3 [256].
Also there is no problem, don't know what's going on,

CodePudding user response:

For reference only, do not use
While (conditions) 
Don't use more
While (portfolio)
To use the
While (1) {
Condition of an if (1) break;
//...
If conditions (2) continue;
//...
If conditions (3) return;
//...
}
Because the first two kinds of writing on the level of language to express meaning is ambiguous, is only the third faithfully reflect the actual situation of program flow,
Typical such as:
The following two paragraphs semantics are as at the end of the file not read characters
while (! The feof (f)) {
A=fgetc (f);
//...
B=fgetc (f);//may already feof!
//...
}
And write no problem like this:
While (1) {
A=fgetc (f);
If (feof (f)) break;
//...
B=fgetc (f);
If (feof (f)) break;
//...
}
Similar examples can also take a lot of,
  • Related