#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;
}
10 Replies - 237 Views - Last Post: 16 September 2012 - 09:35 PM
#1
Merging the numbers in 2 files, then writing numbers to third file
Posted 16 September 2012 - 04:13 PM
Replies To: Merging the numbers in 2 files, then writing numbers to third file
#2
Re: Merging the numbers in 2 files, then writing numbers to third file
Posted 16 September 2012 - 05:26 PM
zachkt, on 17 September 2012 - 02:13 AM, said:
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 }
#3
Re: Merging the numbers in 2 files, then writing numbers to third file
Posted 16 September 2012 - 05:47 PM
#define, on 16 September 2012 - 05:26 PM, said:
zachkt, on 17 September 2012 - 02:13 AM, said:
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
#4
Re: Merging the numbers in 2 files, then writing numbers to third file
Posted 16 September 2012 - 06:44 PM
22 while(in_stream1 >> numbers1)
23 {
24 cout << numbers1;
25 }
26
#5
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?
#6
Re: Merging the numbers in 2 files, then writing numbers to third file
Posted 16 September 2012 - 07:45 PM
#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.
#7
Re: Merging the numbers in 2 files, then writing numbers to third file
Posted 16 September 2012 - 08:31 PM
This post has been edited by zachkt: 16 September 2012 - 08:32 PM
#8
Re: Merging the numbers in 2 files, then writing numbers to third file
Posted 16 September 2012 - 08:39 PM
#9
Re: Merging the numbers in 2 files, then writing numbers to third file
Posted 16 September 2012 - 08:58 PM
#define, on 16 September 2012 - 08:39 PM, said:
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.
#10
Re: Merging the numbers in 2 files, then writing numbers to third file
Posted 16 September 2012 - 09:11 PM
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.
#11
Re: Merging the numbers in 2 files, then writing numbers to third file
Posted 16 September 2012 - 09:35 PM
#define, on 16 September 2012 - 09:11 PM, said:
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
|
|

New Topic/Question
Reply




MultiQuote




|