fork download
  1. #include <stdio.h>
  2. struct ps {
  3. char name[100];
  4. int year;
  5. double ht;
  6. double wt;
  7. };
  8. int main(void)
  9. {
  10. FILE *in;
  11. FILE *out;
  12. struct ps inv[3];
  13. int i = 0;
  14. char buf[100];
  15.  
  16. in = fopen("data.txt", "r");
  17. /* fread(inv, sizeof(struct ps), 3, in); */
  18. for(i=0; i<3; i++) {
  19. fgets(buf,sizeof(buf),in);
  20. sscanf(buf,"%s %d %lf %lf",
  21. inv[i].name, &inv[i].year, &inv[i].ht, &inv[i].wt);
  22. printf("%s\t%5d%7.1f%7.1f\n",
  23. inv[i].name, inv[i].year, inv[i].ht, inv[i].wt);
  24. }
  25. fclose(in);
  26.  
  27. out = fopen("new.txt", "w");
  28. for(i=0; i<3; i++) {
  29. fprintf(out,"%s\t%5d%7.1f%7.1f\n",
  30. inv[i].name, inv[i].year, inv[i].ht, inv[i].wt);
  31. }
  32. /* fwrite(inv, sizeof(struct ps), 3, out); */
  33. fclose(out);
  34. return 0;
  35. }
Runtime error #stdin #stdout 0.01s 1848KB
stdin
Standard input is empty
stdout
Standard output is empty