2 Replies - 395 Views - Last Post: 18 May 2009 - 05:46 AM Rate Topic: -----

Topic Sponsor:

#1 sule  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 07-May 09

file io

Posted 17 May 2009 - 09:02 PM

Hi,

I am tryin to open multiple files at the same time and it wont work. The first file will open but not the second...Thanks for ur help!
see code:

#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <fstream> 
using namespace std; 
#include "filesource.cpp"


void main()
{
	ifstream infile;
	infile.open("C:\\File IO Lab 2 Files\\ints2.txt");  
	if(infile.fail( ))				
	{
		cout << "The File was not successfully open." << endl;
		exit(1); // ends ENTIRE program!!!
	}
//second file
		infile.open("C:\\File IO Lab 2 Files\\ints3.txt"); 
	if(infile.fail( ))				
	{
		cout << "The File was not successfully open." << endl;
		exit(1); // ends ENTIRE program!!!
	}
}


This post has been edited by sule: 17 May 2009 - 09:03 PM


Is This A Good Question/Topic? 0
  • +

Replies To: file io

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 3238
  • View blog
  • Posts: 10,644
  • Joined: 18-April 07

Re: file io

Posted 17 May 2009 - 09:05 PM

You can't use the same file variable to open both. Create multiple file variables. Instead of using "infile" for both, create infile and infile2. Use infile to open the first and infile2 to open the second.

By using the same variable to open both, the second open will close the other.

Hope that makes sense. :)
Was This Post Helpful? 0
  • +
  • -

#3 sule  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 07-May 09

Re: file io

Posted 18 May 2009 - 05:46 AM

Thanks! that worked...have another question

I am suppose to have read data from two files and then output the numbers into another file by smallest to largest number. I cant get the while loop to run for both the files...it only runs for one...i dont get any errors...see code:

#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <fstream> 
using namespace std; 
#include "filesource.cpp"


void main()
{
	ofstream outfile;
	outfile.open("C:\\File IO Lab 2 Files\\results.txt");
	if(!outfile.is_open())
	{
		cout << "NOt working" << endl;
		exit(1);
	}

	ifstream infile1;
	infile1.open("C:\\File IO Lab 2 Files\\ints2.txt");  
	if(infile1.fail( ))				
	{
		cout << "The File was not successfully open." << endl;
		exit(1); // ends ENTIRE program!!!
	}

	ifstream infile2;
	
	infile2.open("C:\\File IO Lab 2 Files\\ints3.txt"); 
	if(infile2.fail( ))				
	{
		cout << "The File was not successfully opezn." << endl;
		exit(1); // ends ENTIRE program!!!
	}

	int next;
	int maximum_number = 0;

	while (infile1 >> next && infile2 >> next)
	 {
		   if(next > maximum_number)
		   maximum_number = next;
		   outfile << maximum_number << endl;
			 
	 }
	outfile << endl;

	infile1.close();
	infile2.close();
	outfile.close();
	
}



Was This Post Helpful? 0
  • +
  • -

Page 1 of 1