Sorting file contents with C++

Hello,
I want to sort the content of my file. but write to file isn't successfully.(No changes to the file)

my file has two columns.
Name Number
John 12345
Alex 1203
...

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
 #include<iostream>
#include<fstream>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;

struct Person
{
    int number;
    string name;
};
bool compareByLength(const Person& a, const Person& b)
{
    return  a.name > b.name;
}
int main()
{
    int number;
    string name;
    vector<Person> Persons;
    Person p;
    fstream inputFile;
    inputFile.open("myfile.txt");
    if (!inputFile)
    {
        cout << "Sorry!, cannot open this file\n";
    }
    else
    {
        while (!inputFile.eof())
        {
            inputFile >> name >> number;
            p.name = name;
            p.number = number;
            Persons.push_back(p);
        }
    }
    sort(Persons.begin(), Persons.end(), compareByLength);
    int i = 0;
    while (i < Persons.size())
    {
        inputFile<< Persons[i].name <<"\t" << Persons[i].number<<endl;
            i++;
    }

    inputFile.close();


    return 0;
}
Last edited on
What is the condition of the "inputFile" when you're trying to write to it?

What do you expect the file to contain after the program finishes?

You may also have to either reposition the stream pointer or flush the output stream when switching between read/write and write/read.
Last edited on
What do you expect the file to contain after the program finishes?

Sorting contents of file based names.
What is the condition of the "inputFile" when you're trying to write to it?

For example:
before sort file is:

name number
D 1
B 4
Z 6

After sort file be:
name number
B 4
D 1
Z 6

For example:
before sort file is:

name number
D 1
B 4
Z 6

After sort file be:
name number
B 4
D 1
Z 6


One problem is that your "sort condition" function is probably using the incorrect comparison operator.

Also you didn't answer my question second question. What is the state of the stream when you try to write to the stream? Remember when you read the file you stopped when eof() was encountered. Normally if a stream is in an error state you must first clear the error, then reposition the stream to the desired position.

Reading and writing to the same stream with an fstream can be fairly tricky. IMO it would be much easier to use two different streams, one ifstream, one ofstream.

@jib thank you for your answer,
I could fix my code with your help.
I didn't answer your second question because I don't answer. (I don't understand your question well)

Fixed code with two different streams but how can I fix it with one stream? (fstream)

Thanks
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
#include<iostream>
#include<fstream>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;

struct Person
{
    int number;
    string name;
};
bool compareByLength(const Person& a, const Person& b)
{
    return  a.name < b.name;
}
int main()
{
    int number;
    string name;
    vector<Person> Persons;
    Person p;
    ifstream inputFile;
    inputFile.open("myfile.txt");
    if (!inputFile)
    {
        cout << "Sorry!, cannot open this file\n";
    }
    else
    {
        while (!inputFile.eof())
        {
            inputFile >> name >> number;
            p.name = name;
            p.number = number;
            Persons.push_back(p);
        }
    }
    sort(Persons.begin(), Persons.end(), compareByLength);

    ofstream oFile("myfile.txt");
    int i = 0;
    while (i < Persons.size())
    {
        oFile<< Persons[i].name <<"\t" << Persons[i].number<<endl;
            i++;
    }

    inputFile.close();


    return 0;
}

Fixed code with two different streams but how can I fix it with one stream? (fstream)

The problem is that you read the stream till eof(), causing a stream error flag to be set. Once a stream enters an error state you must clear the error prior to trying to preform any other read or write operation. And you will probably want to reset the file pointer to the start of the file if you intend to overwrite the file contents.

@jib thank you for your answer,

How can I do it?
What have you tried?

Do you know how to clear a stream error flag?

Do you know how to reposition a stream?

By the way do you realize that "ofstream oFile("myfile.txt");" opens the file and then erases the file contents?
Topic archived. No new replies allowed.