| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
using eof twice with same file
i was testing something out and used eof twice with the same txt file.
so means i open the txt file, use a loop to read all data inside the txt with eof. then i close the loop and use eof again with the same txt. but this time it will return nothing (i.e if i ask it to output something, nothing comes out). Is the because in the first loop, i already ended reading the file, so the 2nd time it just has nothing to read? |
|
#2
|
|||
|
|||
|
Quote:
yes it is. |
|
#3
|
|||
|
|||
|
oh ok then.
so what should i use to overcome such problem. |
|
#4
|
|||
|
|||
|
Quote:
Reset your reading pointer back to the start of the file. |
|
#5
|
||||
|
||||
|
you could just use fread() and see what value it returns, if it's 0, then that means there was nothing for it to read (meaning you're at an EOF condition)....
__________________
Officially a member of the Itsacon fan club. Beer blasts are every friday at Viper_SB's house. I bring the chips. ![]() |
|
#6
|
|||
|
|||
|
B-Con, that doesn't solve his problem. That approach would also only work once. What you need to do is something like:
Code:
#include <fstream>
using std::ifstream;
int main()
{
ifstream infile("c:\\file.txt");
char c;
while (c = infile.get() != EOF)
{
//process character
}
infile.seekg(0, ios::beg); //reset reading pointer back to beginning of file
while (c = infile.get() != EOF)
{
//process character again
}
infile.close();
}
|
|
#7
|
||||
|
||||
|
Quote:
my bad, I didn't understand the question.... ![]() |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > using eof twice woth same file |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|