Sorting Numbers from file

hey there. I am looking for some help. I have been working on this program for days now and for some reason my program will not write to my third file. It will call all of the integers that I need it to call but it will not write them out to a file. also I am unable to figure out how to use my loop correctly. If you could help I'd appreciate it!
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <iostream>
#include <fstream>

using namespace std;

int file1();
int file2();
int file3(int);

int main()
{
	int v1;
	int v2;
	ifstream fin;

	do
	{
		v1 = file1();
		v2 = file2();

		if (v1 < v2)
		{
			file3(v1);
		}
		else if (v2 < v1)
		{
			file3(v2);
		}

	} while (); // not sure how to count the numbers in the file and tell the program my file is empty..
	return 0;
}

int file1()
{
	int v1;
	ifstream fin;

	fin.open("file1.txt");
	if (fin.fail())
	{
		cout << "I am unable to open the file file1.txt" << endl;
		return 1;
	}

	fin >> v1;
	fin.close();
	return v1;
}

int file2()
{
	int v2;
	ifstream fin;

	fin.open("file2.txt");
	if (fin.fail())
	{
		cout << "I am unable to open the file file2.txt" << endl;
		return 1;
	}

	fin >> v2;
	fin.close();
	return v2;
}

int file3(int num)
{
	ifstream fin;
	fstream fout;

	fin.open("file3.txt");
	if (fin.fail())
	{
		cout << "I am unable to open the file file3.txt" << endl;
		return 1;
	}

	fout << num;
	fin.close();
	return 0;
}
Last edited on
1
2
3
4
5
6
7
8
9
int file3(int num)
{
	ifstream fin;
	fstream fout;//better to use ofstream here
//[...]
	fout << num; //You have never opened fout...
	fin.close();
	return 0;
}
Last edited on
even with that change it doesn't write to my third file..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int file3(int num)
{
	ifstream fin;
	ofstream fout;

	fin.open("file3.txt");
	if (fin.fail())
	{
		cout << "I am unable to open the file file3.txt" << endl;
		return 1;
	}

	cout << num;
	fout << num;
	fin.close();
	return 0;
}
Please point out line, where you have opened fout, for me. I still cannot find it anywhere in your code.
Last edited on
according to my professor (unless I interpreted this incorrectly) all you need to do in order to "open fout" as you say is to declare it as
ofstream fout;
please tell me otherwise if I am incorrect.
Ok, I have deleted everything not relating to fout from your function.
1
2
3
4
5
6
int file3(int num)
{
	ofstream fout;
	fout << num;
	return 0;
}
Which file, do you think, you writing to?

And if all you need to open file is to do something like ifstream fin;, why do you have this line in your code: fin.open("file3.txt");?

And final question: Why do you need an input stream in function which should output info to file
Last edited on
I see your point. I have fixed that problem. It was a misunderstanding. What I currently have is 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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <iostream>
#include <fstream>

using namespace std;

int file1();
int file2();
int file3(int);

int main()
{
	int v1;
	int v2;
	int lines(1);
	int i(0);

	cout << "Please enter the number of numbers in your file: ";
	cin >> lines;

	do
	{
		v1 = file1();
		v2 = file2();

		if (v1 < v2)
		{
			file3(v1);
		}
		else if (v2 < v1)
		{
			file3(v2);
		}
		i++;
	}while (i < lines);

	cout << "Your number ordering is complete. Please check file3.txt" << endl;

	return 0;
}

int file1()
{
	int v1;
	ifstream fin;

	fin.open("file1.txt");
	if (fin.fail())
	{
		cout << "I am unable to open the file file1.txt" << endl;
		return 1;
	}

	fin >> v1;
	fin.close();
	return v1;
}

int file2()
{
	int v2;
	ifstream fin;

	fin.open("file2.txt");
	if (fin.fail())
	{
		cout << "I am unable to open the file file2.txt" << endl;
		return 1;
	}

	fin >> v2;
	fin.close();
	return v2;
}

