fork(520) download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <errno.h>
  5.  
  6. const char* getfield(char* line, int num)
  7. {
  8. const char* tok;
  9. for (tok = strtok(line, ";");
  10. tok && *tok;
  11. tok = strtok(NULL, ";\n"))
  12. {
  13. if (!--num)
  14. return tok;
  15. }
  16. return NULL;
  17. }
  18.  
  19. int main()
  20. {
  21. char line[1024];
  22. while (fgets(line, 1024, stdin))
  23. {
  24. char* tmp = strdup(line);
  25. printf("Field 3 would be %s\n", getfield(tmp, 3));
  26. // NOTE strtok clobbers tmp
  27. free(tmp);
  28. }
  29. }
  30.  
Success #stdin #stdout 0.01s 1812KB
stdin
lp;imie;nazwisko;ulica;numer;kod;miejscowosc;telefon;email;data_ur
1;Jan;Kowalski;ul. Nowa;1a;11-234;Budry;123-123-456;jan@go.xxx;1980.05.13
2;Jerzy;Nowak;ul. Konopnicka;13a/3;00-900;Lichowice;(55)333-44-55;jer@wu.to;1990.03.23
stdout
Field 3 would be nazwisko
Field 3 would be Kowalski
Field 3 would be Nowak