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

int main(){
  char string[STRING_LEN], data[STRING_LEN];
  FILE * fp1 = fopen("file.csv", "r");
  
  while(fgets(string, STRING_LEN, fp1)){
    // strtok(string, ",");
    sscanf(string, "%s", data);
    printf("%s\n", data);
  }
  
  return 0;
}


What I have tried:

In this code, I am trying to get the first word of each line with the difference of comma because it's a CSV file and the name contains two words. But sscanf is separating words with the difference of space. So I'm not able to get the last name. I can use strtok to separate but I also want to print the data one by one like data[0], data[1] later on which is not possible with strtok.
Posted
Updated 11-Oct-20 5:34am
v2

The strtok function will do what you want if you actually capture each field as you tokenise it. If you know there are exactly two fields per line then:
C++
char* data[2];
data[0] = strtok(line, " ,");
data[1] = strtok(NULL, " ,");
 
Share this answer
 
Richard is on the right track. I use a function to do this kind of thing that wraps strtok in a loop and saves a pointer to each token found in an array. It is not perfect for dealing with CSV files because it does not handle commas embedded in a quoted string correctly. Here's what it looks like :
C++
int ParseTokens( char * buffer, const char * delims, char * tokens[], int maxTokens )
{
    char * cptr = nullptr;
    char * pbuffer = buffer;
    int tokenCount = 0;
    int x;
    for( x = 0; x < maxTokens; x += 1 )
    {
        cptr = strtok( pbuffer, delims );
        if( ! cptr )
            break;

        tokens[ tokenCount ] = cptr;
        ++tokenCount;
        pbuffer = nullptr;
    }
    return tokenCount;
}
To call it you can do this :
C++
#define MAX_TOKENS 16
    char * tokens[ MAX_TOKENS ] = { nullptr };
    const char * delimiters = ",";
    int tokenCount = ParseTokens( string, delimiters, tokens, MAX_TOKENS );
 
Share this answer
 
Comments
Rick York 11-Oct-20 11:43am    
If you need to handle commas in a quoted string you can do dual-stage parsing where you separate the quoted strings first and then peel out the comma-separated values afterward. Note that strtok is destructive in that it nulls the tokens so if you need to know what the delimiters were it is not the best thing to use for parsing.
Richard MacCutchan 11-Oct-20 12:43pm    
Exactly right, I would certainly not use a declared fixed size array in a live situation.
Rick York 11-Oct-20 14:04pm    
I have another version that uses a vector of tokens but this topic is C. There is also a version that does not use strtok so it is not destructive. That one makes a vector of copied strings and delimiters so it is rather slow but useful for certain purposes. I do a lot of simple, line-based parsing and this family of functions cover about 95% of my use cases.

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