01001012 Principle of Computer Programming ong txt includeprocess

  • Slides: 35
Download presentation

ตวอยางโปรแกรมเปดเทกไฟลทผด 01001012 Principle of Computer Programming กรณทไฟล ong. txt ไมไดอยท //#include<process. h> exit 0;

ตวอยางโปรแกรมเปดเทกไฟลทผด 01001012 Principle of Computer Programming กรณทไฟล ong. txt ไมไดอยท //#include<process. h> exit 0; Directory เดยวกบ โปรแกรม #include<stdio. h> #include<conio. h> int main() { FILE *fp; fp = fopen("ong. txt", "r"); if (fp == NULL) { printf("Cannot open filen"); return 0; } else printf("Can open filen"); return 0; } Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ Cannot open file 10 King Mongkut's Institute of Technology

ตวอยางโปรแกรมเปดเทกไฟลทถก 01001012 Principle of Computer Programming #include<stdio. h> #include<conio. h> //#include<process. h> exit 0;

ตวอยางโปรแกรมเปดเทกไฟลทถก 01001012 Principle of Computer Programming #include<stdio. h> #include<conio. h> //#include<process. h> exit 0; int main() { FILE *fp; fp = fopen("C: /Temp/ong. txt", "r"); Can open file if (fp == NULL) { printf("Cannot open filen"); return 0; } else printf("Can open filen"); return 0; } Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ 11 King Mongkut's Institute of Technology

รปแบบการปดเทกซไฟล 01001012 Principle of Computer Programming fclose (fp) fp คอ ตวแปรทใชเกบตำแหนงไฟล หากสามารถปดเทกไฟลไดสำเรจจะ กลบ แตหากไมสำเรจจะ

รปแบบการปดเทกซไฟล 01001012 Principle of Computer Programming fclose (fp) fp คอ ตวแปรทใชเกบตำแหนงไฟล หากสามารถปดเทกไฟลไดสำเรจจะ กลบ แตหากไมสำเรจจะ return คา EOF Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ return คา 0 12 King Mongkut's Institute of Technology

ตวอยางโปรแกรมเปด - ปดเทกไฟล 01001012 Principle of Computer Programming #include<stdio. h> #include<conio. h> //#include<process. h>

ตวอยางโปรแกรมเปด - ปดเทกไฟล 01001012 Principle of Computer Programming #include<stdio. h> #include<conio. h> //#include<process. h> exit 0; int main() { FILE *fp; Can open file fp = fopen("C: /Temp/ong. txt", "r"); if (fp == NULL) Close file { printf("Cannot open filen"); getch(); return 0; } printf("Can open filen"); if (!fclose(fp)) printf("n. Close filen"); return 0; } Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ 13 King Mongkut's Institute of Technology

โปรแกรมอานขอมลจากเทกไฟล (เขยนเตม ) (2) #include<stdio. h> | 1 ขอความ 01001012 Principle of Computer Programming

โปรแกรมอานขอมลจากเทกไฟล (เขยนเตม ) (2) #include<stdio. h> | 1 ขอความ 01001012 Principle of Computer Programming Can open file #include<conio. h> int main() Close file { FILE *fp; Programming char str[20]; fp = fopen("C: /Temp/ong. txt", "r"); if (fp == NULL) { printf("Cannot open filen"); return 0; } printf("Can open filen"); fscanf(fp, "%s", str); if (!fclose(fp)) printf("n. Close filen"); getch(); printf("%s", str); return 0; } Sittigorn Jirasak http: //www. kmitl. ac. th/~ksjirasa/ 16 King Mongkut's Institute of Technology

โปรแกรมอานขอมลจากเทกไฟล (เขยนยอ ) (3) | 1 ขอความ 01001012 Principle of Computer Programming #include<stdio. h>

โปรแกรมอานขอมลจากเทกไฟล (เขยนยอ ) (3) | 1 ขอความ 01001012 Principle of Computer Programming #include<stdio. h> int main() { FILE *fp; char str[20]; fp = fopen("C: /Temp/ong. txt", "r"); fscanf(fp, "%s", str); fclose(fp); printf("%s", str); return 0; } Programming Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ 17 King Mongkut's Institute of Technology

โปรแกรมอานขอมลจากเทกไฟล (เขยนยอ ) (4) | 3 ขอความ 01001012 Principle of Computer Programming #include<stdio. h>

