10 Replies - 237 Views - Last Post: 16 September 2012 - 09:35 PM Rate Topic: -----

#1 zachkt  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 19-July 12

Merging the numbers in 2 files, then writing numbers to third file

Posted 16 September 2012 - 04:13 PM

Here is what I got, but the program just goes to the end and only displays "End Program". Why wont it show the numbers in both the files? I copied them to the project folder so it should work.


#include <fstream>
#include <iostream>
#include <cstdlib>
using namespace std;

int main ()
{
	ifstream in_stream1,in_stream2;
	ofstream out_stream1,out_stream2;
	int numbers1,numbers2,numbers3,last;

	in_stream1.open("hw3-1.txt");
	if (in_stream1.fail()) 
	{
		cout << "Program was unable to open file hw3-1\n"; 
		cout << "The Program will now exit\n"; 
		cout << endl; 
		system("pause"); 
		exit(1); 
	}

	while(in_stream1 >> numbers1)
	{
		in_stream1 >> numbers1;
	}

	in_stream1.close();

	in_stream1.open("hw3-2.txt");
	if (in_stream1.fail()) 
	{
		cout << "Program was unable to open file hw3-2\n"; 
		cout << "The Program will now exit\n"; 
		cout << endl; 
		system("pause"); 
		exit(1); 
	}

	while(in_stream2 >> numbers2)
	{
		in_stream2 >> numbers2;
	}
	out_stream2.open("hw3-1-2.DAT"); 
	if(out_stream2.fail())
	{
		cout << "Program was unable to create file hw3-1-2.dat\n"; 
		cout << "program will now exit\n"; 
		cout << endl;
		system("pause"); 
		exit(1); 
	}

	while(in_stream2 >> numbers3)
	{  
		in_stream2 >> numbers3; 
		
		if (numbers3 > last)  
		{  
			out_stream2 << numbers3;  
			out_stream2 << endl;  
		}  

	}

	in_stream2.close();  
	out_stream2.close();  



	cout << "<Program End>";  

	cout << endl << endl << endl;  

	system("pause");  
	return 0;  
} 



Attached File(s)

  • Attached File  hw3-1.txt (120bytes)
    Number of downloads: 14
  • Attached File  hw3-2.txt (60bytes)
    Number of downloads: 13


Is This A Good Question/Topic? 0
  • +

Replies To: Merging the numbers in 2 files, then writing numbers to third file

#2 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 980
  • View blog
  • Posts: 3,397
  • Joined: 19-February 09

Re: Merging the numbers in 2 files, then writing numbers to third file

Posted 16 September 2012 - 05:26 PM

View Postzachkt, on 17 September 2012 - 02:13 AM, said:

Here is what I got, but the program just goes to the end and only displays "End Program". Why wont it show the numbers in both the files?


When you say show the numbers in both files, do you mean on the screen or in the file?


What are you doing here?

22	    while(in_stream1 >> numbers1)
23	    {
24	        in_stream1 >> numbers1;
25	    }
26	 
27	    in_stream1.close();




Here you read before the file is opened.

39	    while(in_stream2 >> numbers2)
40	    {
41	        in_stream2 >> numbers2;
42	    }


Was This Post Helpful? 0
  • +
  • -

#3 zachkt  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 19-July 12

Re: Merging the numbers in 2 files, then writing numbers to third file

Posted 16 September 2012 - 05:47 PM

View Post#define, on 16 September 2012 - 05:26 PM, said:

View Postzachkt, on 17 September 2012 - 02:13 AM, said:

Here is what I got, but the program just goes to the end and only displays "End Program". Why wont it show the numbers in both the files?


When you say show the numbers in both files, do you mean on the screen or in the file?


What are you doing here?

22	    while(in_stream1 >> numbers1)
23	    {
24	        in_stream1 >> numbers1;
25	    }
26	 
27	    in_stream1.close();




Here you read before the file is opened.

39	    while(in_stream2 >> numbers2)
40	    {
41	        in_stream2 >> numbers2;
42	    }


