Click here to Skip to main content
15,920,111 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define STRING_LEN 200

int main(){
  char string[STRING_LEN];
  FILE * fp1 = fopen("file.csv", "r");
  char * data[2];
  char * line = NULL;
  int i = 0;

  while(fgets(string, STRING_LEN, fp1)){
    data[i] = strtok(string, ",");
    printf("data[%d] = %s\n", i, data[i]);
    i++;
  }
  return 0;
}


What I have tried:

This code is reading the first word (e.g. first & the last name but considered as one word with the difference of comma) from each line but how can I select the specific one? like I just want to print the data[1] or just want to print data[0]

The result it's giving me

data[0] = Bilal Khan
data[1] = Ali Ahmed
data[2] = Michael Jackson
Posted
Updated 11-Oct-20 22:23pm
v2

1 solution

We have offered a number of suggestions in your previous posts of this same question. Please go back and review them, including the code samples. If you have a specific problem with any offered solution then please post a reply to that message and explain what does not work.

Also, your code above is wrong:
C++
while(fgets(string, STRING_LEN, fp1)){
    // you do not need an index value since you are only trying to print the first field of each line
    char* data = strtok(string, ",");
    printf("data = %s\n", data);
//    i++;
}
 
Share this answer
 
Comments
ibilalkayy 12-Oct-20 4:57am    
Thanks Richard!

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