1
#include <stdio.h>
#include <stdlib.h>

int main() {
    int c1[100];
    char c2[150];
    char c3[100];
    float c4[100];
    float c5[100];
    float c6[100];
    float c7[100];
    char c8[100];
    char c9[100];
    float c10[100];
    char string[10][100];
    int i, j;
    char c;

    FILE *fp1;
    fp1 = fopen("sample.csv", "r");
    while (1) {
        c = fgetc(fp1);
        if (c == EOF)
            break;
        else
            printf("%c", c);
    }

    for (i = 1; i <= 10; i++) {
        fscanf(fp1, "%d,%[^,],%[^,],%[^,],%[^,],%d,%d",
               &c1[i], &c2[i], &c3[i], &c4[i], &c5[i],
               &c6[i], &c7[i], &c8[i], &c9[i], &c10[i]);
    }

    for (j = 0; j <= 10; j++) {
        printf("\n");
        printf("%d", c3); //Here i am trying to read the column3 values but getting random integer values.
        //This problem continues to every column
    }
    return 0;
}

I have to read file sample.csv and store values into the array so that I can perform operation on that values. I am not getting the exact value from the csv file that I have read. I am getting some random integer value on running the program.

1
  • 1
    There are multiple issues with the program. You don't check if fopen was successful or not, c should be an int as fgetc returns an int, array indices in C begin from 0 not 1, you use fscanf after you scanned all the characters in the file, c3 is an array of char and yet you use %d as if it was an int etc. I suggest looking up on a good C tutorial. Commented Aug 25, 2018 at 6:28

2 Answers 2

1

There are many problems in your code:

  • you do not check if fopen() succeeded.
  • c must be defined as int for proper end of file testing
  • you must rewind the file with rewind(fp1); or fseek(fp1, 0L, SEEK_SET); before reparsing the contents with fscanf()
  • the loop index i must start at 0 instead of 1, because arrays are 0 based.
  • it is idiomatic in C to use for (i = 0; i < 10; i++) ... to handle 10 lines of input. i <= 10 would iterate 11 times.
  • you must check the return value of fscanf() to ensure the input stream has the expected format. The format string does not handle empty text fields.
  • the fscanf() format string is incompatible with the arguments provided
  • the printf format string "%d\n" in incompatible with the type of the argument: the argument is the array c3 which is passed as a pointer to its first member, not an int as expected.
Sign up to request clarification or add additional context in comments.

Comments

0

Simply read a line in a loop until there are no more lines

#include <stdio.h>
#include <string.h>
#define MAX_ITEMS 10000
#define LARGEST_LINE 1000
#define LARGEST_ELEMENT 100

int main(void) {
    int c1[MAX_ITEMS];
    char c2[MAX_ITEMS][LARGEST_ELEMENT+1]; // large enough for each `c2`
    char c3[MAX_ITEMS][LARGEST_ELEMENT+1];
    char c4[MAX_ITEMS][LARGEST_ELEMENT+1];
    char c5[MAX_ITEMS][LARGEST_ELEMENT+1];
    int c6[MAX_ITEMS];
    int c7[MAX_ITEMS];

    int tmpc1;
    char tmpc2[LARGEST_ELEMENT+1];
    char tmpc3[LARGEST_ELEMENT+1];
    char tmpc4[LARGEST_ELEMENT+1];
    char tmpc5[LARGEST_ELEMENT+1];
    int tmpc6;
    int tmpc7;

    int lineno = 0;
    char buf[LARGEST_LINE]; // large enough for the largest line
    while (fgets(buf, sizeof buf, fp1)) {
        ++lineno;
        // no error, process line
        if (sscanf(buf, "%d,"
                        "%" LARGEST_ELEMENT "[^,],"
                        "%" LARGEST_ELEMENT "[^,],"
                        "%" LARGEST_ELEMENT "[^,],"
                        "%" LARGEST_ELEMENT "[^,],"
                        "%d,%d",
              &tmpd1, tmpc2, tmpc3, tmpc4, tmpc5, &tmpd6, &tmpd7) == 7) {
            // line ok, copy tmp variables and update indexes
            c1[i] = tmpd1;
            strcpy(c2[i], tmpc2);
            strcpy(c3[i], tmpc3);
            strcpy(c4[i], tmpc4);
            strcpy(c5[i], tmpc5);
            c6[i] = tmpd6;
            c7[i] = tmpd7;
            i++;
        } else {
            // line with error, you may want to report to the user
            fprintf(stderr, "line %d with error.\n", lineno);
        }
    }
    // read "error", probably EOF; close file and report
    fclose(fp1);
    for (int j = 0; j < i; j++) {
        printf("item #%d: %d, %s-%s-%s-%s, %d %d\n",
              c1[j], c2[j], c3[j], c4[j], c5[j], c6[j], c7[j]);
    }
return 0;
}

Also consider putting all those c arrays inside a struct and make 1 single array of that structure.

1 Comment

This causes compilation errors: "%" LARGEST_ELEMENT "[^,]," You should either use a macro to stringize LARGEST_ELEMENT or use a different macro S_LARGEST_ELEMENT defined as #define S_LARGEST_ELEMENT "100"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.