Output file only has first value

Hey everyone I'm trying to open a file and then output the data from the file to a new one. Unfortunately I'm only getting the first value from the original file into the new one. Here's the code I have. Any help appreciated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include<iostream>
#include<fstream>

using namespace std;

int main()
{

	ifstream audioFile;
	audioFile.open("audioframes.txt");
	int data;
	audioFile >> data;
	if (!audioFile.is_open()) {
		cout << "Failure to open file." << endl;
	}
	else {
		cout << "File opened successfully." << endl;
		ofstream outFile("Test file.txt");
		outFile << data;
		outFile.close();
	}

	return 0;
}
On line 12 you read in a single int.
On line 19 you write out that single int.

Write a loop and repeat that over and over until there's nothing left to read in.
Thanks for the reply. I tried using "while (audioFile.good())" but I'm not really sure how to end the loop. The output file also seems to have a random value from the first file, rather than listing all of the values. You said to create a loop until there was nothing left to read in but I'm not sure how to change the position of the value that's being read in, as in instead of reading in the first value what statement can I use to move onto the 2nd value, then the 3rd etc; Assuming that's what you meant.
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>
using namespace std;

int main()
{

	ifstream audioFile;
	audioFile.open("audioframes.txt");
	int data;
	

	if (!audioFile.is_open()) {
		cout << "Failure to open file." << endl;
	}
	else {
		cout << "File opened successfully." << endl;
	}
	while (audioFile.good()) {
		audioFile >> data;
		ofstream outFile("Test file.txt");
		outFile << data;
		outFile.close();

		
	}

	return 0;
}
The logic here is a bit mixed up:
19
20
21
22
23
24
25
26
	while (audioFile.good()) {
		audioFile >> data;
		ofstream outFile("Test file.txt");
		outFile << data;
		outFile.close();

		
	}

The first thing to do is to move the open and close of the outFile outside that loop:
19
20
21
22
23
24
25
26
    ofstream outFile("Test file.txt");

    while (audioFile.good()) {
        audioFile >> data;
        outFile << data;
    }

    outFile.close();

That would be heading in the right direction. However there is a logic error remaining. After the input audioFile >> data; there is no check whether or not it worked. The end of the file might have been reached. Or some other error, such as trying to read an integer and instead finding an alphabetic character.

A good way to handle that is to move the input statement itself inside the condition of the while loop.
19
20
21
22
23
24
25
    ofstream outFile("Test file.txt");

    while (audioFile >> data) {
        outFile << data;
    }

    outFile.close();

Now the body of the loop (line 22) is executed only after a successful read at line 21.

Also, the statement outFile.close(); though correct, is unnecessary. When the end of main() is reached, the file streams will be automatically closed when they go out of scope.
Last edited on
Thanks a lot that worked!
Topic archived. No new replies allowed.