| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Closing and Reopening Files
Hey guys, need some help. My program reads from a text file by a for loop,
and then once it reaches the end of the file, I need it to start back at the beginning of the file. What I did was simple, I just did FileIn.close(); and then FileIn.open(filename);, but it doesnt work for some reason. I have some error-trapping directly after the filein.open in the case that it fails (if(!FileIn)), and everytime it goes right into that if statement. Is there some other way to read from the beginning of your file again after it has reached the end of the file? |
|
#2
|
|||
|
|||
|
I don't see why it would fail in this way. Could you post the section of code up so we can have a look and see if there's something else causing the problem.
-KM- |
|
#3
|
|||
|
|||
|
The first FileIn.open works fine, and it goes through the following do loop perfectly,
up until it gets to the part where its supposed to repeat the song (hence the closing and reopening of the file). At that point it goes right into the if(!FileIn) error-trap, and I have no idea why. I already tried doing FileIn.seekg(0), but that didn't work either for some reason. FileIn.open(filename); if (!FileIn) { cerr << "Cannot open file.\n"; exit(1); } FileIn >> tempo; FileIn.get(); done = false; do { if (!done) { Flag = false; FileIn.getline(tnote, 4); for(y = 0; y <= 3; y++) { if (tnote[y] == '\n' || tnote[y] == '\0') Flag = true; if (Flag) tnote[y] = ' '; if (y == 3) tnote[y] = '\0'; } if(tnote[0] != ' ') { if (tnote[0] == 'T') FileIn >> tempo; else FileIn >> tdur; FileIn.get(); } freq = Frequency(tnote); if (tnote[0] == 'R' && RepeatOn) repeat = true; if (tnote[0] != 'R' && tnote[0] != 'X' && tnote[0] != ' ' && tnote[0] != 'T') { cout << tnote << ' ' << tdur << endl; if (tnote[0] == 'P') sleep((clock_t)(tdur*(tempo / 1000.))); else bBeep = Beep(freq, (tdur*(tempo / 1000.))); } if (tnote[0] == 'X') if (repeat) { FileIn.close(); FileIn.open(filename); if (!FileIn) { cerr << "Cannot open file.\n"; exit(1); } FileIn >> tempo; FileIn.get(); do { FileIn.getline(tnote, 4); if (tnote[0] != ' ') { FileIn >> tdur; FileIn.get(); } if (tnote[0] == 'R') break; }while(true); } else done = true; } } while (true); |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > Closing and Reopening Files |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|