fstream::eof() - returns true if the 'eofbit' has been set by a previous i/o operation. fstream::read(s, n) - sets 'eofbit' if the End-of-File is reached before n characters have been read. The file has only 5 characters and the while loop will execute twice. In the first pass you try to read 5 bytes and it will be success. In the second pass read will fail and sets the 'eofbit'. Since you are not checking how many bytes read, you cout the old buffer contents again. This is the reason why you got "abcfabcf". The code can be changed to print "abcf" once, by checking the bytes read using fstream::gcount() as ................. if(file.is_open()) { //file.write(data,sizeof(data)); while(!file.eof()) { file.read(data,sizeof(data)); if(file.gcount()) cout<<data; } file.close(); } ..................
R
Raj Indian
@Raj Indian
Posts
-
Troublesome Handling of Files in C++ -
How to get Dialog handleCould you please post you code snippet?
-
CListCtrl text underline?Try CListCtrl::SetExtendedStyle() refer: http://msdn.microsoft.com/en-us/library/bb774732(VS.85).aspx
-
char str[] helpif b is a pointer: char *b = str; or if b is an array and you need to copy the whole null terminated string to it: #define SIZE 200 // or any suitable value char b[SIZE]; strcpy( b, str ); One suggestion: please use meaningful names as variable names instead of b
-
Perform the Restart device ,after installation of cabUsing SetSystemPowerState() API a Windows CE device can be restarted.
-
Windows CE 6.0 n .net 2005(C#)- pendrive connect eventTry handling WM_DEVICECHANGE message, if your device is a host.