codingislife
codingislife

Reputation: 39

Scanf parsing a string

i'm trying to develop a method that parses a file where each element is separated by a comma.

I am trying to figure out a way to use scanf to delimit the elements. I tried using scanf("%[^,]",buffer) but got a stack smashing error.

Also do not refer me to other articles I've tried them all. Please let me know how I can use my scanf as a delimiter.

My input is as follows:

+,-,*,/,%,abcdefghijklmnopqrstuvwxyz,0123456789,(,),;,=, \t\n

Below is the code

       char buffer[MAX_LINE_LENGTH];
for (int i = 0; i < returnTable->numberOfClasses-1; i++) {
    scanf("%s ",buffer);
    buffer[strlen(buffer)] = '\0';
    returnTable->inputSymbolClasses[i] = malloc((sizeof(char) * (strlen(buffer) )+ 1));
    strcpy(returnTable->inputSymbolClasses[i],buffer);
}

Upvotes: 1

Views: 78

Answers (1)

codingislife
codingislife

Reputation: 39

I figured it out for anyone struggling with this, use scanf("%[^,],",buffer) to delimit by commas.

Upvotes: 1

Related Questions