Merging two text files to one gives a different output
up vote
1
down vote
favorite
One of the text files is rainfallToDate.txt, and it has:
0.01
1.74
0.19
0.65
0.50
0.10
0.00
0.02
0.01
0.06
1.57
7.76
while the other is averageRainfall.txt:
2.99
3.32
2.04
1.06
0.39
0.09
0.00
0.00
0.23
0.78
1.88
2.12
I am supposed to merge these two text files into a text file called rainfall.txt
Here is a sample output:
Rainfall for Cupertino: A Comparison
Month Average 2015
1 2.99 0.01
2 3.32 1.74
3 2.04 0.19
4 1.06 0.65
5 0.39 0.50
6 0.09 0.10
7 0.00 0.00
8 0.00 0.02
9 0.23 0.01
10 0.78 0.06
11 1.88 1.57
12 2.12 7.76
As you can see averageRainfall.txt is stored on the row "average" while the other text file rainfallToDate.txt is stored under 2015.
Well here is what I have so far and my attempt on "merging" the two text files.
My attempt:
#include <iostream> // for cout
#include <fstream> // for file I/O
#include <cstdlib> // for exit()
using namespace std;
int main()
{
ifstream fin;
ofstream fout;
if (fin.fail())
{
cout << "Input file failed to open.n";
exit(-1);
}
fout.open("rainfall.txt");
if (fout.fail())
{
cout << "Output file failed to open.n";
exit(-1);
}
fout << "Rainfall for Cupertino: A Comparisonn" << endl;
fout << "MonthtAveraget 2015tDeficitn" << endl;
for (int i = 1; i <= 12; i++) { // counts the month from 1-12
fout << fixed << i << endl;
}
fin.close();
fout.close();
// averageRainfall under averages
ifstream average;
ofstream oaverage;
average.open("averageRainfall.txt");
if (average.fail())
{
cout << "Input file failed to open.n";
exit(-1);
}
oaverage.open("rainfall.txt");
if (fout.fail())
{
cout << "Output file failed to open.n";
exit(-1);
}
oaverage << "t" << average;
// rainfallToDate under 2015
average.close();
oaverage.close();
ifstream ToDate;
ofstream oToDate;
ToDate.open("rainfallToDate.txt");
if (ToDate.fail())
{
cout << "Input file failed to open.n";
exit(-1);
}
oToDate.open("rainfall.txt");
if (oToDate.fail())
{
cout << "Output file failed to open.n";
exit(-1);
}
oToDate << "t" << "t" << ToDate;
ToDate.close();
oToDate.close();
return 0;
}
Now for the problem, the code runs and all doesn't state any kind of error, however, when I open up the file where both the code merges the file prints out
0x29fe64
What I believe I did wrong is that I didn't put the two textfiles "averages" and the "rainfalltodate" into two separate arrays and stating it from there. Correct me if I am wrong not very sure if I am supposed two put both of them in two separate arrays.
c++
add a comment |
up vote
1
down vote
favorite
One of the text files is rainfallToDate.txt, and it has:
0.01
1.74
0.19
0.65
0.50
0.10
0.00
0.02
0.01
0.06
1.57
7.76
while the other is averageRainfall.txt:
2.99
3.32
2.04
1.06
0.39
0.09
0.00
0.00
0.23
0.78
1.88
2.12
I am supposed to merge these two text files into a text file called rainfall.txt
Here is a sample output:
Rainfall for Cupertino: A Comparison
Month Average 2015
1 2.99 0.01
2 3.32 1.74
3 2.04 0.19
4 1.06 0.65
5 0.39 0.50
6 0.09 0.10
7 0.00 0.00
8 0.00 0.02
9 0.23 0.01
10 0.78 0.06
11 1.88 1.57
12 2.12 7.76
As you can see averageRainfall.txt is stored on the row "average" while the other text file rainfallToDate.txt is stored under 2015.
Well here is what I have so far and my attempt on "merging" the two text files.
My attempt:
#include <iostream> // for cout
#include <fstream> // for file I/O
#include <cstdlib> // for exit()
using namespace std;
int main()
{
ifstream fin;
ofstream fout;
if (fin.fail())
{
cout << "Input file failed to open.n";
exit(-1);
}
fout.open("rainfall.txt");
if (fout.fail())
{
cout << "Output file failed to open.n";
exit(-1);
}
fout << "Rainfall for Cupertino: A Comparisonn" << endl;
fout << "MonthtAveraget 2015tDeficitn" << endl;
for (int i = 1; i <= 12; i++) { // counts the month from 1-12
fout << fixed << i << endl;
}
fin.close();
fout.close();
// averageRainfall under averages
ifstream average;
ofstream oaverage;
average.open("averageRainfall.txt");
if (average.fail())
{
cout << "Input file failed to open.n";
exit(-1);
}
oaverage.open("rainfall.txt");
if (fout.fail())
{
cout << "Output file failed to open.n";
exit(-1);
}
oaverage << "t" << average;
// rainfallToDate under 2015
average.close();
oaverage.close();
ifstream ToDate;
ofstream oToDate;
ToDate.open("rainfallToDate.txt");
if (ToDate.fail())
{
cout << "Input file failed to open.n";
exit(-1);
}
oToDate.open("rainfall.txt");
if (oToDate.fail())
{
cout << "Output file failed to open.n";
exit(-1);
}
oToDate << "t" << "t" << ToDate;
ToDate.close();
oToDate.close();
return 0;
}
Now for the problem, the code runs and all doesn't state any kind of error, however, when I open up the file where both the code merges the file prints out
0x29fe64
What I believe I did wrong is that I didn't put the two textfiles "averages" and the "rainfalltodate" into two separate arrays and stating it from there. Correct me if I am wrong not very sure if I am supposed two put both of them in two separate arrays.
c++
You're doing a lot of things wrong: you're opening "rainfall.txt" for writing multiple times, and the default mode is "truncate" mode which wipes out existing content; you have multiple variables for the same output file which is pointless, but your bug is because you "oToDate << ToDate" doesn't copy the contents of one file to another, it just prints the address of the "ToDate" object into "oToDate".
– kfsone
Mar 19 '16 at 5:14
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
One of the text files is rainfallToDate.txt, and it has:
0.01
1.74
0.19
0.65
0.50
0.10
0.00
0.02
0.01
0.06
1.57
7.76
while the other is averageRainfall.txt:
2.99
3.32
2.04
1.06
0.39
0.09
0.00
0.00
0.23
0.78
1.88
2.12
I am supposed to merge these two text files into a text file called rainfall.txt
Here is a sample output:
Rainfall for Cupertino: A Comparison
Month Average 2015
1 2.99 0.01
2 3.32 1.74
3 2.04 0.19
4 1.06 0.65
5 0.39 0.50
6 0.09 0.10
7 0.00 0.00
8 0.00 0.02
9 0.23 0.01
10 0.78 0.06
11 1.88 1.57
12 2.12 7.76
As you can see averageRainfall.txt is stored on the row "average" while the other text file rainfallToDate.txt is stored under 2015.
Well here is what I have so far and my attempt on "merging" the two text files.
My attempt:
#include <iostream> // for cout
#include <fstream> // for file I/O
#include <cstdlib> // for exit()
using namespace std;
int main()
{
ifstream fin;
ofstream fout;
if (fin.fail())
{
cout << "Input file failed to open.n";
exit(-1);
}
fout.open("rainfall.txt");
if (fout.fail())
{
cout << "Output file failed to open.n";
exit(-1);
}
fout << "Rainfall for Cupertino: A Comparisonn" << endl;
fout << "MonthtAveraget 2015tDeficitn" << endl;
for (int i = 1; i <= 12; i++) { // counts the month from 1-12
fout << fixed << i << endl;
}
fin.close();
fout.close();
// averageRainfall under averages
ifstream average;
ofstream oaverage;
average.open("averageRainfall.txt");
if (average.fail())
{
cout << "Input file failed to open.n";
exit(-1);
}
oaverage.open("rainfall.txt");
if (fout.fail())
{
cout << "Output file failed to open.n";
exit(-1);
}
oaverage << "t" << average;
// rainfallToDate under 2015
average.close();
oaverage.close();
ifstream ToDate;
ofstream oToDate;
ToDate.open("rainfallToDate.txt");
if (ToDate.fail())
{
cout << "Input file failed to open.n";
exit(-1);
}
oToDate.open("rainfall.txt");
if (oToDate.fail())
{
cout << "Output file failed to open.n";
exit(-1);
}
oToDate << "t" << "t" << ToDate;
ToDate.close();
oToDate.close();
return 0;
}
Now for the problem, the code runs and all doesn't state any kind of error, however, when I open up the file where both the code merges the file prints out
0x29fe64
What I believe I did wrong is that I didn't put the two textfiles "averages" and the "rainfalltodate" into two separate arrays and stating it from there. Correct me if I am wrong not very sure if I am supposed two put both of them in two separate arrays.
c++
One of the text files is rainfallToDate.txt, and it has:
0.01
1.74
0.19
0.65
0.50
0.10
0.00
0.02
0.01
0.06
1.57
7.76
while the other is averageRainfall.txt:
2.99
3.32
2.04
1.06
0.39
0.09
0.00
0.00
0.23
0.78
1.88
2.12
I am supposed to merge these two text files into a text file called rainfall.txt
Here is a sample output:
Rainfall for Cupertino: A Comparison
Month Average 2015
1 2.99 0.01
2 3.32 1.74
3 2.04 0.19
4 1.06 0.65
5 0.39 0.50
6 0.09 0.10
7 0.00 0.00
8 0.00 0.02
9 0.23 0.01
10 0.78 0.06
11 1.88 1.57
12 2.12 7.76
As you can see averageRainfall.txt is stored on the row "average" while the other text file rainfallToDate.txt is stored under 2015.
Well here is what I have so far and my attempt on "merging" the two text files.
My attempt:
#include <iostream> // for cout
#include <fstream> // for file I/O
#include <cstdlib> // for exit()
using namespace std;
int main()
{
ifstream fin;
ofstream fout;
if (fin.fail())
{
cout << "Input file failed to open.n";
exit(-1);
}
fout.open("rainfall.txt");
if (fout.fail())
{
cout << "Output file failed to open.n";
exit(-1);
}
fout << "Rainfall for Cupertino: A Comparisonn" << endl;
fout << "MonthtAveraget 2015tDeficitn" << endl;
for (int i = 1; i <= 12; i++) { // counts the month from 1-12
fout << fixed << i << endl;
}
fin.close();
fout.close();
// averageRainfall under averages
ifstream average;
ofstream oaverage;
average.open("averageRainfall.txt");
if (average.fail())
{
cout << "Input file failed to open.n";
exit(-1);
}
oaverage.open("rainfall.txt");
if (fout.fail())
{
cout << "Output file failed to open.n";
exit(-1);
}
oaverage << "t" << average;
// rainfallToDate under 2015
average.close();
oaverage.close();
ifstream ToDate;
ofstream oToDate;
ToDate.open("rainfallToDate.txt");
if (ToDate.fail())
{
cout << "Input file failed to open.n";
exit(-1);
}
oToDate.open("rainfall.txt");
if (oToDate.fail())
{
cout << "Output file failed to open.n";
exit(-1);
}
oToDate << "t" << "t" << ToDate;
ToDate.close();
oToDate.close();
return 0;
}
Now for the problem, the code runs and all doesn't state any kind of error, however, when I open up the file where both the code merges the file prints out
0x29fe64
What I believe I did wrong is that I didn't put the two textfiles "averages" and the "rainfalltodate" into two separate arrays and stating it from there. Correct me if I am wrong not very sure if I am supposed two put both of them in two separate arrays.
c++
c++
edited Nov 11 at 8:26
Flimzy
36.5k96496
36.5k96496
asked Mar 19 '16 at 4:55
NaritaDogFight
254
254
You're doing a lot of things wrong: you're opening "rainfall.txt" for writing multiple times, and the default mode is "truncate" mode which wipes out existing content; you have multiple variables for the same output file which is pointless, but your bug is because you "oToDate << ToDate" doesn't copy the contents of one file to another, it just prints the address of the "ToDate" object into "oToDate".
– kfsone
Mar 19 '16 at 5:14
add a comment |
You're doing a lot of things wrong: you're opening "rainfall.txt" for writing multiple times, and the default mode is "truncate" mode which wipes out existing content; you have multiple variables for the same output file which is pointless, but your bug is because you "oToDate << ToDate" doesn't copy the contents of one file to another, it just prints the address of the "ToDate" object into "oToDate".
– kfsone
Mar 19 '16 at 5:14
You're doing a lot of things wrong: you're opening "rainfall.txt" for writing multiple times, and the default mode is "truncate" mode which wipes out existing content; you have multiple variables for the same output file which is pointless, but your bug is because you "oToDate << ToDate" doesn't copy the contents of one file to another, it just prints the address of the "ToDate" object into "oToDate".
– kfsone
Mar 19 '16 at 5:14
You're doing a lot of things wrong: you're opening "rainfall.txt" for writing multiple times, and the default mode is "truncate" mode which wipes out existing content; you have multiple variables for the same output file which is pointless, but your bug is because you "oToDate << ToDate" doesn't copy the contents of one file to another, it just prints the address of the "ToDate" object into "oToDate".
– kfsone
Mar 19 '16 at 5:14
add a comment |
3 Answers
3
active
oldest
votes
up vote
1
down vote
accepted
oToDate << "t" << "t" << ToDate;
I guess your intention here is to copy everything from the ToDate stream to the oToDate stream, but that's not quite what's happening. Instead, this is just attempting to write out a single value which represents ToDate -- which turns out to be the (meaningless) pointer address of the variable.
However, even if we fix this we can't quite do what you want at the moment. It looks like you're trying to first copy the average rainfall data, then in the second pass, skip what you've just written and put the rainfall to date next to it. But this isn't going to work -- the second pass is just going to overwrite the existing line.
Instead, what you need to do is to load up both datasets, and write them out in one go. One option is to load the two datasets separately and use a std::vector to hold the values before you write them back out. Let's see how we can do this:
std::ifstream average{"averageRainfall.txt"};
std::vector<double> average_data;
std::copy(std::istream_iterator<double>{average},
std::istream_iterator<double>{},
std::back_inserter(average_data));
This will copy all the data from the input file into a vector (make sure you look up these functions if you haven't seen them before so you know what's going on here!). We can do exactly the same for the second data set, so let's say we save that into a vector called to_date_data.
We then need to write both values out to a file at the same time. First, we open the output file:
std::ofstream rainfall{"rainfall.txt"};
Then we loop through the two vectors, copying the data to the output stream:
auto last = std::min(average_data.size(), to_date_data.size());
for (int i = 0; i < last; i++) {
rainfall << average_data[i] << "t" << to_date_data[i] << "n";
}
and you're all done :-)
add a comment |
up vote
0
down vote
Your strange output comes from this line:
oaverage << "t" << average;
average is a stream, and not a numerical value. When you try to print the stream, the stream ends up being converted to a pointer, and you are seeing a pointer value instead.
There's a similar issue on this line:
oToDate << "t" << "t" << ToDate;
add a comment |
up vote
0
down vote
maybe you should read some basic C++ programming book,here is my code:
#include <iostream> // for cout
#include <fstream> // for file I/O
#include <cstdlib> // for exit()
using namespace std;
int main()
{
std::ifstream fin_rain_date("rainfallToDate.txt");
std::ifstream fin_rain_ave("averageRainfall.txt");
if(fin_rain_ave==NULL||fin_rain_date==NULL)
{
std::cout<<"can not open file"<<std::endl;
}
std::ofstream fout("rainfall.txt");
fout<<"Rainfall for Cupertino: A Comparison"<<std::endl;
fout<<"MonthtAveraget 2015tDeficit"<<std::endl;
for(int i=0;i<12;i++)
{
fout<<i<<"t";
char tmpstr[256];
fin_rain_ave.getline(tmpstr,256);
fout<<tmpstr<<"t";
fin_rain_date.getline(tmpstr,256);
fout<<tmpstr<<std::endl;
}
return 0;
}
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
oToDate << "t" << "t" << ToDate;
I guess your intention here is to copy everything from the ToDate stream to the oToDate stream, but that's not quite what's happening. Instead, this is just attempting to write out a single value which represents ToDate -- which turns out to be the (meaningless) pointer address of the variable.
However, even if we fix this we can't quite do what you want at the moment. It looks like you're trying to first copy the average rainfall data, then in the second pass, skip what you've just written and put the rainfall to date next to it. But this isn't going to work -- the second pass is just going to overwrite the existing line.
Instead, what you need to do is to load up both datasets, and write them out in one go. One option is to load the two datasets separately and use a std::vector to hold the values before you write them back out. Let's see how we can do this:
std::ifstream average{"averageRainfall.txt"};
std::vector<double> average_data;
std::copy(std::istream_iterator<double>{average},
std::istream_iterator<double>{},
std::back_inserter(average_data));
This will copy all the data from the input file into a vector (make sure you look up these functions if you haven't seen them before so you know what's going on here!). We can do exactly the same for the second data set, so let's say we save that into a vector called to_date_data.
We then need to write both values out to a file at the same time. First, we open the output file:
std::ofstream rainfall{"rainfall.txt"};
Then we loop through the two vectors, copying the data to the output stream:
auto last = std::min(average_data.size(), to_date_data.size());
for (int i = 0; i < last; i++) {
rainfall << average_data[i] << "t" << to_date_data[i] << "n";
}
and you're all done :-)
add a comment |
up vote
1
down vote
accepted
oToDate << "t" << "t" << ToDate;
I guess your intention here is to copy everything from the ToDate stream to the oToDate stream, but that's not quite what's happening. Instead, this is just attempting to write out a single value which represents ToDate -- which turns out to be the (meaningless) pointer address of the variable.
However, even if we fix this we can't quite do what you want at the moment. It looks like you're trying to first copy the average rainfall data, then in the second pass, skip what you've just written and put the rainfall to date next to it. But this isn't going to work -- the second pass is just going to overwrite the existing line.
Instead, what you need to do is to load up both datasets, and write them out in one go. One option is to load the two datasets separately and use a std::vector to hold the values before you write them back out. Let's see how we can do this:
std::ifstream average{"averageRainfall.txt"};
std::vector<double> average_data;
std::copy(std::istream_iterator<double>{average},
std::istream_iterator<double>{},
std::back_inserter(average_data));
This will copy all the data from the input file into a vector (make sure you look up these functions if you haven't seen them before so you know what's going on here!). We can do exactly the same for the second data set, so let's say we save that into a vector called to_date_data.
We then need to write both values out to a file at the same time. First, we open the output file:
std::ofstream rainfall{"rainfall.txt"};
Then we loop through the two vectors, copying the data to the output stream:
auto last = std::min(average_data.size(), to_date_data.size());
for (int i = 0; i < last; i++) {
rainfall << average_data[i] << "t" << to_date_data[i] << "n";
}
and you're all done :-)
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
oToDate << "t" << "t" << ToDate;
I guess your intention here is to copy everything from the ToDate stream to the oToDate stream, but that's not quite what's happening. Instead, this is just attempting to write out a single value which represents ToDate -- which turns out to be the (meaningless) pointer address of the variable.
However, even if we fix this we can't quite do what you want at the moment. It looks like you're trying to first copy the average rainfall data, then in the second pass, skip what you've just written and put the rainfall to date next to it. But this isn't going to work -- the second pass is just going to overwrite the existing line.
Instead, what you need to do is to load up both datasets, and write them out in one go. One option is to load the two datasets separately and use a std::vector to hold the values before you write them back out. Let's see how we can do this:
std::ifstream average{"averageRainfall.txt"};
std::vector<double> average_data;
std::copy(std::istream_iterator<double>{average},
std::istream_iterator<double>{},
std::back_inserter(average_data));
This will copy all the data from the input file into a vector (make sure you look up these functions if you haven't seen them before so you know what's going on here!). We can do exactly the same for the second data set, so let's say we save that into a vector called to_date_data.
We then need to write both values out to a file at the same time. First, we open the output file:
std::ofstream rainfall{"rainfall.txt"};
Then we loop through the two vectors, copying the data to the output stream:
auto last = std::min(average_data.size(), to_date_data.size());
for (int i = 0; i < last; i++) {
rainfall << average_data[i] << "t" << to_date_data[i] << "n";
}
and you're all done :-)
oToDate << "t" << "t" << ToDate;
I guess your intention here is to copy everything from the ToDate stream to the oToDate stream, but that's not quite what's happening. Instead, this is just attempting to write out a single value which represents ToDate -- which turns out to be the (meaningless) pointer address of the variable.
However, even if we fix this we can't quite do what you want at the moment. It looks like you're trying to first copy the average rainfall data, then in the second pass, skip what you've just written and put the rainfall to date next to it. But this isn't going to work -- the second pass is just going to overwrite the existing line.
Instead, what you need to do is to load up both datasets, and write them out in one go. One option is to load the two datasets separately and use a std::vector to hold the values before you write them back out. Let's see how we can do this:
std::ifstream average{"averageRainfall.txt"};
std::vector<double> average_data;
std::copy(std::istream_iterator<double>{average},
std::istream_iterator<double>{},
std::back_inserter(average_data));
This will copy all the data from the input file into a vector (make sure you look up these functions if you haven't seen them before so you know what's going on here!). We can do exactly the same for the second data set, so let's say we save that into a vector called to_date_data.
We then need to write both values out to a file at the same time. First, we open the output file:
std::ofstream rainfall{"rainfall.txt"};
Then we loop through the two vectors, copying the data to the output stream:
auto last = std::min(average_data.size(), to_date_data.size());
for (int i = 0; i < last; i++) {
rainfall << average_data[i] << "t" << to_date_data[i] << "n";
}
and you're all done :-)
answered Mar 19 '16 at 5:18
Tristan Brindle
10.9k12065
10.9k12065
add a comment |
add a comment |
up vote
0
down vote
Your strange output comes from this line:
oaverage << "t" << average;
average is a stream, and not a numerical value. When you try to print the stream, the stream ends up being converted to a pointer, and you are seeing a pointer value instead.
There's a similar issue on this line:
oToDate << "t" << "t" << ToDate;
add a comment |
up vote
0
down vote
Your strange output comes from this line:
oaverage << "t" << average;
average is a stream, and not a numerical value. When you try to print the stream, the stream ends up being converted to a pointer, and you are seeing a pointer value instead.
There's a similar issue on this line:
oToDate << "t" << "t" << ToDate;
add a comment |
up vote
0
down vote
up vote
0
down vote
Your strange output comes from this line:
oaverage << "t" << average;
average is a stream, and not a numerical value. When you try to print the stream, the stream ends up being converted to a pointer, and you are seeing a pointer value instead.
There's a similar issue on this line:
oToDate << "t" << "t" << ToDate;
Your strange output comes from this line:
oaverage << "t" << average;
average is a stream, and not a numerical value. When you try to print the stream, the stream ends up being converted to a pointer, and you are seeing a pointer value instead.
There's a similar issue on this line:
oToDate << "t" << "t" << ToDate;
answered Mar 19 '16 at 5:05
Vaughn Cato
52.5k45997
52.5k45997
add a comment |
add a comment |
up vote
0
down vote
maybe you should read some basic C++ programming book,here is my code:
#include <iostream> // for cout
#include <fstream> // for file I/O
#include <cstdlib> // for exit()
using namespace std;
int main()
{
std::ifstream fin_rain_date("rainfallToDate.txt");
std::ifstream fin_rain_ave("averageRainfall.txt");
if(fin_rain_ave==NULL||fin_rain_date==NULL)
{
std::cout<<"can not open file"<<std::endl;
}
std::ofstream fout("rainfall.txt");
fout<<"Rainfall for Cupertino: A Comparison"<<std::endl;
fout<<"MonthtAveraget 2015tDeficit"<<std::endl;
for(int i=0;i<12;i++)
{
fout<<i<<"t";
char tmpstr[256];
fin_rain_ave.getline(tmpstr,256);
fout<<tmpstr<<"t";
fin_rain_date.getline(tmpstr,256);
fout<<tmpstr<<std::endl;
}
return 0;
}
add a comment |
up vote
0
down vote
maybe you should read some basic C++ programming book,here is my code:
#include <iostream> // for cout
#include <fstream> // for file I/O
#include <cstdlib> // for exit()
using namespace std;
int main()
{
std::ifstream fin_rain_date("rainfallToDate.txt");
std::ifstream fin_rain_ave("averageRainfall.txt");
if(fin_rain_ave==NULL||fin_rain_date==NULL)
{
std::cout<<"can not open file"<<std::endl;
}
std::ofstream fout("rainfall.txt");
fout<<"Rainfall for Cupertino: A Comparison"<<std::endl;
fout<<"MonthtAveraget 2015tDeficit"<<std::endl;
for(int i=0;i<12;i++)
{
fout<<i<<"t";
char tmpstr[256];
fin_rain_ave.getline(tmpstr,256);
fout<<tmpstr<<"t";
fin_rain_date.getline(tmpstr,256);
fout<<tmpstr<<std::endl;
}
return 0;
}
add a comment |
up vote
0
down vote
up vote
0
down vote
maybe you should read some basic C++ programming book,here is my code:
#include <iostream> // for cout
#include <fstream> // for file I/O
#include <cstdlib> // for exit()
using namespace std;
int main()
{
std::ifstream fin_rain_date("rainfallToDate.txt");
std::ifstream fin_rain_ave("averageRainfall.txt");
if(fin_rain_ave==NULL||fin_rain_date==NULL)
{
std::cout<<"can not open file"<<std::endl;
}
std::ofstream fout("rainfall.txt");
fout<<"Rainfall for Cupertino: A Comparison"<<std::endl;
fout<<"MonthtAveraget 2015tDeficit"<<std::endl;
for(int i=0;i<12;i++)
{
fout<<i<<"t";
char tmpstr[256];
fin_rain_ave.getline(tmpstr,256);
fout<<tmpstr<<"t";
fin_rain_date.getline(tmpstr,256);
fout<<tmpstr<<std::endl;
}
return 0;
}
maybe you should read some basic C++ programming book,here is my code:
#include <iostream> // for cout
#include <fstream> // for file I/O
#include <cstdlib> // for exit()
using namespace std;
int main()
{
std::ifstream fin_rain_date("rainfallToDate.txt");
std::ifstream fin_rain_ave("averageRainfall.txt");
if(fin_rain_ave==NULL||fin_rain_date==NULL)
{
std::cout<<"can not open file"<<std::endl;
}
std::ofstream fout("rainfall.txt");
fout<<"Rainfall for Cupertino: A Comparison"<<std::endl;
fout<<"MonthtAveraget 2015tDeficit"<<std::endl;
for(int i=0;i<12;i++)
{
fout<<i<<"t";
char tmpstr[256];
fin_rain_ave.getline(tmpstr,256);
fout<<tmpstr<<"t";
fin_rain_date.getline(tmpstr,256);
fout<<tmpstr<<std::endl;
}
return 0;
}
answered Mar 19 '16 at 5:11
an unique monkey
33319
33319
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f36098024%2fmerging-two-text-files-to-one-gives-a-different-output%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
You're doing a lot of things wrong: you're opening "rainfall.txt" for writing multiple times, and the default mode is "truncate" mode which wipes out existing content; you have multiple variables for the same output file which is pointless, but your bug is because you "oToDate << ToDate" doesn't copy the contents of one file to another, it just prints the address of the "ToDate" object into "oToDate".
– kfsone
Mar 19 '16 at 5:14