Redesigning a string & integer textfile


Hello to everyone,
I am new here and new to programming with c++.

My knowledge is surely at beginner level.

I have a textfile which contains strings and integer numbers.

My problem is that the text file has its values for example like this:

Time
xAxe
yAxe

0.8
15.2
4.2


I was thinking of creating a routine that would do this automatically for me to rearrange the positions and bringing the one bottom of the appropriate string.

For example,

Time    xAxe    yAxe
0.8     15.2    4.2


So, I need some help due to the fact that I am complete beginner in c++ and do not know how to pull such thing.

I was thinking, of opening the file, and telling the cursor to move to a certain point, but how to copy the string and pasting to a new position?

Heeeelp please!
Thanks in advance!
Last edited on
You can divide your program in these steps:

- Create a vector to store the strings and another to store the integers.
- Read the strings to the first vector and ints to the other.
- Clean the file.
- Write the elements of the vectors the way you need.

Messing with positions on the file is almost never a good solution (not in this case, for sure).
when you mean vector, you mean an array ??

store the elements in an array ?

I was looking for the best possible solution according to my knowledge in C++ programming.

At this moment, up until know, the only kind of code i have created is by using a console,
to ask questions and storing the answers in a file.

I think that gives the best image of what kind of knowledge I am familiar with.

So, when I was looking for the best possible scenario for my to play with, I was looking in the textfile
that e.g Time was at column 29 and row 4, so I thought that it would be easier to create a loop that
would one at a time, go to each position, copy 4-5-6 lengthy positions and pasting it to a new file at the X and Y of my choice.

what i found out searching, were these :

http://www.cplusplus.com/reference/string/string/copy/

and the gotoxy function:

1
2
3
4
5
6
7
void gotoxy(int x, int y)
{
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_H ANDLE), coord);
}



yet, I cannot get the string copy to work.
I am not sure if it can copy strings from a file input.
Last edited on
That gotoxy() function is for positioning the cursor on the console -- it won't help you when editing files. When messing with files, it is best to load the file, then write the file. The way you load it and write it is what makes the difference.

Instead of using simple arrays, you should use a vector or a deque... which is much easier and safer than using an array directly. The tutorials on this site will run you through all the basics.

To load your file, you'll probably want to do something like 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
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main()
  {
  vector <string> names;
  vector <string> values;

  while (cin)
    {
    // Post edit: empty the 'names' and 'values' vectors
    names.clear();
    values.clear();

    // Get rid of empty lines
    cin >> ws;
    string name;
    // Read all lines until an empty line
    while (getline( cin, name ) && !name.empty())
      names.push_back( name );

    // Get rid of any additional empty lines (besides the one we just read to stop the loop above)
    cin >> ws;
    string value;
    while (getline( cin, value ) && !value.empty())
      values.push_back( name );

    if (cin.fail()) break;  // Stop here if there was an error reading from file

    if (names.size() != values.size())
      {
      // We'll just fail miserably here if you don't have the same number of names and values.
      // (You can be smarter about it than this, if you want).
      cerr << "Hey, there are not equal amounts of names and values!\n";
      return 1;
      }

    // Now we'll write them out the way you want to see them
    // (First the names on the top line)
    for (size_t n = 0; n < values.size(); n++)
      {
      cout << setw( max( names[ n ].length(), values[ n ].length() ) + 4 ) << left << names[ n ];
      }
    cout << endl;
    // (Then the values on the bottom line)
    for (size_t n = 0; n < values.size(); n++)
      {
      cout << setw( max( names[ n ].length(), values[ n ].length() ) + 4 ) << left << values[ n ];
      }
    cout << endl << endl;
    }

  return 0;
  }

Since this is in a big loop, it will go through the entire file. Hence:

Time
xAxe
yAxe

0.8
15.2
4.2

One
Two
Three
Four

(1, 1)
(10, -3)
(15, 12)
(0, 5)

Becomes:
Time    xAxe    yAxe
0.8     15.2    4.2

One       Two         Three       Four
(1, 1)    (10, -3)    (15, 12)    (0, 5)

Hope this helps.

P.S. If you really are Mr T, it is an honor to help you with this!

Last edited on
Duoas thank you very much!
Mr T I chose just for a nickname...nothing special, but it is my honor for you to help me.

Somehow i guess i will have to open the usual way the file in the beginning with?
ifstream myfile ("data.txt", ios::in);


I got to try what you proposed.

The file is large and it has almost same kind of characteristics everywhere,

eg.

as i mentioned, it has for example,

Strings and numerical data beyond them in the same way described,

the problem is that it has many columns like that
and in between many times it has
line of string with a ":" an a numerical data afterwards.

This is so complicated...
Last edited on
It occurred to me yesterday that I forgot to "clear" the "names" and "values" vectors each time through the loop. I added the fix to the code above. I hope that didn't confuse you too much.
thanx Duoas, I'll check it out in the weekend as soon as I get some free time.
Thanx!
Topic archived. No new replies allowed.