File to File

I am completely unsure of how to take data from one file and input that information to another file. The first file I have is:
2
13
14
25
37
49
50
61
72
89
90

file2:
6
12
15
26
37
48
59
60
71
82
93

and I need to write all of that into another file. Taking two files and putting them all into a third.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

#include <iostream>
#include <fstream>
using namespace std;

/*

*/

int main ()
{
        //declarations
        ifstream text1;
        ifstream text2;
        ofstream text3;

  
1. Copy content fo text1 into text3.
2. Copy content fo text2 into text3.

"Copy" as in "Read something from input and write it to output. Repeat until input is empty."
Could you be more descriptive? I am not sure on how to do that, also I need to organize the inputs from smallest to largest and remove duplicates. Thanks
also I need to organize the inputs from smallest to largest and remove duplicates

That changes the question completely. In the original question it was simply a matter of something like this:
1
2
3
4
5
6
7
8
9
10
11
    ifstream text1("input..txt");
    ifstream text2("input2.txt");
    ofstream text3("output.txt");

    int num;
    
    while (text1 >> num)
        text3 << num << '\n';
    
    while (text2 >> num)
        text3 << num << '\n';

... but if you need to change the order, then storing the data in some sort of container would be required. A std::set might fit the task perfectly, it will put the values in sequence and remove duplicates automatically.
http://www.cplusplus.com/reference/set/set/
WE ARE FUCKED.... I have no idea what I am doing. Spent 2 and half hours on this. and I got 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
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

/*

*/
void sort(int, int);
void swap(int, int);
int indexx(int, int, int);
int main ()
{
        //declarations
        ifstream text1;
        ifstream text2;
        ofstream text3;
        string line;
        string word;
        string array[21];
        int loop=0;
        int number = 21;

        text1.open("file1.txt");
        getline (text1,line, '\0');
        text2.open("file2.txt");
        getline (text2,word, '\0');
        array[loop] = line + word;
        cout << array[loop] << endl;
        sort(array[], number);





        text3.open("file3.txt");
        text3 << array[loop];
}


void sort(int array[], int number)
{
        int b;
        for (int index = 0; index < number - 1; index++)
        {
            b = indexx(array[], index, number);
            swap(array[index], array[indexx]);
        }
}

void swap(int& v1, int& v2)
{
        int temp;
        temp = v1;
        v1 = v2;
        v2 = temp;
}

int indexx(const int array[], int start, int number)
{
        int min = array[start];
        int dex = start
        for (int index = start + 1; index < number; index++)
            if (array[index] < min)
            {
                min = array[index];
                dex = index;
            }
        return dex;


so many errors... doesn't work... I cried so much...
If the input files consist of a list of numbers, then why are you using complicated stuff like strings and getline(), when you could just get the numbers directly
1
2
int num;
text1 >> num;


Also, it looks as though your two input files are each sorted in ascending sequence. If that is so, there is no need to store and sort the data, you could just read a value from each file, and output whichever value is the lowest, and keep on reading from each file until there is no more data.

Examples here: http://www.cplusplus.com/forum/beginner/110146/
Last edited on
Reference documentation for std::merge on this site has an example as well.
I'm not sure on this one, can std::merge remove duplicates?
Good point, it doesn't. It is std::set_union that does. The library has so many algorithms ...
Ok, thanks. I looked at this many different ways. The shortest code I came up with used std::set.

@bridgster I looked at your code, it was a bit muddled, so I tidied up some of it. But I wasn't sure about the sort routine.

You could press on with this approach, if you can get the sort working, and probably eliminate duplicates during the output phase; However, this is pretty much re-inventing the wheel, you could use std::sort for sorting, and a std::vector might be simpler than an array, though it does the same job.

I guess it depends upon what constraints you are obliged to comply with, due to course requirements etc.

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

using namespace std;

void sort(string *, int);
void swap(string &, string &);
int indexx(const string array[], int start, int number);

int main ()
{
    ifstream text1("file1.txt");
    ifstream text2("file2.txt");
    ofstream text3("file3.txt");

    string line;

    const int size = 200;
    string array[size];

    int loop=0;

    while (getline (text1, line) )
        array[loop++] = line;

    while (getline (text2, line) )
        array[loop++] = line;

    sort(array, loop);

    for (int i=0; i<loop; i++)
        text3 << array[i] << "\t";

    return 0;
}
Last edited on
Topic archived. No new replies allowed.