โปรแกรมอานขอมลจากเทกไฟล (เขยนยอ ) (4) | 3 ขอความ 01001012 Principle of Computer Programming #include<stdio. h> int main() { FILE *fp; char str 1[20], str 2[20], str 3[20]; fp = fopen("C: /Temp/ong. txt", "r"); fscanf(fp, "%s %s %s", str 1, str 2, str 3); fclose(fp); printf("%s %s %s", str 1, str 2, str 3); return 0; } Programming 2548 kmitl Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ 18 King Mongkut's Institute of Technology

โปรแกรมอานขอมลจากเทกไฟล (เขยนยอ ) (5) | 3 อกขระ 01001012 Principle of Computer Programming #include<stdio. h>

โปรแกรมอานขอมลจากเทกไฟล (เขยนยอ ) (5) | 3 อกขระ 01001012 Principle of Computer Programming #include<stdio. h> int main() { FILE *fp; char ch 1, ch 2, ch 3; fp = fopen("C: /Temp/ong. txt", "r"); fscanf(fp, "%c%c%c", &ch 1, &ch 2, &ch 3); fclose(fp); printf("%c %c %c", ch 1, ch 2, ch 3); return 0; } P r o Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ 19 King Mongkut's Institute of Technology

โปรแกรมอานขอมลจากเทกไฟล จำนวนเตม (ยอ ) (6) #include<stdio. h> int main() { FILE *fp; char str

โปรแกรมอานขอมลจากเทกไฟล จำนวนเตม (ยอ ) (6) #include<stdio. h> int main() { FILE *fp; char str 1[20], str 2[20]; | 2 ขอความ 1 01001012 Principle of Computer Programming int num; fp = fopen("C: /Temp/ong. txt", "r"); fscanf(fp, "%s %d %s", str 1, &num, str 2); fclose(fp); printf("%s %s %d", str 1, str 2, num); return 0; } Programming kmitl 2548 Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ 20 King Mongkut's Institute of Technology

โปรแกรมรบชอ และอายของคน 3 คนแลว () 01001012 Principle of Computer Programming #include<stdio. h> #include<conio. h>

โปรแกรมรบชอ และอายของคน 3 คนแลว () 01001012 Principle of Computer Programming #include<stdio. h> #include<conio. h> //#include<process. h> exit 0; int main() { FILE *fp; char name[20], ch; int age, count=0; fp = fopen("C: /Temp/new. txt", "w"); if (fp == NULL) { printf("Cannot open filen"); getch(); return 0; } printf("Can open file (write)n"); //continue Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ 23 King Mongkut's Institute of Technology

โปรแกรมรบชอ และอายของคน 3 คนแลว () 01001012 Principle of Computer Programming /*input data in program

โปรแกรมรบชอ และอายของคน 3 คนแลว () 01001012 Principle of Computer Programming /*input data in program & write on file*/ while(count<3) { printf ("n. Person [%d]n", count+1); printf ("Enter name : "); scanf ("%s", name); printf ("Enter age : "); scanf ("%d", &age); fprintf (fp, "n. Person [%d]n", count+1); fprintf (fp, "Name : %-20 s age : %dn", name, age); count++; } if (!fclose(fp)) printf("n. Close filen"); //continue Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ 24 King Mongkut's Institute of Technology

โปรแกรมรบชอ และอายของคน 3 คนแลว () 01001012 Principle of Computer Programming getch(); fp = fopen("C:

โปรแกรมรบชอ และอายของคน 3 คนแลว () 01001012 Principle of Computer Programming getch(); fp = fopen("C: /Temp/new. txt", "r"); if (fp == NULL) { printf("Cannot open filen"); getch(); return 0; } printf("Can open file (read)n"); ch = getc(fp); while (ch!=EOF) { printf("%c", ch); ch = getc(fp); } if (!fclose(fp)) printf("n. Close filen"); return 0; } Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ 25 King Mongkut's Institute of Technology

โปรแกรมเขยน Structure ลงไฟล 01001012 Principle of Computer Programming #include<stdio. h> Name : Kmitl int

โปรแกรมเขยน Structure ลงไฟล 01001012 Principle of Computer Programming #include<stdio. h> Name : Kmitl int main() ID : 48010000 { Age : 44_ FILE *fptr; struct student { char name[20]; char ID[9]; int age; } s; fptr = fopen("St. Record. txt", "w"); printf("Name : "); scanf("%s", s. name); //flushall(); printf("ID : "); scanf("%s", s. ID); //flushall(); printf("Age : "); scanf("%d", &s. age); //flushall(); fwrite(&s, sizeof(struct student), 1, fptr); fclose(fptr); return 0; } Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ 27 King Mongkut's Institute of Technology

