chap7.key

Similar documents
제1장 Unix란 무엇인가?

제7장 C 표준 파일 입출력

제7장 C 표준 파일 입출력

Microsoft PowerPoint - chap4 [호환 모드]

歯9장.PDF

Microsoft PowerPoint - chap13-입출력라이브러리.pptx

PowerPoint 프레젠테이션

Microsoft PowerPoint - 제11강 파일 처리

PowerPoint 프레젠테이션

금오공대 컴퓨터공학전공 강의자료

제12장 파일 입출력

PowerPoint Template

C Programming

Microsoft PowerPoint - Lesson13.pptx

슬라이드 1

Microsoft PowerPoint - 09_(C_Programming)_(Korean)_File_Processing

<4D F736F F F696E74202D2034C5D8BDBAC6AEC6C4C0CFC0D4C3E2B7C2312E505054>

Microsoft PowerPoint - Chap14_FileAccess.pptx

untitled

11장 포인터

Microsoft PowerPoint - 10_C_Language_Text_Files

À©µµ³×Æ®¿÷ÇÁ·Î±×·¡¹Ö4Àå_ÃÖÁ¾

제1장 Unix란 무엇인가?

ABC 11장

Microsoft PowerPoint APUE(Intro).ppt

BMP 파일 처리

Microsoft PowerPoint - Lecture 4-1 Linux File Environment.ppt [호환 모드]


PowerPoint 프레젠테이션

2009년 상반기 사업계획

K&R2 Reference Manual 번역본

<4D F736F F F696E74202D FC6C4C0CF20C0D4C3E2B7C2205BC8A3C8AF20B8F0B5E55D>

슬라이드 1


Microsoft PowerPoint - chap1 [호환 모드]

<4D F736F F F696E74202D20C1A63136C0E520C6C4C0CFC0D4C3E2B7C2>

Computer Programming (2008 Fall)

표준입출력스트림 기본적인스트림들은프로그래머가생성하지않아도자동으로생성된다. 이름스트림연결장치 stdin 표준입력스트림키보드 stdout 표준출력스트림모니터의화면 stderr 표준오류스트림모니터의화면 입출력함수의분류 사용하는스트림에따른분류 표준입출력스트림을사용하여입출력을하

제1장 Unix란 무엇인가?

Microsoft PowerPoint - 11_C_Language_C_Standard_Library

PowerPoint 프레젠테이션

untitled

<443A5C4C C4B48555C B3E25C32C7D0B1E25CBCB3B0E8C7C1B7CEC1A7C6AE425CBED0C3E0C7C1B7CEB1D7B7A55C D616E2E637070>

본 강의에 들어가기 전

Microsoft PowerPoint - chap11-1.ppt [호환 모드]

Microsoft PowerPoint - C프로그래밍-chap16.ppt

Microsoft PowerPoint - chap11.ppt [호환 모드]

10.


untitled

Microsoft PowerPoint - C_Programming_Basics.ppt [호환 모드]

untitled

PowerPoint 프레젠테이션

PowerPoint 프레젠테이션

6주차.key

학번 : 이름 : 1. 다음파일트리구조를가진유닉스시스템이있다고가정하자. / /bin/ /home/ /home/taesoo/ /usr/ /usr/lib/ /usr/local/lib /media 모든폴더에파일이하나도없다고가정했을때사용자가터미널에서다음 ls 명령입력시화면출력

03장.스택.key

슬라이드 1

고급 프로그래밍 설계

Microsoft PowerPoint - Ch12.파일.pptx

윤성우의 열혈 TCP/IP 소켓 프로그래밊

데이타전송

프로그램을 학교 등지에서 조금이라도 배운 사람들을 위한 프로그래밍 노트 입니다. 저 역시 그 사람들 중 하나 입니다. 중고등학교 시절 학교 도서관, 새로 생긴 시립 도서관 등을 다니며 책을 보 고 정리하며 어느정도 독학으르 공부하긴 했지만, 자주 안하다 보면 금방 잊어

