Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 12 economic10
- 13 science5
- 14 music1
- 15 physics9
- 16 chemistry9
- 17 history2
- 18 anatomy7
- 19 physiology7
- 20 literature3
- 21 fiction3
- 16 chemistry7
- 14 music10
- 20 literature1
- #include <stdio.h>
- #define NAME_MAX 64
- int main(int argc, char ** argv)
- {
- FILE * file = fopen("foo.txt", "rb");
- unsigned int course, department;
- char name[NAME_MAX];
- while(fscanf(file, "%u %[a-z]%u", &course, name, &department) != EOF)
- {
- // do stuff with records
- printf("%u-%u %sn", department, course, name);
- }
- fclose(file);
- return 0;
- }
- DO_READ read from file
- is END_OF_RECORD char present?
- yes: GOTO DO_PROCESS
- no : GOTO DO_READ
- DO_PROCESS read into buffer
- is END_OF_FILE mark present?
- yes: GOTO DOSOMETHINGWITHIT
- no: GOTO DO_PROCESS
- // binaryFile.cpp
- #include "stdafx.h"
- #include <stdio.h>
- #include <string.h>
- #define BUFSIZE 64
- int _tmain(int argc, _TCHAR* argv[])
- {
- FILE *f;
- char buf[BUFSIZE+1];
- // create dummy bin file
- f = fopen("temp.bin","wb");
- if (f)
- { // not writing all the data, just a few examples
- sprintf(buf,"%04d%s 0",12,"economic10"); fwrite(buf,sizeof(char),strlen(buf)+1,f);
- sprintf(buf,"%04d%s 0",13,"science5"); fwrite(buf,sizeof(char),strlen(buf)+1,f);
- sprintf(buf,"%04d%s 0",14,"music1"); fwrite(buf,sizeof(char),strlen(buf)+1,f);
- sprintf(buf,"%04d%s 0",15,"physics9"); fwrite(buf,sizeof(char),strlen(buf)+1,f);
- fclose(f);
- }
- // read dummy bin file
- f = fopen("temp.bin","rb");
- if (f)
- {
- int classID;
- char str[64];
- char *pData
- long offset = 0;
- do
- {
- fseek(f,offset,SEEK_SET);
- pData = fgets(buf,BUFSIZE,f);
- if (pData)
- { sscanf(buf,"%04d%s",&classID,&str);
- printf("%dt%srn",classID,str);
- offset +=strlen(pData)+1; // record + 1 null character
- }
- } while(pData);
- fclose(f);
- }
- getchar();
- return 0;
- }
Add Comment
Please, Sign In to add comment