โปรแกรมอาน Structure จากไฟล 01001012 Principle of Computer Programming #include<stdio. h> Name: Kmitl int main()

โปรแกรมอาน Structure จากไฟล 01001012 Principle of Computer Programming #include<stdio. h> Name: Kmitl int main() ID: 48010000 and Age: 44 { FILE *fptr; ----------------struct student { char name[20]; char ID[9]; int age; } s; fptr = fopen("St. Record. txt", "r"); if (fptr == (FILE *)NULL) printf("Cannot open filen"); else while (fread(&s, sizeof(struct student), 1, fptr)!=0) { printf("Name: %sn", s. name); printf("ID: %s and Age: %dn", s. ID, s. age); printf("-----------------n"); } fclose(fptr); return Jirasak Sittigorn 0; 29 King Mongkut's Institute of Technology } http: //www. kmitl. ac. th/~ksjirasa/

โปรแกรมการใชคำสง fseek () 01001012 Principle of Computer Programming FILE *fptr; int noffset; struct student

โปรแกรมการใชคำสง fseek () 01001012 Principle of Computer Programming FILE *fptr; int noffset; struct student { char name[20]; char ID[9]; int age; } s; fptr = fopen("St. Record. txt", "w"); strcpy (s. name, "Student A"); strcpy (s. ID, "48010000"); s. age = 17; fwrite(&s, sizeof(struct student), 1, fptr); strcpy (s. name, "Student B"); strcpy (s. ID, "48012000"); s. age = 18; fwrite(&s, sizeof(struct student), 1, fptr); strcpy (s. name, "Student C"); strcpy (s. ID, "48015000"); s. age = 20; fwrite(&s, sizeof(struct student), 1, fptr); fclose(fptr); Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ 31 King Mongkut's Institute of Technology

โปรแกรมการใชคำสง fseek () 01001012 Principle of Computer Programming fptr = fopen("St. Record. txt", "r");

โปรแกรมการใชคำสง fseek () 01001012 Principle of Computer Programming fptr = fopen("St. Record. txt", "r"); noffset = 0 * sizeof(struct student); if (fseek( fptr, noffset, 0) == 0 ) { if (fread(&s, sizeof(struct student), 1, fptr) != 0) { printf("Name: %sn", s. name); printf("ID: %s and Age: %dn", s. ID, s. age); printf("-----------------n"); } } fclose(fptr); Name: Student A ID: 48010000 and Age: 17 ----------------Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ 32 King Mongkut's Institute of Technology

โปรแกรมการใชคำสง fseek () 01001012 Principle of Computer Programming fptr = fopen("St. Record. txt", "r");

โปรแกรมการใชคำสง fseek () 01001012 Principle of Computer Programming fptr = fopen("St. Record. txt", "r"); noffset = 1 * sizeof(struct student); if (fseek( fptr, noffset, 0) == 0 ) { if (fread(&s, sizeof(struct student), 1, fptr) != 0) { printf("Name: %sn", s. name); printf("ID: %s and Age: %dn", s. ID, s. age); printf("-----------------n"); } } fclose(fptr); Name: Student B ID: 48012000 and Age: 18 ----------------Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ 33 King Mongkut's Institute of Technology

โปรแกรมการใชคำสง fseek () 01001012 Principle of Computer Programming fptr = fopen("St. Record. txt", "r");

โปรแกรมการใชคำสง fseek () 01001012 Principle of Computer Programming fptr = fopen("St. Record. txt", "r"); noffset = 2 * sizeof(struct student); if (fseek( fptr, noffset, 0) == 0 ) { if (fread(&s, sizeof(struct student), 1, fptr) != 0) { printf("Name: %sn", s. name); printf("ID: %s and Age: %dn", s. ID, s. age); printf("-----------------n"); } } fclose(fptr); Name: Student C ID: 48015000 and Age: 20 ----------------Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ 34 King Mongkut's Institute of Technology

คำสงการอานขอมลจากไฟล และเขยนขอมลลงไฟล 01001012 Principle of Computer Programming fscanf (fp, "format", &variable); var = getc(fp);

คำสงการอานขอมลจากไฟล และเขยนขอมลลงไฟล 01001012 Principle of Computer Programming fscanf (fp, "format", &variable); var = getc(fp); fgets(var, length, fp); fprintf (fp, "format", value); putc (ch, fp); fputs (str, fp); fwrite(*ptr, size, items, *stream); Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ 35 King Mongkut's Institute of Technology