Arranging Numbers like in a tabular and print them to a file

Dec 22, 2011 at 2:31pm
Hey folks,

Basically my question is: "How can I write next to something in a file"
For example in the target file stands:
30
23
45
.
.
.
how can I then write something like
30 40
23
45
...


My aim is to do something like this:

Source:

30 23
30 45
30 71
30 12
30 84
30 12
30 96
30 26
30 20
40 54
40 23
40 67
40 89
40 27
40 59
40 32
40 56

Afterwards in other file:
30 40
23 54
45 23
71 67
12 89
84 27
12 59
96 32
26 56
20

Till now I managed to read a string, and sort out the two numbers. For example the first line up above would lead to: indicator_str = 30 and a temp_str = 23.
To make things easier I now would like to print the indicator_str once and after that the temp_str underneath it, then read the next line from the file (leading to indicator_str = 30 and temp_str = 45), realize that it's the same indicator and therefore just print the temp_str in the next line.

So far so good. My problem for that is that if indicator_str changes (for example to 40) I would need to write next to something existing in the target file and not underneath. Would I need to read from the target before and print the entire new string or is there a special function in c++ that could manage that problem?

Thanks very much, I appreciate it.
Dec 22, 2011 at 2:41pm
You can use fseek but it is likely to be cumbersome. Best read the entire file first, then arrange your data and then write in one shot the result.
Dec 22, 2011 at 2:45pm
that's exactly what I'm trying not to do. It was supposed to be a tiny helper ... Now it's a huge load of programing and brainpain :P.

I tried something like this with fseek in C (I'm writing c++ right now) but that didn't worked at all. For some weird reason it didn't even delete the old line. It just added the line to the end of the file.
Dec 22, 2011 at 3:06pm
fseek and co work fine, maybe you messed up the parameters(and it is easy to mess up) but the probleme is that it does not insert the data in the file but replace it, that's why it's difficult for your problem. I am pretty sur there is no way to insert data into file, that's why i think the easyest solution is to proceed as i said before.
Dec 22, 2011 at 3:25pm
I wouldn't blame the fseek function that it didn't work. How you said. Pretty sure it was me :P
I just figured out that the problem disappeared since there never will be the source file ... just waisted time and server space again :(
Topic archived. No new replies allowed.