my output text file grows uncontrollably

Can anyone tell me why/how I'm messing up this simple code?

It reads a text file and outputs what it read to a new text file. (it's supposed to anyways)

It reads fine and if I output to the console, no problem. But...

The new text file it creates grows exponentially until I end the program. No information is in the huge (empty) file.

What am I doing wrong?

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main ()
{
string line;
string filename;
string output;
ifstream myfile (filename);
ofstream outfile (output);

cout << "enter the input file name: ";
 cin >> filename;

cout<<"enter the output file name:  ";
cin>>output;

myfile.open(filename.c_str());

if(myfile.is_open())

	while ( myfile.good())
	{
		getline(myfile,line);
	}
else cout<<"Unable to open file";

myfile.close();

outfile.open(output.c_str());

if(outfile.is_open())
		
			while ( outfile.good())
			{
				outfile<<line;
			}
else cout<<"Unable to open file";		
		
outfile.close();


return 0;
}
My guess is because you output the same empty line to outfile over and over and over again.

1
2
3
4
			while ( outfile.good())
			{
				outfile<<line;
			}


Where does line change in this? When do you expect outfile to suddenly stop being 'good'?
1
2
3
4
while ( outfile.good())
			{
				outfile<<line;
			}
This is an infinite loop, continually adding the same line to the file.
oh my goodness, I thought my variable "line" was holding the text input from the input file and that once line was put into my outfile it would end at the null.

so it seems my entire logic is flawed here. what do you think? should I create an array in place of my line variable to hold the text input from the text file?

I got confused because when I output "line" to the screen, I got the correct result. It was only when I tried t write it to the output file that it blew up.

edited to add:

or --- are you saying that it's the while loop in the output that is causing the problem? maybe I don't need the while loop there at all?
Last edited on
The obvious thing to do, if you're just copying the content verbatim with line-oriented operations would be to open infile for reading, outfile for writing and:

1
2
while ( getline(infile, line) )
    outfile << line << '\n' ;


[edit: replaced >> with <<]
Last edited on
I tried removing the while loop real quick. Now by output file doesn't blow up. You guys are so smart! Thank you.

But only get one line of my text into the output file. I should have all 4-5 lines in it. Basically it should have everything the input file had in it.

For example, if my input is
"Hello world.
Today is Saturday.
I spend all my free time learning C++."
Then my output text file should contain all 3 lines.

Does it stop writing because of the carriage return?
Do I need some other type to hold the entire string?
Is it an array I need?

Thanks again for your help.
oh thanks cire, I didn't see your reply before I kept writing.

Ok, I'm going to go try that. THANKS!!!
THANK YOU SOOOO MUCH!!!

Here's my pretty new code.

I really love learning this, but I honestly have to say that if it wasn't for all of you wonderful people helping and encouraging me as I learn, I may have thrown in the towel several times already. So, thank you very much.

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
31
32
33
34
35
36
37
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main ()
{
string line;
string filename;
string output;
ifstream myfile (filename);
ofstream outfile (output);

cout << "enter the input file name: ";
cin >> filename;

cout<<"enter the output file name:  ";
cin>>output;

myfile.open(filename.c_str());
outfile.open(output.c_str());

if(myfile.is_open())

	if(outfile.is_open())
{
	while ( getline(myfile, line) )
		outfile << line <<'\n';
}
else cout<<"Unable to open input file";
	else cout<<"Unable to open output file";	

myfile.close();		
outfile.close();

return 0;
}
Topic archived. No new replies allowed.