Sorting a text file

My assignment is to read two separate text files(file1.txt and file2.txt) and merge them into a 3rd file(output.txt), then sort the new output file and display. File1 is simply odd numbers from 1 to 9, and file2 is the even numbers from 0 to 8.

I successfully created a program to merge the two files into the third file, but I'm stuck on how to sort the file before displaying it. Is there a function that'll do that?

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
#include "stdafx.h"
#include <iostream>
#include <algorithm>
#include <fstream>


using namespace std;




int main()
{
	const int SIZE = 20;
	char input1[SIZE];
	char input2[SIZE];
	char input3[SIZE];
	char output1[SIZE];

	ifstream in_stream1;
	ifstream in_stream2;
	ifstream in_stream3;
	ofstream out_stream;

// Open file1.txt and display
	in_stream1.open("file1.txt");
	if (in_stream1.fail())
	{
		cout << "Input file opening failed.\n";
		exit(1);
	}
	in_stream1.getline(input1, SIZE);
	cout << "The contents of file1: " << input1 << endl;

	out_stream.open("file3.txt", ios::out);
	out_stream << input1 <<"";
	out_stream.close();

	

// Open file2.txt and display
	in_stream2.open("file2.txt");
	if (in_stream2.fail())
	{
		cout << "Input file opening failed. \n";
		exit(1);
	}
	in_stream2.getline(input2, SIZE);
	cout << "The contents of file2: " << input2 << endl;

	out_stream.open("file3.txt", ios::app);
	out_stream << input2;
	out_stream.close();

	
	

	
	in_stream3.open("file3.txt");
	in_stream3.getline(input3, SIZE);
	cout << "The contents of file3: " << input3 << endl;
	
	in_stream1.close();
	in_stream2.close();
	in_stream3.close();
	out_stream.close();
	
}
in_stream1.getline(input1, SIZE); on line 32 does not extract integers, the same thing goes for line 42.

You need to extract the numbers from both files and store them in a single std::vector<int>. After that you have to sort the container using std::sort(). After that you have to traverse the std::vector<int> printing each element into the third file.
Last edited on
Vector isn't something I'm familiar with. This is an Intro to C++ course, so that may be above my pay grade. I'll do some research on it. Thanks for the input.

Any ideas would be appreciated.
Last edited on
As a programmer you are regularly going to have to research topics that you are not familiar with, but that are required by the needs of the project you are working on.
I did a little research, but I'm still not sure how to go about storing the numbers in a vector. I understand sort, I've used that before.

Will vector automatically convert a char into an int?
Will vector automatically convert a char into an int?


No.

Instead of extracting the data from the file as characters you should extract the data as integers. For example, let's say you have a file called "a.txt" with the following data:

1
2
3
1
2
3


Here's how to extract the numbers from the file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <fstream>
#include <vector>

int main()
{
	std::ifstream ifs("a.txt");
	std::vector<int> numbers;
	int temp = 0;
	while(ifs >> temp)
	{
		numbers.push_back(temp);
	}

	return 0;
}
Last edited on
I'm trying to follow the logic. Forgive me, I haven't programmed since I had a TRS-80 Color as a kid.

Basically, Instead of extracting with a getline function and writing that into file3, I extract the data from file1 and file2 into a temporary vector of ints, sort that, then write the contents of the vector into file3?

I'm not sure of what you meant by "traverse" too.
Last edited on
I understand I can use the atoi function to convert the string into ints, but I'm not sure how to deal with the whitespaces.
Last edited on
I extract the data from file1 and file2 into a temporary vector of ints, sort that, then write the contents of the vector into file3?


Exactly.

I'm not sure of what you meant by "traverse" too.


It just a fancy word programmers use which means to go through each element in a container, to travel through the container, to iterate over it.
Last edited on
Topic archived. No new replies allowed.