On screen is what I meant
Was This Post Helpful? 0
  • +
  • -

#4 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 980
  • View blog
  • Posts: 3,397
  • Joined: 19-February 09

Re: Merging the numbers in 2 files, then writing numbers to third file

Posted 16 September 2012 - 06:44 PM

You want to use cout to print the numbers.

22	    while(in_stream1 >> numbers1)
23	    {
24	        cout << numbers1;
25	    }
26	 


Was This Post Helpful? 0
  • +
  • -

#5 zachkt  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 19-July 12

Re: Merging the numbers in 2 files, then writing numbers to third file

Posted 16 September 2012 - 06:57 PM

#include <fstream>
#include <iostream>
#include <cstdlib>
using namespace std;

int main ()
{
	ifstream in_stream1,in_stream2;
	ofstream out_stream1,out_stream2;
	int numbers1,numbers2,numbers3,last;

	in_stream1.open("hw3-1.txt");
	if (in_stream1.fail()) 
	{
		cout << "Program was unable to open file hw3-1\n"; 
		cout << "The Program will now exit\n"; 
		cout << endl; 
		system("pause"); 
		exit(1); 
	}

	while(in_stream1 >> numbers1)
	{
		in_stream1 >> numbers1;
	}

	in_stream1.close();

	in_stream2.open("hw3-2.txt");
	if (in_stream2.fail()) 
	{
		cout << "Program was unable to open file hw3-2\n"; 
		cout << "The Program will now exit\n"; 
		cout << endl; 
		system("pause"); 
		exit(1); 
	}

	while(in_stream2 >> numbers2)
	{
		in_stream2 >> numbers2;
	}
	
	in_stream2.close();

	out_stream2.open("hw3-1-2.txt"); 
	if(out_stream2.fail())
	{
		cout << "Program was unable to create file hw3-1-2.dat\n"; 
		cout << "program will now exit\n"; 
		cout << endl;
		system("pause"); 
		exit(1); 
	}

	while(in_stream2 >> numbers3)
	{  
		in_stream2 >> numbers3; 
		
		if (numbers3 > last)  
		{  
			out_stream2 << numbers3;  
			out_stream2 << endl;  
		}  

	}

	in_stream2.close();  
	out_stream2.close();  



	cout << "<Program End>";  

	cout << endl << endl << endl;  

	system("pause");  
	return 0;  
} 



I opened the second file. And actually I do not need to display it on screen I just changed the hw3-1-2 to a txt file but none of the number from the original 2 files are in the new one (hw3-1-2). What is wrong?
Was This Post Helpful? 0
  • +
  • -

#6 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 980
  • View blog
  • Posts: 3,397
  • Joined: 19-February 09

Re: Merging the numbers in 2 files, then writing numbers to third file

Posted 16 September 2012 - 07:45 PM

Here is your code with some comments.

#include <fstream>
#include <iostream>
#include <cstdlib>

using namespace std;

int main ()
{
  // declare variables
  ifstream in_stream1,in_stream2;
  ofstream out_stream1,out_stream2;
  int numbers1,numbers2,numbers3,last;

  // open first file
  in_stream1.open("hw3-1.txt");
  if (in_stream1.fail()) 
  {
    cout << "Program was unable to open file hw3-1\n"; 
    cout << "The Program will now exit\n"; 
    cout << endl; 
    system("pause"); 
    exit(1); 
  }

  // read the the data from first file
  // repeatedly overwrite an integer variable 
  while(in_stream1 >> numbers1)
  {
    in_stream1 >> numbers1;
  }

  // close the first input file
  in_stream1.close();



  // open second file
  in_stream2.open("hw3-2.txt");
  if (in_stream2.fail()) 
  {
    cout << "Program was unable to open file hw3-2\n"; 
    cout << "The Program will now exit\n"; 
    cout << endl; 
    system("pause"); 
    exit(1); 
  }

  // read the the data from second file
  // repeatedly overwrite an integer variable 
  while(in_stream2 >> numbers2)
  {
    in_stream2 >> numbers2;
  }
	

  // close the second input file
  in_stream2.close();


  // open third file - output
  out_stream2.open("hw3-1-2.txt"); 
  if(out_stream2.fail())
  {
    cout << "Program was unable to create file hw3-1-2.dat\n"; 
    cout << "program will now exit\n"; 
    cout << endl;
    system("pause"); 
    exit(1); 
  }

  // try and read from closed second input file
  while(in_stream2 >> numbers3)
  {  
    // try and read again from closed second input file
    // repeat instruction which is in while condition
    in_stream2 >> numbers3; 
		
    // try and use last which is uninitialized
    if (numbers3 > last)  
    {  
      out_stream2 << numbers3;  
      out_stream2 << endl;  
    }  

  }

  // try and close already closed second input file 
  in_stream2.close();  
  out_stream2.close();  


  cout << "<Program End>";  
  cout << endl << endl << endl;  

  system("pause");  
  return 0;  
} 





