c - Split/Parse a String after reading a Text File? -


i trying read in basic text file, split each line separate strings , rearrange/copy them onto new text file. there simple way split , identify these strings added new file @ end of processing lines?

my code far:

#include <stdio.h> #include <stdlib.h> #include <string.h>  int main() {     file *pfilecust     fpointer = fopen("athletes.txt", "r");     char singleline[150];      while (!feof(pfilecust)){         fscanf(singleline, 150);         int id, name, sport;         fprintf(%d[0,6], %s[8,15], %s[16,22], id, name, sport);     }      fclose(fpointer);     return 0; } 

example text file read program:

88888 john doe tennis 99999 jane smith softball 

example output trying achieve.

tennis 88888 john doe softball 99999 jane smith 

each line in file corresponds record. each series of consecutive non-whitespace characters corresponds field in current record. accordingly,

/* getrecord: read next record on fp */ char *getrecord(file *fp) {     assert(fp);      char *line = malloc(maxline);     if (line != null)         if (fgets(line, maxline, fp) != null)             return line;      return null; }  /* getfield: read next field in record */ char *getfield(const char *record, int *pos) {     assert(record && pos);      char *record;     int ret;      if ((record = malloc(maxrecord)) != null) {         ret = sscanf(record + *pos, "%s", record);         if (ret == 1)             return record;     }     return null; } 

while each function not appear work, separating business logic record/field reading has real benefits. allows extensibility (for example can add error handling these routines). can make more sense of code. can write main function use pair of calls.


Comments

Popular posts from this blog

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -

python - Error while using APScheduler: 'NoneType' object has no attribute 'now' -