I have two files with an unknown list of integers in ascending order.

What i want to do is send the contents of the two files into another file in ascending order.
We dont know the contents of the two numbers files.

so for example
File 1 may have the numbers:
1 3 4 5 6 9

File 2 may have the numbers:
2 3 10 11

The output file should have the two files like this.

1
2
3
3
4
5
6
9
10
11


I try using an array, but it cant work becaue we dont know the size of the two files.

Recommended Answers

All 3 Replies

Keep a counter when you are reading in each of the files and use that number to dynamically allocate the array (using new).

You don't need an array, this can be done by streaming the numbers to the output file directly using a merge algorithm. The merge algorithm is straightforward, and I'll encourage you to see what you can come up with on your own. However, merging is a common task, so you'll find heaps of examples online. And if you just can't figure it out, come back here and someone will outline it for you.

There are many ways you can approach this problem. I would just notepad to first
sort the file and then read it in and do whatever I need to do with it. But I assume
this is for learning purposes. So here is some things you can do :

1) Get the length of file and create an array dynamically and read it and doStuff();
2) Use vectors and get the input from both files. Then use std::sort to sort the vector
3) Append file#2 to file#1, then read it into a vector, then use std::sort
4) ... come up with your own idea

Just pick one and start.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.