Over coding a simple file program

Could someone tell me how I could tell me how I could cut this program down a bit. I feel I'm am greatly over coding it with the fin statements.

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

int main(int argc, char * argv[])
{
	using namespace std;
	
	char ch;
	string temp, temp2;
	
	if (argc < 4)
	{
		cerr << "Usage: " << argv[0] << " filename[s]\n";
		exit(EXIT_FAILURE);
	}
	
	ifstream fin(argv[1], ios_base::in);
	ifstream fin2(argv[2], ios_base::in);
	ofstream fout(argv[3], ios_base::out | ios_base::trunc);
	
	while (!fin.eof() || !fin2.eof())
	{
		while (fin.get(ch))
			if (ch != '\n')
				fout << ch;
			else
				break;
		fout << ' ';
		while (fin2.get(ch))
			if (ch != '\n')
				fout << ch;
			else
				break;
		fout << endl;
	}
		
	return 0;
}


And here are the text files I use:

1
2
3
eggs kites donuts
balloons hammers
stones


1
2
zero lassitude
finance drama


And the third file can be any .txt file or none at all since it will always be truncated.
Last edited on
I was able to reduce the while loop down to this but I still think it could be trimmed even further.

1
2
3
4
5
6
7
8
9
10
11
12
	while (!fin.eof() || !fin2.eof())
	{
		if (fin.peek() == EOF)
			fin.get();
		else
			getline(fin, temp);
		if (fin2.peek() == EOF)
			fin2.get();
		else
			getline(fin2, temp2);
		fout << temp << ' ' << temp2 << endl;
	}
1
2
while ( getline(fin, temp) && getline(fin2, temp2) )
    fout << temp << ' ' << temp2 << '\n' ;
1
2
3
4
5
6
7
8
9
while (!fin.eof() || !fin2.eof())
	{
		while (fin.get(ch) && ch!='\n')
			fout << ch;
		fout << ' ';
		while (fin2.get(ch) && ch!='\n')
			fout << ch;
		fout << endl;
	}

Although yours works cire it doesn't display the line stone which I need to be displayed. K0t4k0t4 is the best I've seen so far, either way thank you both for sharing your insight.
Last edited on
Although yours works cire it doesn't display the line stone which I need to be displayed. K0t4k0t4 is the best I've seen so far, either way thank you both your insight.


I don't see much point in making it work for a one-off difference if it doesn't also work for an arbitrary difference, nevertheless:

1
2
3
4
5
while ( getline(fin, temp) && getline(fin2, temp2) )
    fout << temp << ' ' << temp2 << '\n' ;

if ( fin || fin2 )
    fout << (fin ? temp : temp2) << '\n' ;
Last edited on
1
2
3
4
5
while (!fin.eof() || !fin2.eof())
	{
		getline(fin, temp) && getline(fin2, temp2);
                fout << temp << ' ' << temp2 << '\n' ;
	}

I really liked cire's approach so i combined ours.
Last edited on
^Note that eof is not set until AFTER you have tried to read and failed so you will print out two copies of the lines at the end. That is why cire was looping on the getline()s themselves; they will return false when they fail, meaning you will not get the duplicates at the end of the file.
on the test files he provided it does not produce duplicates on the last line on my system. Im not trying to argue that its correct i would actually never program it this way for a real use i was simply shooting for the smallest loop that produced the same output as the original for this case.
I just realized that I misinterpreted the original code by somehow mistaking the || for an &&. If we're going for compact and cute:

1
2
while ( getline(fin, temp) || getline (fin2, temp2) )
    fout << (fin ? temp + ' ' + (fin2?temp2:string()) : temp2) + '\n' ;
while ( getline(fin, temp) || getline (fin2, temp2) )

May be
while ( getline(fin, temp) && getline (fin2, temp2) )
Topic archived. No new replies allowed.