How can I reposition the pointer of a file to a new line?

Hello!

I have a file with the following content:
1
2
ASDASDIOIOIOIOIPPOPO
1 13 4 3 2 4 3 5 2 1


More exactely: on the first line I have a string and on the second line I have 10 numbers separated by a space. I want to read the content of that file, using the functions provided by <cstdio> header (as you may think, I use FILE pointers).

So, I managed to read the string (I stored it into an array of chars) on the first line, using the fgets function.
Now, I want to read those ten numbers and to store them into an array of integers.

I thought that it might be a simple task, but it isn't.

I started doing that normally:
1
2
3
4
5
6
char x[102];  int vector[100];
fgets(x, 100, f);
for(int i=1; i<=10; i++)
{   
    fscanf(f, "%d", &vector[i])
}


It doesn't work.

I tried then to set the file-pointer to the end of the file, and then to move it backward with 10 positions, and then to read those numbers:

1
2
3
4
5
6
fseek(f, -10, SEEK_END);
      for(int i=1; i<=10; i++)
      {
          fscanf(f, "%d", &vector[i]);

      }


It works, but only with numbers with one digit. If I put one or more numbers with two digits, I got a zero on the last position in vector for every digit that I add.

I really don't find another way to read this.

Oh, I also thought about reading all the contens of the file and store it in an array of chars and then to transform the last 10 elements (the numbers I need) into integers but the atoi function provided by the cstdio header it's weird. There are evaluators that don't recognize it, I don't know why. I made a few programs and I used that function. It worked fine on GCC but when I evaluate it, it didn't compile because of that function.

Any ideas?

Thank you respectfully.
for(int i=1; i<=10; i++)

Array indexing starts from 0.
Topic archived. No new replies allowed.