int file3(int num)
{
	ifstream fin;
	ofstream fout;

	fout.open("file3.txt");
	if (fin.fail())
	{
		cout << "I am unable to open the file file3.txt" << endl;
		return 1;
	}

	fout << num;
	fout.close();
	return 0;
}


This is the entirety of my code at the moment. I am now writing to file3.txt however I am only writing the first number. There are 5 numbers in each input file. When I have completed the entire program I end up with just a 3 in file3.txt when I should have this:


3 4 5 6 7 8 9 10 13 15


I don't know if you will be able to help me from here on out but I appreciate the help so far. Thank you!
How your code works now:
* open file1.txt, read first number, close file
* open file2.txt, read first number, close file
* find max number
* open file3.txt for writing, erasing everything it contained before, write number, close file

* open file1.txt, read first number, close file (first number will always be the same: you are not deleting it from file)
* open file2.txt, read first number, close file
* find max number
* open file3.txt for writing, erasing everything it contained before, write number, close file (if file reading was working right, you would see only last number in this file)

[...]
Could you show me in my code where I am erasing everything it contained before? I had no idea I was doing that. Closing the file doesn't erase everything does it? If so, would this be an appropriate fix to the problem?

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
int main()
{
	ofstream fout;
	int v1;
	int v2;
	int lines(1);
	int i(0);

	cout << "Please enter the number of numbers in your file: ";
	cin >> lines;

	do
	{
		v1 = file1();
		v2 = file2();

	fout.open("file3.txt");
	if (fout.fail())
	{
		cout << "I am unable to open the file file3.txt" << endl;
		return 1;
	}

		if (v1 < v2)
		{
			fout << v1;
		}
		else if (v2 < v1)
		{
			fout << v2; 
		}
		i++;
	}while (i < lines);

	fout.close();
	cout << "Your number ordering is complete. Please check file3.txt" << endl;

	return 0;
}


This would then get rid of the functionint file3(int);
Could you show me in my code where I am erasing everything it contained before?
There: fout.open("file3.txt");
This is default mode for outputting. You can just open file once and everything will be fine.
If you insist on opening file each time (it is slow and unnessesary load on HDD) you can look at append mode: http://cplusplus.com/reference/fstream/ofstream/open/
Last edited on
Thank you for your help but I got my program to work in a completely different manner. For anyone looking at this program, this was the code I ended up with.
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <iostream>
#include <fstream>

using namespace std;

int compare(int&, int&);

int main()
{
	int v1;
	int v2;
	int lines(0);
	int i(0);
	int returned;
	ifstream file1;
	ifstream file2;
	ofstream file3;

	file1.open("file1.txt");
	if (file1.fail())
	{
		cout << "I am unable to opent he file file1.txt" << endl;
		return 1;
	}
	file2.open("file2.txt");
	if (file2.fail())
	{
		cout << "I am unable to open the file file2.txt" << endl;
		return 1;
	}
	file3.open("file3.txt");
	if (file3.fail())
	{
		cout << "I am unable to opent the file file3.txt" << endl;
		return 1;
	}
	cout << "Please enter the number of numbers in your files (combined): ";
	cin >> lines;
	
	file1 >> v1;
	file2 >> v2;
	returned = compare(v1, v2);
	file3 << returned << " ";

	do
	{
		if(v1 > v2)
		{
			file2 >> v2;
		}
		else if(v2 > v1)
		{
			file1 >> v1;
		}
		returned = compare(v1, v2);
		file3 << returned << " ";
		i++;
	} while (i < (lines-2));

	if ( v1 > v2)
	{
		file3 << v1;
	}
	else if (v2 > v1)
	{
		file3 << v2;
	}
	

	cout << "Your number ordering is complete. Please check file3.txt" << endl;

	file1.close();
	file2.close();
	file3.close();

	return 0;
}

int compare(int& v1, int& v2)
{
	int output;
	if (v1 < v2)
	{
		output = v1;
	}
	else if (v2 < v1)
	{
		output = v2;
	}
	return output;
}
Topic archived. No new replies allowed.