Sorting programm.

Hey! I have no code to put here, because I don't really know where to start.. :(

But my problem is this - I need to make a programm, that gets a file with some lines of text (There are topics with IDs in frot of them, and then each line starts with a topics ID followed by one word), and I need the programm to sort the text and put it in a different file like this - to topic wothout the "" and in the next like all the words, that had the topics ID in front.

Should I use a sided-list? And if so, how can I assing the according line to the node??

Can someone PLEASE give me some advice on, how to achieve this? I know it's not good that I have no code, bet I'm a begginer, so...yeah :/

Split your problem up first into manageable chunks.

First you need to open the file and read it.
See here:
http://www.cplusplus.com/doc/tutorial/files/

specifically the section "Text files".

get that working first. Don't even bother worrying about parsing, storing or sorting yet.
Last edited on
Yeah, I actually was reading that before. I made somethng like this. This actually does something - it copies the first files text into the second file. That I got, but all the next is a mystery to me...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <fstream>

using namespace std;


int main(){
ifstream fin("text.in");
ofstream fout("text.out");
 string line;

 while (getline(fin, line, '\n'))
    {
            fout<<line<<endl;
 }


fout.close();
 fin.close();
 return 0;
}
You're complicating things slightly mate. Like I said don't worry about everything at the same time. worry about writing to another file later.

get the reading working first.

The example I referred to you was:
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
// reading a text file
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main () {
  string line;
  ifstream myfile ("example.txt");
  if (myfile.is_open())
  {
    while ( getline (myfile,line) )
    {
      cout << line << '\n';
     
      // You will have to do extra stuff here, but for now just get 
      // each line printing to your screen.
    }
    myfile.close();
  }

  else cout << "Unable to open file"; 

  return 0;
}


Once you're printing lines to the screen from your file, make sure you understand all the functions in the example as well.
Last edited on
It's printing and the functions in the example - I understand
cool. can you give us a real-world example of what one line would look like?

and are you allowed to use vectors?
Last edited on
Topic archived. No new replies allowed.