how to read a file continuously using CFile
-
Personally, it'd be like the following:
char str\[3\]; CFile file; .... // other preliminary codes before reading it while( !EOF() ) { file.Read( &str, 3 ); } file.Close();
But IDK how to write the codes for "!EOF()". Or any other ways to read files continuously? Thx in advance! PS: .txt files
-
Personally, it'd be like the following:
char str\[3\]; CFile file; .... // other preliminary codes before reading it while( !EOF() ) { file.Read( &str, 3 ); } file.Close();
But IDK how to write the codes for "!EOF()". Or any other ways to read files continuously? Thx in advance! PS: .txt files
What do you mean exactly by "continuously" ? :confused: Do you mean you want to read the complete file ?
Cédric Moonen Software developer
Charting control [v3.0] OpenGL game tutorial in C++ -
What do you mean exactly by "continuously" ? :confused: Do you mean you want to read the complete file ?
Cédric Moonen Software developer
Charting control [v3.0] OpenGL game tutorial in C++ -
yep! In fact I wanna plot a range of x- and y-coordinates onto a graph while reading them in a txt file. So I used "continuously".
The Read function returns the number of bytes read. When there's nothing more to read (because you reached the end of the file), the function will return 0.
Cédric Moonen Software developer
Charting control [v3.0] OpenGL game tutorial in C++ -
The Read function returns the number of bytes read. When there's nothing more to read (because you reached the end of the file), the function will return 0.
Cédric Moonen Software developer
Charting control [v3.0] OpenGL game tutorial in C++ -
Personally, it'd be like the following:
char str\[3\]; CFile file; .... // other preliminary codes before reading it while( !EOF() ) { file.Read( &str, 3 ); } file.Close();
But IDK how to write the codes for "!EOF()". Or any other ways to read files continuously? Thx in advance! PS: .txt files
-
What's wrong with:
while( file.Read( &str, 3 ) == 3 )
{
// Do stuff with the three characters read
}That will terminate the loop if it can't read 3 bytes from the file. Cheers, Ash
modified on Monday, September 6, 2010 12:17 PM
Aescleal wrote:
What's wrong with: while...
The concept is OK, some details are all wrong. :laugh:
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.
-
Aescleal wrote:
What's wrong with: while...
The concept is OK, some details are all wrong. :laugh:
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.
-
No problem. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.
-
Personally, it'd be like the following:
char str\[3\]; CFile file; .... // other preliminary codes before reading it while( !EOF() ) { file.Read( &str, 3 ); } file.Close();
But IDK how to write the codes for "!EOF()". Or any other ways to read files continuously? Thx in advance! PS: .txt files
Hi Best solution is listen events and read file after the file has changed. If you read file continously, you waste CPU time. If you want to see code sample how to do this, see: https://gurux.svn.sourceforge.net/svnroot/gurux/GXCom/GXMedias/GuruxFile GuruxFile is ATL component that listens selected file or folder and notifies when file changed. Happy coding, Mikko http://www.gurux.org
-
Personally, it'd be like the following:
char str\[3\]; CFile file; .... // other preliminary codes before reading it while( !EOF() ) { file.Read( &str, 3 ); } file.Close();
But IDK how to write the codes for "!EOF()". Or any other ways to read files continuously? Thx in advance! PS: .txt files
-
Personally, it'd be like the following:
char str\[3\]; CFile file; .... // other preliminary codes before reading it while( !EOF() ) { file.Read( &str, 3 ); } file.Close();
But IDK how to write the codes for "!EOF()". Or any other ways to read files continuously? Thx in advance! PS: .txt files
you have got a couple mistakes on the line file.Read( &str, 3 ); First of all str by itself is provides a pointer, so there is no need to put the ampersand in front of it. Secondly, you are reading 3 bytes, but your buffer is only 3 bytes. You will have no room for the terminating zero character, although one may be supplied by the file itself. The code should be: file.Read( str, 2 ); str[ 2 ] = '\0'; Refer to MSDN for further information http://msdn.microsoft.com/en-us/library/ctka0kks%28v=VS.80%29.aspx[^]