File Processing 1 data hierarchy Bit Byte Field

File Processing (������� ) 1



ลำดบของขอมล (data hierarchy) Bit ขนาดเลก Byte Field Record File Database ขนาดใหญ 4

ลำดบของขอมล SU 1 2 3 KU 4 5 6 CU 7 8 9 SU 1 2 3 SU Field 00100100 1 (ตอ ( File Record Byte Bit 5









การเปดและปดแฟมขอม ล (ตอ ( �������� FILE ���� student. txt ������� mode “r” ����� #include<stdio. h> void main() { FILE *ptr. Data; if((ptr. Data = fopen(“c: \student. txt”, ”r”)) != NULL) { printf(“Open file Complete !!”); fclose(ptr. Data); } ������ else ������ printf(“Can not Open file”); } 14




#include <stdio. h> void main() { char ch; FILE *ptr. Data; ptr. Data=fopen(“ex 2. txt”, ”r”); if (ptr. Data != NULL) { while ((ch=fgetc(ptr. Data)) != EOF) printf(“%c”, ch); } else printf(“Open file Error”); fclose(ptr. Data); } 18


#include <stdio. h> E 25 main() C 11 { F 30 FILE *ptr. Data; char ch; int num; ptr. Data=fopen("c: \code. txt", “r"); if (ptr. Data != NULL) { while(feof(ptr. Data) ==0) { fscanf(ptr. Data, "%c%d", &ch, &num); if (ch==‘C') printf("your room is %c%d", ch, num); } } else printf(“Open file Error”); fclose(ptr. Data); } 20

การอานขอมลในแฟมแบบ Access code. txt E 25 C 11 F 30 %c *ptr. Data Sequential program %d ����� � your room is C 11 fscanf(ptr. Data, "%c%d", &ch, &num); 21


#include <stdio. h> void main() { FILE *ptr. Data; char ch; ptr. Data=fopen("c: \bin. txt", "w"); if (ptr. Data != NULL) { printf(“Type words (<enter> to exit) ->"); while((ch=getchar()) != 'n') fputc(ch, ptr. Data); } else printf(“Open file Error”); fclose(ptr. Data); } 23

การเขยน ขอมลในแฟมแบบ Access Sequential Type words (<enter> to exit) -> Computer Olympic ch ta a D tr p * Computer Olympic fputc(ch, ptr. Data); bin. txt 24


#include <stdio. h> void main() { FILE *ptr. Data; ptr. Data=fopen("c: \print_t. txt", "w"); if (ptr. Data != NULL) fprintf(ptr. Data, "Hello class %d", 1); else printf(“Open file Error”); fclose(ptr. Data); } Hello class 1 print_t. txt 26




#include <stdio. h> void main() { FILE *ptr. Source; FILE *ptr. Target; char c; if((ptr. Source=fopen("Source. dat", "r")) != NULL) { if((ptr. Target=fopen("Target. dat", "w")) != NULL) { while(!feof(ptr. Source)) /* check EOF */ { c=fgetc(ptr. Source); if(ferror(ptr. Source)!= 0) /* check write data */ { printf("Error Read data from source filen"); return(); } 30

fputc(c, ptr. Target); /* write to target file */ if(ferror(ptr. Target)!= 0) /* check write data */ { printf("Error Writing to Target filen"); return(); } } /* end while */ } /* end if open Target file */ else printf("Can't open Target file"); } /* end if open Source file */ else printf("Can't open Source file"); fclose(ptr. Source); fclose(ptr. Target); } /* end main */ 31

การตรวจสอบการอาน /เขยน แฟมขอมล Source. dat Abcdefg 1234 c=fgetc(ptr. Source); ������� Target. dat Abcdefg 1234 fputc(c, ptr. Target); 32







������� ��� String (��� ) EX. ��������� fgets(); #include <stdio. h> #include <string. h> #include <conio. h> void main() { FILE *ptr. Data; char str_in[25]; clrscr(); if((ptr. Data=fopen("a: \student. txt", "r")) != NULL) { fgets(str_in, 25, ptr. Data); printf("n%s", str_in); } else printf("n. Openfile not Completen"); fclose(ptr. Data); } 39

������� ��� String (��� ) student. txt abcdefghijklmnop 123456789 opqrstuvwxyz 102004507872 25 ������ fgets(str_in, 25, ptr. Data); str_in a b c d e f 0 g h i j k l m n o p 1 2 3 4 5 6 7 8 ‘ ’ 24 ** �������� string ����������� 40



������� ��� String (��� ) EX. ��������� fputs(); #include <stdio. h> #include <string. h> void main() { FILE *ptr. Data; char visitor_name[50], ch=‘y’; if((ptr. Data=fopen("a: \visitor. txt", “w")) != NULL) { while(ch==‘y’) { printf(“Type Visitor name and press “enter” ->”); scanf(“%s”, visitor_name); fputs(visitor_name, ptr. Data); fputc(‘n’, ptr. Data); /* insert new line at end of name */ printf(“nn. Press ‘y’ to enter another name or any key to Exit->”); ch=getchar(); } } else printf("n. Openfile not Completen"); fclose(ptr. Data); } 43

������� ��� String (��� ) EX. ��������� fputs(); Visitor_name W o r w u t ‘ ’ a Dat *ptr ���� Type ��Visitor name and press “enter” -> Worawut a Press ‘y’ to enter another name or any key to exit-> n visitor. txt Worawut fputs(visitor_name, ptr. Data); 44









- Slides: 53