input text from one file to another file question

Mar 21, 2012 at 11:18pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <fstream>
#include <string>


using namespace std;

int main()
{

    ifstream wormfile("worm.txt");
    ofstream dtfile("dt.txt");
    string wormline;


//while (!wormfile.eof())
//{
getline(wormfile, wormline);
//cout << wormline << endl;
//}

dtfile << wormline << endl;
cout << " dtfile has the data" << endl;

    wormfile.close();
    dtfile.close();
    return 0;

}


worm.txt has:
1
2
3
I like pie
I like rice
and I like applesauce


dt.txt has nothing.

I am trying to get worm.txt to copy all of its data to dt.txt. My problem is that the program only gets the first line and doesn't get the other lines.
Mar 21, 2012 at 11:47pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

#include <iostream>
#include <fstream>
#include <string>


using namespace std;

int main()
{

    ifstream wormfile("worm.txt");
    ofstream dtfile("dt.txt");
    string wormline;


getline(wormfile, wormline);
dtfile << wormline << endl;
getline(wormfile, wormline);
dtfile << wormline << endl;
getline(wormfile, wormline);
dtfile << wormline << endl;

cout << " dtfile has the data" << endl;

    wormfile.close();
    dtfile.close();
    return 0;

}


update. I found out how to copy the worm.txt text to dt.txt but now I am trying to do it without having to do all of this:

1
2
3
4
5
6
getline(wormfile, wormline);
dtfile << wormline << endl;
getline(wormfile, wormline);
dtfile << wormline << endl;
getline(wormfile, wormline);
dtfile << wormline << endl;


What kind of loop would I use to compare both of the files to stop at the end of the file?

does that make sense?
Mar 22, 2012 at 4:32am
1
2
3
for(int i = 0; i < 3; i++){
std::getline(wormfie, wormline);
dtfile << wormline << '\n'; 

will acompish what you want.
Topic archived. No new replies allowed.