Home > Enterprise >  Values in a csv file not reading into a two dimension array properly in c
Values in a csv file not reading into a two dimension array properly in c

Time:03-23

I have a text file that contains numbers like below

23,10,5,7,8,9,10,1,2,5
23,10,5,7,8,9,10,1,2,5
23,10,5,7,8,9,10,1,2,5
23,10,5,7,8,9,10,1,2,5
23,10,5,7,8,9,10,1,2,5
23,10,5,7,8,9,10,1,2,5
23,10,5,7,8,9,10,1,2,5
23,10,5,7,8,9,10,1,2,5
23,10,5,7,8,9,10,1,2,5
23,10,5,7,8,9,10,1,2,5

I am trying to read al those values into a two dimension array using the c programming langauge, the first line reads into the array succesfully but the rest of the values are 0s. Please help me debug where the problem is, below is the code am using

#include <cstdlib>
#include <stdio.h>
#include <iostream>
#include <string.h>



using namespace std;

int main() {
    //create a new file pointer
    FILE* myfile = fopen("matrixA.csv", "r");
    //define the dimensions of our multidimensional array
    const int ROWS = 10;
    const int COLS = 10;
    //allocate memory to our array
    int array[ROWS][COLS];
    //intialize all members of the array to zero
    memset(array, 0, sizeof(array));
    //check if the file is null
    if (myfile == NULL) {
        printf("%s", "The file does not exist");
        //return an exit code to the operating system
        return EXIT_FAILURE;
    }
    
    
    //the file is real, read from it and enter its values into the matrix 
    //array
    for (int row = 0;row < ROWS;row  ) {
        for (int col = 0;col < COLS;col  ) {
            if (fscanf(myfile,"%d,", &array[row][col]) != 1) {
                //return an exit code to the operatig system
                printf("%s\n", "The scan of some components of this file was unsuccessful");
                return EXIT_FAILURE;
            }
        }
        //for every member of this array, print them out
        for (int i = 0;i < ROWS;i  ) {
            for (int j = 0;j < COLS;j  ) {
                printf("%d ", array[i][j]);
            }
            //break a row when a row is complete
            printf("\n");
        }

        return 0;
    }
}

The output am getting when I run my code on the terminal is like below after printing out the elements of the array which is incorect.

23 10 5 7 8 9 10 1 2 5
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0

How do I make my code read all the values of the file into the two dimension array succesfully>?

CodePudding user response:

It looks like the problem is newline character at the end of each line in the file. fscanf with %d conversion specifier can't read \n characters, so they remain in input buffer. That means fscanf never goes past first \n which appears after 5 in the first row, and the remaining numbers are never read into the array.

fscanf(myfile,"%d%*c", &array[row][col]) != 1) might work correctly.

CodePudding user response:

Somehow, you've moved output statements into reading loops. Moving them out should fix it.

    //the file is real, read from it and enter its values into the matrix
    //array
    for (int row = 0; row < ROWS; row  ) {
        for (int col = 0; col < COLS; col  ) {
            if (fscanf (myfile, "%d,", &array[row][col]) != 1) {
                //return an exit code to the operatig system
                printf ("%s\n", "The scan of some components of this file was unsuccessful");
                return EXIT_FAILURE;
            }
        }
    }
    //for every member of this array, print them out
    for (int i = 0; i < ROWS; i  ) {
        for (int j = 0; j < COLS; j  )
            printf ("%d ", array[i][j]);
        //break a row when a row is complete
        printf ("\n");
    }

    return 0;

CodePudding user response:

Firstly, please close your file. Secondly, the fscanf() does not read consecutive lines as expected. Finding more information in this: https://docs.microsoft.com/vi-VN/troubleshoot/developer/visualstudio/cpp/libraries/fscanf-does-not-read-consecutive-line

  • Related