C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 10-07-2005, 07:38 PM   #1
Registered User
 
Join Date: Oct 2005
Posts: 19
Need help with input streams from multiple source files

I have a master file listing 15 subfiles in the same directory. Each of these files has a name in the top line, and 2 digit numbers in each subsequent line (about 5 lines). What I need to do is open each of those file names by calling it from the master file, and print the name (first line) to the screen in descending order, and calculate the average of the numbers.

What I'm having trouble with is i get what looks like an infinite loop. I'm able to output the name and average of each file just fine, but after that, nothing happens, it seems to be trying to get input that isnt there. Im using Gnu on a unix system

Any help would be appreciated, thanks!

Code:
        infile1.open("master_file")
	while(!infile1.eof())
		{
		infile1.getline(string, 80, '\n');
		cout<<string<<endl;


		infile2.open(string); 
		for(line=0;line<1;line++)
			{
			infile2.getline(name, 80, '\n');	//get name
			cout<<name<<endl;

			infile2>>score;
/*-----------------while(!infile2.eof())			//get score
				{
				total+=score;
				num++;
				infile2>>score;
				} ------------------------Gets stuck in this loop*/
			cout<<total/num<<endl;
			}
		infile2.close();
		infile2.clear();
                total=num=0;	
		}
     infile1.close();
     infile2.clear();
orikon is offline   Reply With Quote
Old 10-08-2005, 11:42 AM   #2
Registered User
 
Join Date: Jan 2005
Posts: 7,252
I'm not sure if this is your problem, but if the infile2 file stream goes into a fail state, that loop will go forever because you are only checking for eof(). A better way to do that loop is to put the read into the control of the while loop. Instead of this:
Code:
infile2>>score;
while(!infile2.eof())			//get score
{
	total+=score;
	num++;
	infile2>>score;
}
Use this:
Code:
while(infile2>>score)			//get score
{
	total+=score;
	num++;
}
Daved is offline   Reply With Quote
Old 10-08-2005, 02:56 PM   #3
Registered User
 
Join Date: Oct 2005
Posts: 19
Thank you so much, it's working now
You saved me a lot of time, really appreciate it
orikon is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Splitting source into multiple files(Linux & make) IceDane C Programming 6 05-18-2009 07:31 AM
Mutex across multiple source files Quasar Linux Programming 7 12-04-2007 08:25 AM
pseudocode for multiple source files Calef13 C++ Programming 4 11-13-2007 09:07 AM
WM_COPYDATA and mutex selecting multiple files gh0st Windows Programming 2 10-27-2006 02:22 PM
Beginner Question: Multiple source files ironfistchamp C++ Programming 8 07-16-2006 02:19 PM


All times are GMT -6. The time now is 03:17 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22