shouldn't this be okay to do?
i want to get serpeatet data each time i call this loop open but then 2nd time i open the loop the file gives -1(EOF) to the file buffer making it exit the loop
ifsteam is the class accocated with script
while(fileBuffer != EOF)
{
script.seekg(i, ios::beg);
fileBuffer = script.peek();
if(fileBuffer == dataIwant)
moveData(fileBuffer);
i++;
}
What do i need to do to make the 2nd loop do the same as the first read the whole file picking out the data i want
[Edited by - alex_dergian on December 17, 2005 7:21:33 PM]
I have to say I don't understand your question a bit. From your source (in the future, please put
tags around it, by the way) I can tell you're writing something that's reading from a file in C++, but that's about it. What's the problem? Do you know what's causing it right now?
Can you post both loops ?.
Also, did you clear fileBuffer to 0 before entering the 2nd loop ?.
Also, did you clear fileBuffer to 0 before entering the 2nd loop ?.
You only gave them 18 minutes, 37 seconds to answer. The people here have other lives. Please allow adequate time for potential posters to post before bumping. I believe most of the forum FAQs say to wait at least 24 hours before bumping.
Now to your question. You probably haven't closed the file before opening it back up so as far as your program is concerned, you're still at the end of the file, hence the EOF you are getting the second time you call the loop. Hope this helps.
-AJ
[EDIT]
Well, I guess it's a moot point now. *shrug*
Now to your question. You probably haven't closed the file before opening it back up so as far as your program is concerned, you're still at the end of the file, hence the EOF you are getting the second time you call the loop. Hope this helps.
-AJ
[EDIT]
Quote:
Original post by u235
You only gave them 18 minutes, 37 seconds to answer. The people here have other lives. Please allow adequate time for potential posters to post before bumping. I believe most of the forum FAQs say to wait at least 24 hours before bumping.
Well, I guess it's a moot point now. *shrug*
the problem is if i call the loop a 2nd time it starts at the ending rather then the begging of the file here is my code
c++
c++
//open accses to the file stream ifstream script; script.open(fileName, ifstream::in); //don't do anything if the file was not found if(!script.fail()) { //check the number of elements in file int fileBuffer = 0; int i = 0; m_totalElements = 0; while(fileBuffer != EOF) { script.seekg(i, ios::beg); fileBuffer = script.peek(); if(fileBuffer == elementId) m_totalElements++; i++; } //Read the posstion of the elements int *elemPosses; elemPosses = new int[m_totalElements]; int currentElem = 0; i = 1; fileBuffer = 0; while(fileBuffer != EOF) { script.seekg(i, ios::beg); fileBuffer = script.peek(); if(fileBuffer == elementId) { elemPosses[currentElem] = i; cout << "elem posstion" << i << "\n"; currentElem++; } i++; } }
Quote:Original post by u235
Now to your question. You probably haven't closed the file before opening it back up so as far as your program is concerned, you're still at the end of the file, hence the EOF you are getting the second time you call the loop. Hope this helps.
Note that you can also call the clear member function to reset the flags to a good state.
Quote:Original post by Skeleton_V@T
Can you post both loops ?.
Also, did you clear fileBuffer to 0 before entering the 2nd loop ?.
I meant the file pointer should be cleared back to the beginning of your file.
I don't know which file I/O functions you used, but standard C library functions all provide methods to position the file pointer:
Win32: CreateFile (), OpenFile (), CloseHandle (), SetFilePointer () etc ...
Obsolete DOS mode: _fopen (), _fclose (), fseek ()
Edit: Overlooked your code, try ifstream::seekg (0, ios_base::beg)
[Edited by - Skeleton_V@T on December 17, 2005 8:01:17 PM]
If I were you, I'd change your peeking and seeking and just use read() and when you reach the end of file, clear the bits and seekg() back to the beginning.
//open access to the file stream ifstream script; script.open(fileName, ifstream::in); //don't do anything if the file was not found if(script.good()) // good will check fail/badbit/eof, so it is more conclusive { //check the number of elements in file char fileBuffer; int i = 0; m_totalElements = 0; while(script.good()) { script.get(fileBuffer); if(fileBuffer == elementId) m_totalElements++; } script.clear() script.seekg(0, ios::beg); //Read the posstion of the elements int *elemPosses; elemPosses = new int[m_totalElements]; int currentElem = 0; i = 0; // you mean 0 or 1? Probably didn't matter before. fileBuffer = 0; while(script.good()) { script.get(fileBuffer); if(fileBuffer == elementId) { elemPosses[currentElem] = i; cout << "elem position" << i << "\n"; currentElem++; } i++; }}
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement