issues using ofstream (Output stream class)

Good morning guys,

I'm having issues using ofstream (Output stream class). I have a text file in my thumb drive called infile and I'm using ifstream to copy 7 integers ,but when I use ofstream to copy all 7 integers on each record I always get a duplicate line at the end. see code below. Another question I have is how do I do this with just one variable instead of using 7 numbers ?


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 <iomanip>
using namespace std;
int main()
{
	int num1, num2, num3, num4, num5, num6, num7;
	int total;
	ifstream inFile;
	ofstream outFile;
	inFile.open("I:\\lab5a.txt");
	if (!inFile)
	{
		cout << "Cannot open input file. "
			<< "Program termintes!" << endl;
		return 1;
	}
	outFile.open("I:\\lab5a_output.txt");
	while (!inFile.eof())
  
	{
		inFile >> num1 >> num2 >> num3 >> num4 >> num5 >> num6 >> num7;
		outFile << left << setw(5) << num1 << " " << setw(5) << num2 << " "
			<< setw(5) << num3 << " " << setw(5) << num4 << " " << setw(5) << num5 << " "
			<< setw(5) << num6 << " " << setw(5) << num7 <<  endl;
	}
	return 0;
}



Example of outfile the last line is a duplicate.
346 130 982 90 656 117 595

415 948 126 4 558 571 87

415 948 126 4 558 571 87 Duplicate ..

Last edited on
I think the problem lies in the fact that once you have read the data values in the last line ... you aren't necessarily at the end of the file as CR and LF characters (I think) are still there. .eof is not a very reliable method here, though it may be better with getline than reading individual data.

Try preceding outFile on line 24 by
if (inFile)
which will be false once you have failed to read new data. Otherwise, you fail to read new data, so it simply duplicates the previous.
Thanks lastchance,

but can you give me an example ?. I did the following but it did not work .

if (inFile >> num1 >> num2 >> num3 >> num4 >> num5 >> num6 >> num7);
outFile << left << setw(5) << num1 << " " << setw(5) << num2 << " "
<< setw(5) << num3 << " " << setw(5) << num4 << " " << setw(5) << num5 << " "
<< setw(5) << num6 << " " << setw(5) << num7 << endl;
lastchance,

thank you it worked after adding if (inFile) at the end of the infile line like so
inFile >> num1 >> num2 >> num3 >> num4 >> num5 >> num6 >> num7; if (inFile)
Is there a way to use one variable instead of seven numbers ?
I meant that you should put the whole of
If (inFile)
INCLUDING the end ) before the outFile << ...

An alternative method (which I prefer) is
while (getline(inFile,line))
{
stringstream(line) >> num1 >> ... // as many as you want

You should declare line as a string and put
#include<string>
#include<sstream>
in the header.
Last edited on
Is there a way to use one variable instead of seven numbers ?


Something like this:

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
#include <fstream>
#include <iomanip>
#include <iostream>

int main()
{
    std::ifstream inFile("I:/lab5a.txt");
    if (!inFile)
    {
        std::cout << "Cannot open input file. Program terminates!\n";
        return 1;
    }

    std::ofstream outFile("I:/lab5a_output.txt");
    outFile << std::left;

    unsigned value_count = 0;
    int value;

    while (inFile >> value)
    {
        outFile << std::setw(5) << value;

        if (++value_count % 7 == 0) outFile << '\n';
    }
}
Topic archived. No new replies allowed.