Click here to Skip to main content
16,020,673 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

char line[100];
int studentid;
char name[30];
float gpa;

int main()
{
    FILE *fp;
    fp = fopen("Data.csv", "r");
    if (fp == NULL)
    {
        printf("not opening");
        exit(0);
    }
    char *sp;
    while (fgets(line, 100, fp) != NULL)
    {

        sp = strtok(line, ",");
        studentid = atoi(sp);

        sp = strtok(NULL, ",");
        strcpy(name, sp);

        sp = strtok(NULL, ",");
        gpa = atof(sp);

        printf("\n%d %s", studentid, name);
    }
    fclose(fp);
    return 0;
}


What I have tried:

Code is compiled properly but it's not printing anything , seems like it's a logic error.
Here is the content of my excel file :
1,Brian,4.5
2,Clem,6.7
3,Abhi,8.9
Posted
Updated 20-Apr-22 5:47am
v3
Comments
[no name] 20-Apr-22 11:15am    
Check the content of your .csv file.
xguybluehat 20-Apr-22 11:40am    
Here is the content of my excel file:
1,Brian,4.5
2,Clem,6.7
3,Abhi,8.9
[no name] 20-Apr-22 11:57am    
It works fine for me, the output is:
1 Brian
2 Clem
3 Abhi
xguybluehat 20-Apr-22 12:51pm    
oh yeah, man! it's working. Thanks.
The Other John Ingram 20-Apr-22 12:27pm    
What exactly is it not doing?

1 solution

Your code works fine with the following input data (Data.csv: I had to invent it, because you didn't provide it):
100,Foo,2.5
101,Boo,2.51
102,Goo,0.97
 
Share this answer
 
Comments
xguybluehat 20-Apr-22 11:45am    
Oh yeah ,here is the content of my excel file:
1,Brian,4.5
2,Clem,6.7
3,Abhi,8.9
But it's still not working. do you know any other way to read the content of excel file?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900