Advertisement
SrJefers_Loading

strtok

Apr 18th, 2016
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.07 KB | None | 0 0
  1. #include <iostream>
  2. #include <conio.h>
  3. #include <string>
  4. #include <fstream>
  5. int main(){
  6. char mystring[50];
  7. FILE* fe;
  8. int x;
  9. char cadena[128], *data1, *data2, *data3, *data4;
  10. //creacion e un fichero de salida
  11. ofstream fs("data.txt");
  12. //enviamos a una carpeta el fichero de salida
  13. fs << "103;zapatos; 2; 23" << endl;
  14. fs << "103;zapatos; 2; 23" << endl;
  15. fs << "103;zapatos; 2; 23" << endl;
  16. fs << "103;zapatos; 2; 23" << endl;
  17. fs.close();
  18. fe = fopen("data.txt", "r");
  19. rewind(fe);//para que el puntero se coloque al principio del archivo
  20. while (feof(fe) == 0){
  21. fgets(mystring, 50, fe);
  22. x = 1;
  23. char = pch;
  24. pch = strtok(mystring, ";");
  25. dato1 = pch;
  26. while (pch != NULL){
  27. pch = strtok(NULL, ";");
  28. if (x == 1){
  29. dato2 = pch; x = 2;
  30. }
  31. if (x == 2){
  32. pch = strtok(NULL, ";");
  33. dato3 = pch; x = 3;
  34. }
  35. if (x == 3){
  36. pch = strtok(NULL, ";");
  37. dato4 = pch; x = 4;
  38. }
  39. pch = strtok(NULL, ";");
  40. }
  41. cout << "dato1" << data1 << "dato2" << data2 << "dato3" << data3 << "dato4" << data4 << endl;
  42. }
  43. return 0;
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement