Salviati
Salviati

Reputation: 778

Reading a big file and place the data in correct array without being too verbal

So what I am trying to do is that I have two files with a 64x64 array separated by spaces and new lines. What I want to do is to read the files and place it in a struct containing two 64x64 arrays. The files will look something like this:

2 5 1 6 2 ... 6 
3 2 9 5 1 ... 8 
. 
. 
2 4 1 5 2 ... 5

And this is how I thought I would do it

int
getFromFile(char *fileNameMatrixA, char *filenameMatrixB, struct Matrises *matrix)
{
  FILE *fileA, *fileB;
  char buffer[BUFFER_LEN];
  int counter = 0;

  if((fileA = fopen(fileNameMatrixA, "r")) == NULL)
    {
      return 1;
    }

  if((fileB = fopen(fileNameMatrixB, "r")) == NULL)
    {
      fclose(fileA);
      return 2;
    }

  while(fgets(buffer, sizeof(buffer), fileA) != NULL)
    {
      if(sscanf(buffer, "%d\s%d\s%d\s%d\s%d\s%d\s%d\s%d\s%d\s%d\s%d\s%d\s%d\s%d\s%d\s%d\s%d\s%d\s%d\s%d\s%d\s%d\s%d\s%d\s%d\s%d\s%d\s%d\s%d\s%d\s%d\s%d\s%d\s%d\s%d\s%d\s%d\s%d\s%d\s%d\s%d\s%d\s%d\s%d\s%d\s%d\s%d\s%d\s%d\s%d\s%d\s%d\s%d\s%d\s%d\s%d\s%d\s%d\s%d\s%d\s%d\s%d\s%d\s%d", matrix->matrixA[counter][0], matrix->matrixA[counter][1], matrix->matrixA[counter][2], matrix->matrixA[counter][3], ... , matrix->matrixA[counter][63]) != 64)
    {
      fclose(fileA);
      fclose(fileB);
      return 3;
    }
      counter++;
    }

  counter = 0;

  while(fgets(buffer, sizeof(buffer), fileB) != NULL)
    {
      if(sscanf(buffer, "%d\s%d\s%d\s%d\s%d\s%d\s%d\s%d\s%d\s%d\s%d\s%d\s%d\s%d\s%d\s%d\s%d\s%d\s%d\s%d\s%d\s%d\s%d\s%d\s%d\s%d\s%d\s%d\s%d\s%d\s%d\s%d\s%d\s%d\s%d\s%d\s%d\s%d\s%d\s%d\s%d\s%d\s%d\s%d\s%d\s%d\s%d\s%d\s%d\s%d\s%d\s%d\s%d\s%d\s%d\s%d\s%d\s%d\s%d\s%d\s%d\s%d\s%d\s%d", matrix->matrixB[counter][0], matrix->matrixB[counter][1], matrix->matrixB[counter][2], matrix->matrixB[counter][3], ... , matrix->matrixB[counter][63]) != 64)
    {
      fclose(fileA);
      fclose(fileB);
      return 4;
    }
      counter++;
    }

  fclose(fileA);
  fclose(fileB);
  return 0;
}

I think all of you see the problem.. This is in no way a good way of doing it. But I do not know of any other way do do it without being so verbal.

Is there a way of doing this more efficient and clean?

Upvotes: 0

Views: 38

Answers (1)

4386427
4386427

Reputation: 44274

You can use a loop and %n to scan one number in each loop. The purpose of %n is to get the number of characters used by the scan. In this way you can iterate the string number by number.

Here is an example:

int main(int argc, char *argv[])
{
    char buf[20] = "1 2 3 4";  // Assume this is the data read using fgets
    int x;
    int offset = 0;
    int temp;
    while (sscanf(buf + offset, "%d%n", &x, &temp) == 1)
    {
        printf("At offset %d scanned %d\n", offset, x);
        offset += temp;
    }
}

Output:

At offset 0 scanned 1
At offset 1 scanned 2
At offset 3 scanned 3
At offset 5 scanned 4

Adding a simple counter that you increment in every loop will allow you to store the numbers in an array.

Upvotes: 2

Related Questions