User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 429,791 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,806 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 8252 | Replies: 1
Reply
Join Date: Feb 2005
Posts: 2
Reputation: samarth is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
samarth samarth is offline Offline
Newbie Poster

C++: ifstream file pointer not getting reset

  #1  
Feb 23rd, 2005
Hi,

I have the following piece of code. I do the following here

1)open a file.
2) Print the filepointer positon . I get 0
3)Read the file in a while loop till i reach the end
4)close the file
5)Open the same file
6) Print the file pointer position I get -1.( I expect 0 here)

Can anyone let me know the reason for this error and how is it possible to avoid this.

Thanking you in advance
Samarth

#include <fstream>
#include <iostream>

using namespace std;
int OpenFile(fstream  &fileHandle, char * fileName)
{
	cout << "Opening File" << endl;
	int success = true;
	
	// Open the file and read the data
	fileHandle.open(fileName, ios::in|ios::binary);
	
	// Check if the file is present
	if (!fileHandle.is_open())
	{ 
		// File is not present. Log the information
		cout << "OpenFile::Error opening file "<< endl; 
		success = false;
	}
	
	return success;
}

int ReadFile(char *buf, fstream  &file)
{
	while(!file.eof())
	{
		file.read(buf, 1024);
	}
}

int main()
{
	fstream file;
	OpenFile(file, "1.mpg");
	cout << "tellg " << file.tellg()<< endl;
	
	char buf[1024];
	ReadFile(buf,file);
	file.close();
	
	//file = new ifstream();
	OpenFile(file, "1.mpg");
	cout << "tellg " << file.tellg()<< endl;
	
	return 1;
}
Last edited by alc6379 : Feb 23rd, 2005 at 5:16 pm. Reason: added [code] tags
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Sep 2004
Posts: 220
Reputation: frrossk is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 9
frrossk's Avatar
frrossk frrossk is offline Offline
Posting Whiz in Training

Re: C++: ifstream file pointer not getting reset

  #2  
Feb 23rd, 2005
Use code tags
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 4:26 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC