How to Split strings with fgets or fscanf in C? -


i understand how read in text file , scan/print entire file, how can line split several strings? also, can variables assigned strings called later? code far:

#include <stdio.h> #include <stdlib.h> #include <string.h>  int main() {     file *fpointer;     fpointer = fopen("p1customer.txt", "r");     char singleline[150];     int id;       while (!feof(fpointer)){         fscanf(fpointer, "%d", &id);         printf("%d",id);      }      fclose(fpointer);     return 0;    } 

example text file read: 99999 john doe basketball

example output: john doe has id number 99999 , plays basketball

i attempting split/tokenize strings , assign them variables (idnumber, name, sport) , print output in new file.

you can use library function strtok(str,chrs) function. sequence of calls of strtok(str,chrs) splits str tokens, each delimited character chrs.

the first call in sequence non null str.it finds first token in str consisting of chars not int chrs;it terminates overwrtting next characters of str \0 , return pointer token. each subsequent call,indicated null value of str,retuens pointer next such token, searching past end of privious one.


Comments

Popular posts from this blog

ios - MKAnnotationView layer is not of expected type: MKLayer -

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -