0

I am quite new to C programming and I have been set the task of reading a .csv file containing a 2d integer array, I need to then print it so it shows up in the terminal. The array is a 4 row by 2 col integer array, but when I use this code I get a random list of numbers. I have attached the code I used and the .csv file array and how it should look like.

#include <stdlib.h> #include <stdio.h> int main() { FILE *in_data = fopen("in.csv", "r"); int i; int j; int trIn[4][2]; if(in_data == NULL) { printf("error\n"); return 1; } for(i = 0; i < 4; i++){ for(j = 0; j < 2; j++){ char junk; if (j != 0) fgetc(in_data); fscanf(in_data, "%c%d%c", &junk, &trIn[i][j], &junk); printf("%d ", trIn[i][j]); } fgetc(in_data); printf("\n"); } return 0; } 

.csv file array:

0 0 0 1 1 0 1 1 

.csv file (raw):

"0","0" "0","1" "1","0" "1","1" 
3
  • can you give the raw .csv file data? Commented Oct 29, 2020 at 22:38
  • Mind that fgetc parses one character at a time, including spaces and newline characters. Commented Oct 29, 2020 at 22:41
  • thanks, last question: Are these numbers always a single character? Commented Oct 29, 2020 at 22:42

2 Answers 2

1

You can create a pattern which discards the unwanted characters, like so:

Live demo

for (i = 0; i < 4; i++) { for (j = 0; j < 2; j++) { //printf("%s", str); //print str to check if there are any weird chars fscanf(in_data, " \"%d\",", &trIn[i][j]); printf("%d ", trIn[i][j]); } printf("\n"); } 

Chek if you have spaces in the lines after the values, that can throw fscanf off. Although a space before the specifier, like in the sample, should solve that.

Also, you can export .csv files without the quotation marks, if that's something you can control.

You can also try a more robust method using a combo of fgets and sscanf:

Live demo

char str[20]; int i = 0; int j = 0; //... while (i < 4 && fgets(str, sizeof str, in_data)) { //printf("%s", str); //print str to check if there are any weird chars if(sscanf(str, "\"%d\",\"%d\"", &trIn[i][j], &trIn[i][j+1]) == 2) printf("%d %d\n", trIn[i][j], trIn[i][j+1]); i++; } 
Sign up to request clarification or add additional context in comments.

3 Comments

@xmxn I added another method, if ithis doesn't work, there is something seriously wrong with your file. Perhaps nor printable characters being parsed.
fgets() followed by sscanf() (or using a pair of pointers and strtol()) is always the way to go.
@DavidC.Rankin, for sure.
0

You can replace you current loop with this one:

for(i = 0; i < 4; i++){ for(j = 0; j < 2; j++){ char junk; if (j != 0) fgetc(in_data); fscanf(in_data, "%c%d%c", &junk, &trIn[i][j], &junk); printf("%d ", trIn[i][j]); } fgetc(in_data); printf("\n"); } 

This works because fscanf(in_data, "%d", &trIn[i][j]) reads an int (%d) from the file in_data into the memory location of trIn[i][j]. fgetc didn't work because it only reads a single character which was then printed as if it were an integer.

EDIT: Now, for each value in the .csv file, you read to a junk variable the " and \n characters.

Line by line:

if (j != 0) fgetc(in_data); reads the , from the csv file into a junk variable (which isn't the first character on a line).

fscanf(in_data, "%c%d%c", &junk, &trIn[i][j], &junk); reads:

  • 1st: the opening " character into a junk variable
  • 2nd: the number (int: %d) into to memory location of trIn[i][j].
  • 3rd: the closing " character into a junk variable

1 Comment

Comments are not for extended discussion; this conversation has been moved to chat.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.