0

I am trying to read the ints from a CSV file into a 2D-Array.

Here is my code...

FILE* fp = fopen(argv[1], "r");
int counter = 0;
char line[50];
while (fgets(line, 50, fp)) {
    counter++;
}
int arry[counter - 1][4];
NUM_ROWS = counter -1;
counter = 0;

//Iterate File Again to Populate 2D Array of PID, Arrival Time, Burst Time, Priority
//File in Format: #,#,#,# 
rewind(fp);
//Skip First Line of Var Names
fgets(line, 50, fp);
while(fgets(line, 50, fp)) {
    sscanf(line, "%d%d%d%d", &arry[counter][0], &arry[counter][1], &arry[counter][2], &arry[counter][3]);
    counter++;
}

However, sscanf() is not reading the line into the array. I am unsure why this is not working

Edit: Here is a picture of the file.

wirly
  • 27
  • 6
  • 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? In particular, did you inspect the contents of the `line` array before the `sscanf` function call? If you did not try this, then you probably 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 Jun 15 '21 at 00:56
  • 1
    The "C" in "CSV" is "comma". Where do you handle the commas in your code? – kaylum Jun 15 '21 at 00:56
  • 1
    Did you check the return value of the `sscanf` function call? – Andreas Wenzel Jun 15 '21 at 00:59
  • 1
    The format string should be `%d,%d,%d,%d` to skip over the commas. – Barmar Jun 15 '21 at 01:00
  • @AndreasWenzel Thanks for your reply. I am very confused now. I checked the value of line in gdb, and it is reading as... ""\"3,\t1,\t3,\t2\"\r\n\000val Time\tBurst Time\tPriority \"\r\n\000\000\034". However, printing the line right before is presenting it as "3, 1, 3, 2" – wirly Jun 15 '21 at 01:43
  • gdb is showing the entire array contents. The last line that `fgets` read ends at the first `\r\n\0`. And apparently the lines that you are reading contain quote characters. You should [edit] the question and post the first three lines of the file so we can see what's actually in the file. – user3386109 Jun 15 '21 at 03:13
  • @user3386109 Hello, I have edited the question and included a picture of the CSV file. – wirly Jun 15 '21 at 04:11
  • If possible, please post a [mre] of the problem (including a function `main` and all `#include` directives), after verifying that this example actually reproduces the problem. Instead of posting a picture of a graphical representation of the .CSV file, it would be better if you posted the contents of the .CSV file as text into the question. To obtain the actual text of the .CSV file, on the Windows platform, you can open the file in `notepad.exe` and copy&paste from there. – Andreas Wenzel Jun 15 '21 at 10:42

1 Answers1

1
  1. You need to have the scanf format string which reflects the format of the scanned line.
  2. Always check the result of scanf

Example:

int main(void)
{
    char line[] = "34543,78765,34566,35456";
    int arry[1][4];
    int counter = 0;

    int result = sscanf(line, "%d,%d,%d,%d", &arry[counter][0], &arry[counter][1], &arry[counter][2], &arry[counter][3]);
    
    printf("result = %d\n", result);
    printf("CSV line = `%s`\n", line);
    printf("data read: %d, %d, %d, %d\n", arry[counter][0], arry[counter][1], arry[counter][2], arry[counter][3]);
}  

https://godbolt.org/z/zs5Y8ff8h

0___________
  • 60,014
  • 4
  • 34
  • 74