LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-20-2003, 12:14 AM   #1
magicvash
Member
 
Registered: Aug 2003
Location: Chicago
Distribution: Mandrake 9.1 (x86)
Posts: 62

Rep: Reputation: 15
C++ File IO bugs -- can't read file twice?


This is a project I've started working on...this code is just an example but it's essential for my project. The point is to repeatedly read through the same file over and over again, applying small changes to it depending on certain conditions. However, I get errors the 2nd time around. I simplified that part of the code and wrote this program:



// This is file test.cpp
#include <iostream>
#include <fstream>

using namespace std;

int main(int argc, char** argv) {
ifstream ifs;
for(int i = 0; i < 2; i++) {
ifs.open(argv[1],ios::in);
if(!ifs) { cerr << "error" << endl; return 0; }
string line;
while(getline(ifs,line))
cout << line << endl;
ifs.close();
}
return 0;
}


Compiles just fine: g++ -o test test.cpp

when I run ./test test.cpp (i.e. it will display the contents of file test.cpp twice onto cout)
I get this:


$ ./test test.cpp
#include <iostream>
#include <fstream>

using namespace std;

int main(int argc, char** argv) {
ifstream ifs;
for(int i = 0; i < 2; i++) {
ifs.open(argv[1],ios::in);
if(!ifs) { cerr << "error" << endl; return 0; }
string line;
while(getline(ifs,line))
cout << line << endl;
ifs.close();
}
return 0;
}
error
$

So it's reading the file once, and then freaking out the 2nd time...why? This seems logical but I'm missing something obviously....
 
Old 12-20-2003, 12:53 AM   #2
Stack
Member
 
Registered: Oct 2003
Distribution: FreeBSD
Posts: 325

Rep: Reputation: 30
Code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main(int argc, char** argv)
{	
	for(int i = 0; i < 2; i++)
	{
		ifstream ifs;
		ifs.open(argv[1]);
		if(!ifs)
		{
			cerr << "error " << endl;
			return 0;
		}
		string line;
		while(getline(ifs,line))
			cout << line << endl;
		ifs.close();
	}
	return 0;
}
 
Old 12-20-2003, 01:04 AM   #3
magicvash
Member
 
Registered: Aug 2003
Location: Chicago
Distribution: Mandrake 9.1 (x86)
Posts: 62

Original Poster
Rep: Reputation: 15
OK i'm an idiot :-)
Thanks....
 
Old 12-22-2003, 05:49 AM   #4
dakensta
Member
 
Registered: Jun 2003
Location: SEUK
Distribution: Debian & OS X
Posts: 194

Rep: Reputation: 35
Quote:
...why? This seems logical but I'm missing something obviously....
because closing and opening a file does not reset its state which is 'failed' following this line: while(getline(ifs,line))

If you want to re-use the same stream object, call ifs.clear() before attempting any further reads, or create another one, as in Stack's example.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Read parameters from config file (file parser?) alaios Programming 8 07-09-2012 11:29 AM
How to read .chm file in fedora, can't mount ntfs file system ishti_du Linux - Newbie 12 03-06-2007 03:27 AM
How to read variable from one file & update its value in another file minil Programming 1 03-22-2005 12:12 AM
Isolating read access to only to one file only by another file. jon_k Linux - Software 3 08-14-2004 05:16 AM
Change from Read only to Read Write File System? justiceisblind Linux - Newbie 3 03-03-2002 07:23 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:44 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration