# C++ file read endfinte loop

## Recommended Posts

kingpinzs    117
How can I make a file be read from begining to end and back again tell I exit the program? I want to display the text in a file continusley. Like say I load a txt file I read its contents from top to bottom but then I want it to start at the top again ect tell I exit the program.

##### Share on other sites
Hodgman    51223
Which part are you having difficulty with?
bool quit = false;do{ /* print file */ if( /* check input */ )   quit = true;}while( !quit );

##### Share on other sites
kingpinzs    117
this is an inner loop so I have to use the if statment but I kind of figured this should work but its not displaying anything at all

if(!dataFile.eof()){  dataFile.getline(fileInput,TSize);   cout<<fileInput;}  else  {   dataFile.clear();   dataFile.seekg(0L,ios::beg);  }

##### Share on other sites
kingpinzs    117
Never mind it works now it was just the way I was opening the text file.

##### Share on other sites
Captain P    1092
Read the file once, store it's content in memory, and then display that ad infinitum. Accessing memory is more efficient than accessing the hard-drive.

That may not be an issue for a small toy program, but it's something to keep in mind for real-life scenario's. Just saying. ;)