0

I want to scan elements from a txt into an array. The txt doesn't have how many rows or columns I'm going to have, it only contains a coordinate, and the elements of the array. It looks like this:

2,3
2,1
3,0
-

How can i put these numbers into an array so that array[0][0] will be 2 and array[1][0] will be 3 etc...

I want to make this work with other inputs as well.

My code so far :

The ?? is there because I have no idea how I should declare these if I don't even know how many rows or columns every input txt will have.

#include <stdio.h>
#include <stdlib.h>

int main()
{
FILE* in = fopen("in.txt", "r");

int x, y;

int array[??][??];

if (in == NULL) {
    printf("Can't open in.txt");
    fclose(in);
    return 1;
}

if (fscanf(in, "%d,%d\n", &x, &y) != 2) {
    printf("Cant read file.");
    return 2;
}

for (int i = 0; i < ??; i++) {
    for (int j = 0; j < ??; j++)
    fscanf(in, "%d", &array[i][j]);
}

return 0;
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Stan Marsh
  • 31
  • 1
  • 8
  • You say "it only contains a cordinate, and the elements" but I don't see the coordinate for `array[0][0]` in the example input. – Weather Vane Nov 22 '18 at 19:02
  • 1
    Wouldn't it be `array[0][1]` that should be `3`, and `array[1][0]` be the next `2`? That would make more sense to me. – Some programmer dude Nov 22 '18 at 19:06
  • The question is not clear to me; why do you read `x` and `y`? Are they actually the array bounds? But earlier you say you don't know the array bounds. It would improve the question to try and describe this better, as well as provide a realistic example of the file you are trying to read. For the file you provided you just need an array of fixed width 2 , since each row has 2 elements – M.M Nov 22 '18 at 21:14

2 Answers2

0

You want to read in a list of value pairs? That sounds like you will need to have a (possibly long) array of sets of two numbers. Rather than remembering that X is the first and Y is the second, may I suggest setting up a structure to hold the values. Something like this should work:

int main()
{
    FILE* in = fopen("lis.csv", "r");
    int count=0;
    int error=0;
    int x, y;
    typedef struct {
        int x;
        int y;
    } COORD;
    COORD array[999]={0};
    if (in == NULL) {
        printf("Can't open in.txt");
        fclose(in);
        return 1;
    }
    while(!feof(in))
    {
        if (fscanf(in, "%d,%d\n", &x, &y) != 2) {
            printf("Cant read file.");
            error=1;
            break;
        }
        array[count].x=x;
        array[count].y=y;
        count++;
    }
    return error;
}

I did not add anything bright for the error condition and it helps if you do something with the values after reading them in but you get the idea.

user1683793
  • 1,213
  • 13
  • 18
  • 1
    Please see [Why is `while ( !feof (file) )` always wrong?](http://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) – Weather Vane Nov 22 '18 at 19:45
  • I was trying to give a hand with the fscanf and where to store the data. If it were me, I would have written it with `while(fgets(linein,sizeof(linein), in))` It just happens that in this case, with GCC on Linux (and cygwin), the EOF test occurs after the last fscanf so this works fine. If the fscanf did not detect the EOF, the test of fscanf and break will avoid storing polluted data. – user1683793 Nov 22 '18 at 20:06
0

You should use dynamic array allocation to scan elements from an unknown txt file into an array. for C++ programmers the best solution is std::vector. but C programmers should use alternative solutions. please read this post: (std::vector alternative for C)