Sign in to follow this  
kingpinzs

C++ file read endfinte loop

Recommended Posts

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 this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites
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. ;)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this