0

I want to store my 2D matrix file into the while (fgets), but somehow it didn't store the value correctly. can someone help me check the while(fget) for the matrix is correct please. I'm a newbie with C. Thank you so much.

I've using 2D array to store in Matrix1 and matrix2, but i want read the file by using while (fgets) and store the value into it.

  char buff[50000];
    char *token;
    // Get the input files
    FILE* inputMatrix1 = fopen(argv[1], "r");
    FILE* inputMatrix2 = fopen(argv[4], "r");

    char* p1;
    char* p2;
    
    // Get matrix 1's dims
    int n_row1 = strtol(argv[2], &p1, 10);
    int n_col1 = strtol(argv[3], &p2, 10);

    // Get matrix 2's dims
    int n_row2 = strtol(argv[5], &p1, 10);
    int n_col2 = strtol(argv[6], &p2, 10);

   

    int* matrix1 = malloc((n_row1 * n_col1) *sizeof(int));
    int* matrix2 = malloc((n_col2 * n_col2) *sizeof(int));
    int* result = malloc ((n_row1 * n_col1)* sizeof(int));

    // TODO: Parse the input csv files and fill in the input matrices
while(fgets(buff,50000,inputMatrix1) != NULL){
           
        if (strstr(buff, ",") != NULL){    
            token = strtok(buff, ",");
            int column ; 
            int row;
            for(row = 0; row < n_row1; ++row){
                for (column = 0; column < n_col1; ++column){
                    while (token != NULL){   //If there still tokens               
                        matrix1[row * n_col1 +column] = atoi(token);
                        token = strtok(NULL, ",");
                        column++;
                    }
                    
                }row++;
            }
        }
        
}
Phat Phat
  • 1
  • 1
  • what did you see when you stepped through with your debugger – pm100 Feb 19 '22 at 19:47
  • 2
    " but somehow it didn't print." there is no code in here to print anything. – pm100 Feb 19 '22 at 19:49
  • I don't know if i can give full of my code in here or not, so i just give a part of the code problem. I'm sorry my question is my while fget is storing correctly? – Phat Phat Feb 19 '22 at 20:01
  • Have you tried running your code line by line in a debugger while monitoring the values of all variables, in order to determine at which point your program stops behaving as intended? If you did not try this, then you may want to read this: [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/12149471) You may also want to read this: [How to debug small programs?](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). – Andreas Wenzel Feb 19 '22 at 23:51
  • Even if using a debugger does not actually solve the problem, it should at least help you to isolate the problem and to create a [mre] of the problem, so that it will be easier for other people to help you. – Andreas Wenzel Feb 19 '22 at 23:52
  • You say 'is my while fgets storing', fgets doesnt store anything , it reads things from a file into memory. Its really not clear what you are asking – pm100 Feb 20 '22 at 01:14

0 Answers0