본문 바로가기
728x90
반응형
SMALL

전체 글380

chapter 18) 파일의 형태와 개방 모드가 다른 경우 #include int main(void){ FILE *fp; int ary[10] = {5,2,3,4,1,6,7,8,9,10}; int i, res; fp = fopen("a.txt", "wb"); for (i = 0; i< 10; i++){ fputc(ary[i], fp); } fclose(fp); fp = fopen("a.txt", "rt"); while(1){ res = fgetc(fp); if (res == EOF) break; printf("%4d", res); } fclose(fp); return 0; } ### 파일 쓰기 - `FILE *fp;`는 파일을 가리키는 포인터 `fp`를 선언합니다. - `int ary[10] = {5,2,3,4,1,6,7,8,9,10};`는 파일에 쓸 데이터를 .. 2024. 10. 15.
chapter 18) 파일의 내용을 화면에 출력하기 #include int main(void){ FILE *fp; int ch; fp = fopen("a.txt","r"); if (fp == NULL){ printf("do not open the file.\n"); return 1; } while(1){ ch = fgetc(fp); if (ch == EOF){ break; } putchar(ch); } fclose(fp); return 0; } ### 프로그램의 주요 부분 - `FILE *fp;`는 파일을 다루기 위한 파일 포인터 `fp`를 선언합니다. - `fp = fopen("a.txt", "r");`를 통해 "a.txt" 파일을 읽기 모드(`"r"`)로 엽니다. 파일이 성공적으로 열리면, `fp`는 열린 파일을 가리키게 됩니다. 파일을 열 수 없으면.. 2024. 10. 14.
chapter 18) 파일을 열고 닫는 프로그램 #include int main(void){ FILE *fp; fp = fopen("a.txt", "r"); if (fp ==NULL){ printf("do not open the file.\n"); return 1; } printf("open File.\n"); fclose(fp); return 0; } ### 프로그램의 주요 부분 설명 - `FILE *fp;`는 파일 작업을 위한 파일 포인터 `fp`를 선언합니다. - `fp = fopen("a.txt", "r");`는 현재 디렉토리에서 "a.txt"라는 이름의 파일을 읽기 모드(`"r"`)로 엽니다. 파일 열기가 성공하면, `fp`는 열린 파일에 대한 포인터를 가지게 되며, 파일 열기가 실패하면 `fp`는 `NULL`을 가지게 됩니다. - `if (.. 2024. 10. 13.
chapter 18) 여러 줄의 문장을 입력하여 한 줄로 출력 #include #include int main(void){ FILE *ifp, *ofp; char str[80]; char *res; ifp = fopen("a.txt", "r"); if (ifp == NULL){ printf("do not make the file.\n"); return 1; } ofp = fopen("b.txt", "w"); if (ofp == NULL){ printf("do not make the file.\n"); return 1; } while(1){ res = fgets(str, sizeof(str), ifp); if (res == NULL) break; str[strlen(str) - 1] = '\0'; fputs(str, ofp); fputs(" ", ofp); } fcl.. 2024. 10. 12.
728x90
반응형
LIST