Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   Problem with getline C++ (http://www.go4expert.com/forums/getline-cpp-t14600/)

at_pradeep 17Oct2008 19:35

Problem with getline C++
 
I am having following code for reading and writing from an file....
but when i directly read from file... it displays all the data..
but when i do some write and the again read then it does not read any thing..
getline returns without reading anything...
and then file reaches to eof...
Code:

int main()
{
fstream tempFile;
tempFile.open( "abc.txt" );

if( !tempFile.is_open() )
{
printf("ERROR: Enable to Open File ");
exit(0);
}

printf("Writting To file\n");
string buffer = "I am in File";
tempFile.write ( buffer.c_str(), buffer.size( ) );
tempFile.write ( "\n", 1 );

printf("Reading From file\n");
string tempBuf;
while( !tempFile.eof( ) )
{
getline( tempFile, tempBuf, '\n' );
printf("Data %s\n", tempBuf.c_str() );
tempBuf.clear();
}


return 0;
}

whats the problem am i missing any thing...
please HELP! :confused:

sun_kangane 24Nov2008 21:46

Re: Problem with getline C++
 
you have used

tempFile.open( "abc.txt" );

to open file
you have not given read, write i. e. w, r, +w etc. mode of open file...
i think by default it takes "w" mode which delete existing data of file and start writing it from 0th byte...........

try by using "+w" mode..............................


All times are GMT +5.5. The time now is 19:07.