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
scanf
format string which reflects the format of the scanned line.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]);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.