shouldn't this be okay to do?

Started by
7 comments, last by nprz 18 years, 4 months ago
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]
Advertisement
no buddy know?
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?
It only takes one mistake to wake up dead the next morning.
Can you post both loops ?.
Also, did you clear fileBuffer to 0 before entering the 2nd loop ?.
--> The great thing about Object Oriented code is that it can make small, simple problems look like large, complex ones <--
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]
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*
V/R,-AJThere are 10 kinds of people in the world: Those who understand binary and those who don't...
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++
//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.

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

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]
--> The great thing about Object Oriented code is that it can make small, simple problems look like large, complex ones <--
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