Need help with input streams from multiple source files

This is a discussion on Need help with input streams from multiple source files within the C++ Programming forums, part of the General Programming Boards category; I have a master file listing 15 subfiles in the same directory. Each of these files has a name in ...

  1. #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();

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,318
    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++;
    }

  3. #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

Popular pages Recent additions subscribe to a feed

Similar Threads

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

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