I think you only need one loop, because the merging (reading and outputting) is happening at one time. As a start try reading the numbers from both input streams and output both numbers. Later you can think about the order.
Was This Post Helpful? 0
  • +
  • -

#7 zachkt  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 19-July 12

Re: Merging the numbers in 2 files, then writing numbers to third file

Posted 16 September 2012 - 08:31 PM

well the issue now is that I tested it with cout <<numbers1, and then I tried 2 and 3 but it was not listing the numbers in the respected file, it just gave a weird negative number. Any ideas as to how I can get it to list the numbers in the third file?

This post has been edited by zachkt: 16 September 2012 - 08:32 PM

Was This Post Helpful? 0
  • +
  • -

#8 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 980
  • View blog
  • Posts: 3,397
  • Joined: 19-February 09

Re: Merging the numbers in 2 files, then writing numbers to third file

Posted 16 September 2012 - 08:39 PM

What does the code look like now? What changes did you make after reading the comments?
Was This Post Helpful? 0
  • +
  • -

#9 zachkt  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 19-July 12

Re: Merging the numbers in 2 files, then writing numbers to third file

Posted 16 September 2012 - 08:58 PM

View Post#define, on 16 September 2012 - 08:39 PM, said:

What does the code look like now? What changes did you make after reading the comments?

I honestly didn't see anything saying what needed to be changed except for when you said "last" was uninitialized, but I actually initialized it at the beginning. The code remains the same so far.
Was This Post Helpful? 0
  • +
  • -

#10 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 980
  • View blog
  • Posts: 3,397
  • Joined: 19-February 09

Re: Merging the numbers in 2 files, then writing numbers to third file

Posted 16 September 2012 - 09:11 PM

Lines 25 to 33, I don't see the point of this.

You read all the numbers from the data file - only the last number is stored.

25	  // read the the data from first file
26	  // repeatedly overwrite an integer variable
27	  while(in_stream1 >> numbers1)
28	  {
29	    in_stream1 >> numbers1;
30	  }
31	 
32	  // close the first input file
33	  in_stream1.close();



The code doesn't do anything useful.
Was This Post Helpful? 0
  • +
  • -

#11 zachkt  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 19-July 12

Re: Merging the numbers in 2 files, then writing numbers to third file

Posted 16 September 2012 - 09:35 PM

View Post#define, on 16 September 2012 - 09:11 PM, said:

Lines 25 to 33, I don't see the point of this.

You read all the numbers from the data file - only the last number is stored.

25	  // read the the data from first file
26	  // repeatedly overwrite an integer variable
27	  while(in_stream1 >> numbers1)
28	  {
29	    in_stream1 >> numbers1;
30	  }
31	 
32	  // close the first input file
33	  in_stream1.close();



The code doesn't do anything useful.

doesn't line 25 to 33 say that if the first file is opened, everything inputted from the file will be saved as numbers1? or am I completely misusing the ">>" operator?

This post has been edited by zachkt: 16 September 2012 - 09:36 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1