fork download
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. #define FILENAME "book-6.dat"
  5. struct BOOK{
  6. char date[50];
  7. char item[100];
  8. int flag;
  9. int tanka;
  10. int suryo;
  11. };
  12.  
  13. #if 0
  14. 20120601 1 meat 125 10000
  15. 20120602 1 rice 2000 10
  16. 20120603 0 uriage 500 100
  17. 20120603 1 beer 140 24
  18. #endif
  19.  
  20. #define MAXLINE 100
  21.  
  22. int readData(FILE *fp, struct BOOK *book) {
  23. int i;
  24. for (i = 0; i < MAXLINE; i++) {
  25. if (fscanf(fp, "%s %d %s %d %d\n",
  26. book[i].date, &book[i].flag, book[i].item, &book[i].tanka, &book[i].suryo) != 5)
  27. break;
  28. }
  29. return i;
  30. }
  31. int main() {
  32. struct BOOK mybook[100];
  33. int i, n;
  34. FILE *fp;
  35. if((fp = fopen(FILENAME, "r")) == 0) {
  36. fprintf(stderr, "%s cannot be found!\n", FILENAME);
  37. exit(1);
  38. }
  39. n = readData(fp, mybook);
  40. fclose(fp);
  41. for(i = 0; i < n; i++){
  42. printf("%s : %d x %d\n", mybook[i].item, mybook[i].tanka, mybook[i].suryo);
  43. }
  44. return 0;
  45. }
  46. /* end */
  47.  
Runtime error #stdin #stdout 0.01s 1848KB
stdin
Standard input is empty
stdout
Standard output is empty