C++-¿Ïº®Çؼ³10Àå

<443A5C4C C4B48555C B3E25C32C7D0B1E25CBCB3B0E8C7C1B7CEC1A7C6AE425CBED0C3E0C7C1B7CEB1D7B7A55C4C656D70656C2D5A69762E637070>

Microsoft PowerPoint - [2009] 02.pptx

2007_2_project4

<4D F736F F F696E74202D20B8AEB4AABDBA20BFC0B7F920C3B3B8AEC7CFB1E22E BC8A3C8AF20B8F0B5E55D>

본 강의에 들어가기 전

< E20C6DFBFFEBEEE20C0DBBCBAC0BB20C0A7C7D12043BEF0BEEE20492E707074>

歯7장.PDF

chap7.PDF

연습문제만-수정

13주-14주proc.PDF

<4D F736F F F696E74202D D20B9AEC0DABFAD2C20BDBAC6AEB8B2B0FA20C6C4C0CF20C0D4C3E2B7C2>

Microsoft PowerPoint - 제9강 문자열

Standard C Library.hwp

학번 : 이름 : 1. 다음파일트리구조를가진유닉스시스템이있다. / /bin/ /home/ /home/taesoo/ /home/taesoo/downloads /usr/ /usr/lib/ /usr/local/lib /media 모든폴더에파일이하나도없다고가정했을때사용자 (t

PowerPoint 프레젠테이션

int main(void) int a; int b; a=3; b=a+5; printf("a : %d \n", a); printf("b : %d \n", b); a b 3 a a+5 b &a(12ff60) &b(12ff54) 3 a 8 b printf(" a : %x \

Microsoft PowerPoint - chap12 [호환 모드]

PowerPoint 프레젠테이션


Sena Technologies, Inc. HelloDevice Super 1.1.0

Microsoft PowerPoint - chap2

<4D F736F F F696E74202D20C1A63132B0AD20B5BFC0FB20B8DEB8F0B8AEC7D2B4E7>

11장 포인터

Microsoft PowerPoint APUE(File InO).ppt

untitled

Microsoft PowerPoint APUE(File InO)

5.스택(강의자료).key

Microsoft PowerPoint - C프로그래밍-chap15.ppt [호환 모드]

3. 다음그림은프로세스의 file table 과 v-node 테이블의연결관계예제이다. 위그림을참고하여두개의서로다른프로세스가같은파일을 open 명령을사용하여열었을때의연결관계를도시하시오. 4. 메모리영역은 low-address 부터 high-adress 까지순서대로나열했을

Chapter #01 Subject

Microsoft PowerPoint - Chapter_09.pptx

PowerPoint 프레젠테이션

Microsoft PowerPoint - chap6 [호환 모드]

(Asynchronous Mode) ( 1, 5~8, 1~2) & (Parity) 1 ; * S erial Port (BIOS INT 14H) - 1 -

Microsoft PowerPoint APUE(File InO).pptx

vi 사용법

Transcription:

1 7 C

2 7.1

C (System Calls) Unix UNIX man Section 2 C. C (Library Functions) C 1975 Dennis Ritchie ANSI C Standard Library 3

(system call). 4

C?... 5

C (text file), C. (binary file). 6

C 1. : fopen( ) 2. : 3. : fclose( ) 7

(fopen). FILE FILE. fopen() FILE *fopen(const char *filename, const char *mode); const char *filename: const char *mode: FILE *fp; fp = fopen( ~/sp/text.txt", "r"); if (fp == NULL) { printf(" \n"); } fp = fopen("outdata.txt", "w"); fp = fopen("outdata.txt", "a"); 8

fopen (): "r" (read) NULL "w" (write) "a" (append) "r+" NULL "w+" "a+" 9

FILE (file descriptor) C fopen( ) FILE (FILE *) FILE FILE #include <stdio.h> FILE, 10

FILE FILE : typedef struct { int cnt; unsigned char*base; unsigned char*ptr; // // // unsinged flag; int fd; } FILE; // FILE // ( _IOFBF, _IOLBF, _IONBUF, // _IOEOF, _IOERR _IOREAD, _IOWRT) // 11

/ / I/O (stream) open stdin, stdout, stderr FILE* #include <stdio.h> stdin FILE stdout FILE stderr FILE 12

. int fclose(file *fp ); fp fopen 0, EOF( -1). fclose(fp); 13

7.2

getchar() fgetc(), getc() putchar() fputc(), putc() gets() fgets() puts() fputs() scanf() fscanf() printf() fprintf()

fgetc() fputc(). int fgetc(file *fp); getc fp. EOF(-1). int fputc(int c, FILE *fp); putc EOF(-1) 16

cat.c #include <stdio.h> /* */ int main(int argc, char *argv[]) { FILE *fp; int c; if (argc < 2) fp = stdin; // else fp = fopen(argv[1],"r"); // c = getc(fp); // while (c!= EOF) { // putc(c, stdout); // c = getc(fp); // } fclose(fp); return 0; } 17

copy.c ( ) #include <stdio.h> /* */ int main(int argc, char *argv[]) { char c; FILE *fp1, *fp2; if (argc!=3) { fprintf(stderr, " : %s 1 2\n", argv[0]); return 1; } 18

copy.c fp1 = fopen(argv[1], "r"); if (fp1 == NULL) { fprintf(stderr, " %s \n", argv[1]); return 2; } fp2 = fopen(argv[2], "w"); while ((c = fgetc(fp1))!= EOF) fputc(c, fp2); } fclose(fp1); fclose(fp2); return 0; 19

int feof(file *fp) fp 0 0. int ungetc(int c, FILE *p) c. 1. int fflush(file *fp) fp.. 20

fgets() fputs(). char* fgets(char *s, int n, FILE *fp); s s ('\n') EOF n-1 NULL. NULL. int fputs(const char *s, FILE *fp); s fp EOF 21

line.c 1 #include <stdio.h> 2 #define MAXLINE 80 3 4 /*. */ 5 int main(int argc, char *argv[]) 6 { 7 FILE *fp; 8 int line = 0; 9 char buffer[maxline]; 10 11 if (argc!= 2) { 12 fprintf(stderr, " :line \n"); 13 exit(1); 14 } 22

line.c 15 16 if ( (fp = fopen(argv[1],"r")) == NULL) 17 { 18 fprintf(stderr, " 23 line++; 19 exit(2); 20 } 21 22 while (fgets(buffer, MAXLINE, fp)!= NULL) { // 23 line++; 24 printf("%3d %s", line, buffer); 25 } 26 exit(0); 27 } 23

fprintf() printf(). fscanf() scanf(). int fprintf(file *fp, const char *format,...); fprintf fp FILE printf int fscanf(file *fp, const char *format,...); fscanf fp FILE scanf 24

fprint.c #include <stdio.h> #include "student.h" /*. */ int main(int argc, char* argv[]) { struct student rec; FILE *fp; if (argc!= 2) { fprintf(stderr, " : %s \n", argv[0]); return 1; } fp = fopen(argv[1], "w"); printf("%-9s %-7s %-4s\n", " ", " ", " "); while (scanf("%d %s %d", &rec.id, rec.name, &rec.score)==3) fprintf(fp, "%d %s %d ", rec.id, rec.name, rec.score); fclose(fp); return 0; } 25

fscan.c #include <stdio.h> #include "student.h" /*. */ int main(int argc, char* argv[]) { struct student rec; FILE *fp; if (argc!= 2) { fprintf(stderr, " : %s \n", argv[0]); return 1; } fp = fopen(argv[1], "r"); printf("%-9s %-7s %-4s\n", " ", " ", " "); while (fscanf(fp,"%d %s %d", &rec.id, rec.name, &rec.score)==3) printf("%10d %6s %6d\n", rec.id, rec.name, rec.score); fclose(fp); return 0; } 26

7.3

fopen(): "rb" (read) NULL "wb" (write) "ab" (append) "rb+" NULL "wb+" "ab+" 28

fread() fwrite() int fread(void *buf, int size, int n, FILE *fp); fp size ( ) n buf int fwrite(const void *buf, int size, int n, FILE *fp); fp buf size ( ) n 29

fwrite() 30

: rec struct student rec; FILE *fp = fopen( stfile", "wb"); fwrite(&rec, sizeof(rec), 1, fp); 31

stcreate1. c #include <stdio.h> #include "student.h" int main(int argc, char* argv[]) { struct student rec; FILE *fp; if (argc!= 2) { fprintf(stderr, " : %s \n",argv[0]); exit(1); } student.h struct student { int id; char name[20]; short score; }; fp = fopen(argv[1], "wb"); printf("%-9s %-7s %-4s\n", " ", " ", " "); while (scanf("%d %s %d", &rec.id, rec.name, &rec.score) == 3) fwrite(&rec, sizeof(rec), 1, fp); } fclose(fp); exit(0); 32

fread() 33

stprint.c #include <stdio.h> #include "student.h" /*. */ int main(int argc, char* argv[]) { struct student rec; FILE *fp; if (argc!= 2) { fprintf(stderr, " : %s \n", argv[0]); return 1; } if ((fp = fopen(argv[1], "rb")) == NULL ) { fprintf(stderr, " \n"); return 2; } 34

stprint.c printf("-----------------------------------\n"); printf("%10s %6s %6s\n", " ", " ", " "); printf("-----------------------------------\n"); while (fread(&rec, sizeof(rec), 1, fp) > 0) if (rec.id!= 0) printf("%10d %6s %6d\n", rec.id, rec.name, rec.score); } printf("-----------------------------------\n"); fclose(fp); return 0; 35

7.4 36

(current file position) (file position pointer). 37

fseek(file *fp, long offset, int mode) rewind(file *fp). ftell(file *fp) 38

fseek() fseek(file *fp, long offset, int mode) FILE fp (mode) (offset). SEEK_SET 0 SEEK_CUR 1 SEEK_END 2 39

fseek() fseek(fd, 0L, SEEK_SET); (rewind) fseek(fd, 100L, SEEK_SET); 100 fseek(fd, 0L, SEEK_END); (append) fseek(fd, n * sizeof(record), SEEK_SET); n+1 fseek(fd, sizeof(record), SEEK_CUR); fseek(fd, -sizeof(record), SEEK_CUR); fseek(fd, sizeof(record), SEEK_END); 40

: fwrite(&record1, sizeof(record), 1, fp); fwrite(&record2, sizeof(record), 1, fp); fseek(fd, sizeof(record), SEEK_END); fwrite(&record3, sizeof(record), 1, fp); #1 #2 #3 41

stcreate2.c #include <stdio.h> #include "student.h" #define START_ID 1201001 /*. */ int main(int argc, char* argv[]) { struct student rec; FILE *fp; if (argc!= 2) { fprintf(stderr, " : %s \n", argv[0]); exit(1); } 42

stcreate2.c } fp = fopen(argv[1], "wb"); printf("%7s %6s %4s\n", " ", " ", " "); while (scanf("%d %s %d", &rec.id, rec.name, &rec.score) == 3) { fseek(fp, (rec.id START_ID)* sizeof(rec), SEEK_SET); fwrite(&rec, sizeof(rec), 1, fp); } fclose(fp); exit(0); 43

stquery.c #include <stdio.h> #include "student.h" int main(int argc, char *argv[]) { struct student rec; char c; int id; FILE *fp; if (argc!= 2) { fprintf(stderr, " : %s \n", argv[0]); exit(1); } if ((fp = fopen(argv[1], "rb")) == NULL ) { fprintf(stderr, " \n"); exit(2); } 44

stquery.c do { printf(" : "); if (scanf("%d", &id) == 1) { fseek(fp, (id - START_ID) *sizeof(rec), SEEK_SET); if ((fread(&rec, sizeof(rec), 1, fp) > 0) && (rec.id!= 0)) printf(" : %8d : %4s : %4d\n", rec.id, rec.name, rec.score); else printf(" %d \n", id); } else printf(" "); printf("?(y/n)"); scanf(" %c", &c); } while (c == 'Y'); } fclose(fp); exit(0); 45

(1) (2) (3). 46

stupdate.c #include <stdio.h> #include "student.h" /*. */ int main(int argc, char *argv[]) { struct student rec; int id; char c; FILE *fp; if (argc!= 2) { fprintf(stderr, " : %s \n", argv[0]); exit(1); } if ((fp = fopen(argv[1], "rb+")) == NULL) { fprintf(stderr, " \n"); exit(2); } 47

stupdate.c do { printf(" : "); if (scanf("%d", &id) == 1) { fseek(fp, (id - START_ID) * sizeof(rec), SEEK_SET); if ((fread(&rec, sizeof(rec), 1, fp) > 0)&&(rec.id!= 0)) { printf(" : %8d : %4s : %4d\n", rec.id, rec.name, rec.score); printf(" : "); scanf("%d", &rec.score); fseek(fp, -sizeof(rec), SEEK_CUR); fwrite(&rec, sizeof(rec), 1, fp); } else printf(" %d \n", id); } else printf(" \n"); printf("?(y/n)"); scanf(" %c",&c); } while (c == 'Y'); fclose(fp); exit(0); } 48

49 7.5

C C I/O read (), write () I/O C (fully buffered) (line buffered) (unbuffered) 50

C I/O (newline) I/O (stdin, stdout). (stderr) 51

buffer.c #include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]) { FILE *fp; if (!strcmp(argv[1], "stdin")) { fp = stdin; printf(" :"); if (getchar() == EOF) perror("getchar"); } else if (!strcmp(argv[1], "stdout")) fp = stdout; else if (!strcmp(argv[1], "stderr")) fp = stderr; else if ((fp = fopen(argv[1], "r")) == NULL) { perror("fopen"); exit(1); } else if (getc(fp) == EOF) perror("getc"); 52

buffer.c printf(" = %s, ", argv[1]); if (fp->_flags & _IO_UNBUFFERED) printf(" "); else if (fp->_flags & _IO_LINE_BUF) printf(" "); else printf(" "); } printf(", = %d\n", fp->_io_buf_end - fp->_io_buf_base); exit(0); 53

setbuf()/setvbuf() #include <stdio.h> void setbuf (FILE *fp, char *buf ); int setvbuf (FILE *fp, char *buf, int mode, size_t size );, 54

setbuf() void setbuf (FILE *fp, char *buf ); on/off. buf NULL buf BUFSIZ / 55

setvbuf() int setvbuf (FILE *fp, char *buf, int mode, size_t size ); : 0, nonzero mode _IOFBF : _IOLBF : _IONBF : 56

setvbuf() mode == _IONBF buf size mode == _IOLBF or _IOFBF buf NULL buf size NULL stat st_blksize (disk files) st_blksize BUFSIZ (pipes) 57

:setbuf #include <stdio.h> main() { printf(", "); sleep(1); printf("!"); sleep(1); printf(" \n"); sleep(1); setbuf(stdout, NULL); printf(", "); sleep(1); printf(" "); sleep(1); printf(" ^^"); sleep(1); printf(" \n"); sleep(1); } 58

:setvbuf #include <stdio.h> int main( void ) { char buf[1024]; FILE *fp1, *fp2; fp1 = fopen("data1", "a"); fp2 = fopen("data2", "w"); if( setvbuf(fp1, buf, _IOFBF, sizeof( buf ) )!= 0 ) printf(" : \n" ); else printf(" : 1024 \n" ); } if( setvbuf(fp2, NULL, _IONBF, 0 )!= 0 ) printf(" : \n" ); else printf(" : \n" ); 59

fflush() #include <stdio.h> int fflush (FILE *fp); fp write() : 0, EOF (-1) fp NULL, 60

. fopen() FILE. FILE. fgetc() fputc(). fgets() fputs(). fread() fwrite().. fseek(). 61