1

I have a file.txt like:

4,3
3,2
5,1
6,5

Every line is a coordinate. For the first line, x1=4, y1 = 3. Then for the second line, x2=3, y2=2, and so on. The purpose of this method is to get the 4 coordinates inside the file and store them inside a variables or an array. I thought the best way was to create an array for x and another one for y, so we would have coordX[] and coordY[]. Since the file I must read will always contain only 4 coordinates, both arrays will have a size of [3]. The code I'm using right now is this:

void readFile(){
   int x1, y1;
   int coordX[3], coordY[3];
   while (infile >> x1 >> y1){
       //How to insert the coordinates into coordX[] and coordY[]?
   }
}

As you can see, I don't know how to make the method insert the values of each coordinate into the array, is there a way to do it using an array? Or should I create int variables x1, y1, x2, y2, and so on? Thanks in advance!

jww
  • 97,681
  • 90
  • 411
  • 885
asantiagot
  • 153
  • 2
  • 12

2 Answers2

2

If the number of coordinates is fixed at four, you could unroll the loop by repeating the read four times, like this:

void readFile(){
   int coordX[4], coordY[4];
   infile >> coordX[0] >> coordY[0];
   infile >> coordX[1] >> coordY[1];
   infile >> coordX[2] >> coordY[2];
   infile >> coordX[3] >> coordY[3];
   ...
}

You could do it with a loop, too:

void readFile(){
   int coordX[4], coordY[4];
   for (int i = 0 ; i != 4 ; i++) {
       infile >> coordX[i] >> coordY[i];
   }
   ...
}

Note that since you are reading four items, coordX and coordY must be declared with four, not three, elements. The general rule is that when you declare an array of N items, the valid indexes are from zero, inclusive, to N-1, inclusive.

Also note that coordX and coordY must be either used inside the same function, or copied to a container or a dynamically allocated array in order to be returned from the readFile function.

Finally, if the number of items to read is not fixed, you should change the code to use a vector or a list instead of an array, to hold a variable number of results.

I was just wondering, how do I insert the comma as a token separator?

You can add a peek-and-ignore code similar to what is described in this answer:

void readFile(){
   int coordX[4], coordY[4];
   for (int i = 0 ; i != 4 ; i++) {
       infile >> coordX[i];
       if (infile.peek() == ',') {
           // If the next character is a comma, skip it
           infile.ignore();
       }
       infile >> coordY[i];
   }
   ...
}
Community
  • 1
  • 1
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • Thanks for your solution!! I was just wondering, how do I insert the comma as a token separator? Cause the code works only if the coordinates inside the file are not separated by a comma, let's say: `4 2` But it won't work if there are commas between the values: `4, 3` `4, 2` Is there a way to do this? Thanks! :) – asantiagot Sep 15 '14 at 18:24
  • Thank you so much!!! Your solution is soooo much simple than those posts about reading CSV, thanks!!!!!!!!!!!!! – asantiagot Sep 15 '14 at 18:48
0

With minimal mods to yr original code

void readFile(){
   int x, y;
   int i = 0;
   int coordX[4], coordY[4];
   while (infile >> x >> y){
       //How to insert the coordinates into coordX[] and coordY[]?
       coordx[i] = x;
       coordy[i] = y;
       i++;
   }
}

of course this code is totally wrong. If the file contains 5 lines it will break, if it contains 3 lines you wont know that the last line was missing. You should be using std::vector,...

pm100
  • 48,078
  • 23
  • 82
  • 145