Guest User

Untitled

a guest
Jun 18th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.90 KB | None | 0 0
  1. 12 economic10
  2. 13 science5
  3. 14 music1
  4. 15 physics9
  5. 16 chemistry9
  6. 17 history2
  7. 18 anatomy7
  8. 19 physiology7
  9. 20 literature3
  10. 21 fiction3
  11. 16 chemistry7
  12. 14 music10
  13. 20 literature1
  14.  
  15. #include <stdio.h>
  16.  
  17. #define NAME_MAX 64
  18.  
  19. int main(int argc, char ** argv)
  20. {
  21. FILE * file = fopen("foo.txt", "rb");
  22. unsigned int course, department;
  23. char name[NAME_MAX];
  24.  
  25. while(fscanf(file, "%u %[a-z]%u", &course, name, &department) != EOF)
  26. {
  27. // do stuff with records
  28. printf("%u-%u %sn", department, course, name);
  29. }
  30.  
  31. fclose(file);
  32.  
  33. return 0;
  34. }
  35.  
  36. DO_READ read from file
  37. is END_OF_RECORD char present?
  38. yes: GOTO DO_PROCESS
  39. no : GOTO DO_READ
  40.  
  41. DO_PROCESS read into buffer
  42. is END_OF_FILE mark present?
  43. yes: GOTO DOSOMETHINGWITHIT
  44. no: GOTO DO_PROCESS
  45.  
  46. // binaryFile.cpp
  47. #include "stdafx.h"
  48. #include <stdio.h>
  49. #include <string.h>
  50.  
  51. #define BUFSIZE 64
  52. int _tmain(int argc, _TCHAR* argv[])
  53. {
  54. FILE *f;
  55. char buf[BUFSIZE+1];
  56.  
  57. // create dummy bin file
  58. f = fopen("temp.bin","wb");
  59. if (f)
  60. { // not writing all the data, just a few examples
  61. sprintf(buf,"%04d%s0",12,"economic10"); fwrite(buf,sizeof(char),strlen(buf)+1,f);
  62. sprintf(buf,"%04d%s0",13,"science5"); fwrite(buf,sizeof(char),strlen(buf)+1,f);
  63. sprintf(buf,"%04d%s0",14,"music1"); fwrite(buf,sizeof(char),strlen(buf)+1,f);
  64. sprintf(buf,"%04d%s0",15,"physics9"); fwrite(buf,sizeof(char),strlen(buf)+1,f);
  65. fclose(f);
  66. }
  67. // read dummy bin file
  68. f = fopen("temp.bin","rb");
  69. if (f)
  70. {
  71. int classID;
  72. char str[64];
  73. char *pData
  74. long offset = 0;
  75. do
  76. {
  77. fseek(f,offset,SEEK_SET);
  78. pData = fgets(buf,BUFSIZE,f);
  79. if (pData)
  80. { sscanf(buf,"%04d%s",&classID,&str);
  81. printf("%dt%srn",classID,str);
  82. offset +=strlen(pData)+1; // record + 1 null character
  83. }
  84. } while(pData);
  85. fclose(f);
  86. }
  87. getchar();
  88. return 0;
  89. }
Add Comment
Please, Sign In to add comment