『 1~100을 이용한 알고리즘의 이해 2: C언어편 』
저자: 김득수
사용 프로그램: Visual Studio 2019
※ 본 게시글에 작성된 문제는 책 본문과 일치하지 않는 부분이 존재하며, 풀이 또한 정확하지 않습니다.
14-1. 1~10까지 10개의 숫자데이터를 "c:\t.txt" 파일에 쓰기 하는 순서도를 완성하시오.
문제풀이
#include <stdio.h>
void main()
FILE* fp;
int i;
// 파일명 지정 시 주의할 점
// 1. 경로에 띄어쓰기가 들어가지 않았는지 확인
// 2. 경로에 \\는 두 번씩
fp = fopen("C:\\t.txt", "wt");
if (fp == NULL) {
printf("open error");
}
for (i = 1; i <= 10; i++) {
fprintf(fp, "%d \n", i);
}
printf("file write end");
fclose(fp);
}
<에러코드 C4996 해결방법>
1. #define _CRT_SECURE_NO_WARNINGS 추가
2. fopen 대신 fopen_s 사용
└ fopen_s(&fp, "text.txt", "w");
※ 2번의 경우, FILE 선언과 fopen_s 를 한 번에 정의할 수 없음! (fp = fopen_s~ 불가)
※ 이를 전부 시도했음에도 파일 저장에 오류가 생긴다면, 실행 중인 프로그램(ex. 비주얼 스튜디오)등을 종료 후 관리자 권한으로 재실행 (C 드라이브 저장에 관리자 권한이 필요)
실행결과
file write end
14-2. 연습문제 14.1에서 생성한 "c:\t.txt"의 10개 데이터를 읽어 화면에 출력하는 순서도를 완성하시오.
문제풀이
#include <stdio.h>
#include <io.h>
#include <stdlib.h>
void main() {
FILE* fp;
int a;
fp = fopen_s(&fp, "c:\\t.txt", "rt"); // ※ fopen_s 는 앞에 fp = 부분을 붙이면 X
if (fp == NULL) {
printf("open error");
}
while (fscanf(fp, "%d", &a) != EOF) {
printf("%d \n", a);
}
printf("file read end");
fclose(fp);
}
오답풀이
#include <stdio.h>
#include <io.h>
#include <stdlib.h>
void main() {
FILE* fp;
int a;
fopen_s(&fp, "c:\\t.txt", "rt");
if (fp == NULL) {
printf("open error");
}
while (fscanf_s(fp, "%d", &a) != EOF) {
printf("%d \n", a);
}
printf("file read end");
fclose(fp);
}
실행결과
1
2
3
4
5
6
7
8
9
10
file read end
14-3.
연습문제 14.1에서 생성한 "c:\t.txt"에 11~20까지 숫자 10개를 추가한 후 다시 이 파일을 읽어 화면에 출력하는 순서도를 완성하시오.
문제풀이
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <io.h>
#include <stdlib.h>
void main() {
FILE* fp;
int i;
// ※ int a 선언이 빠짐
fp = fopen("c:\\t.txt", "at");
if (fp == NULL) {
printf("open error");
}
for (i = 11; i <= 20; i++) {
fprintf(fp, "%d \n", i);
}
printf("file append end \n");
fclose(fp);
fp = fopen("c:\\t.txt", "rt");
if (fp == NULL) {
printf("open error - rt");
}
while (fscanf(fp, "%d", &a) != EOF) {
printf("%d ", a);
}
printf("\n");
printf("append file read end");
fclose(fp);
}
오답풀이
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <io.h>
#include <stdlib.h>
void main() {
FILE* fp;
int i, a;
fp = fopen("c:\\t.txt", "at");
if (fp == NULL) {
printf("open error");
}
for (i = 11; i <= 20; i++) {
fprintf(fp, "%d \n", i);
}
printf("file append end \n");
fclose(fp);
fp = fopen("c:\\t.txt", "rt");
if (fp == NULL) {
printf("open error - rt");
}
while (fscanf(fp, "%d", &a) != EOF) {
printf("%d ", a);
}
printf("\n");
printf("append file read end");
fclose(fp);
}
실행결과
file append end
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
append file read end
14-4.
2~21까지 데이터에서 홀수 항을 차례로 "c:\t1.txt" 파일에 순차쓰기하고 짝수 항을 차례로 "c:\t2.txt" 파일에 순차쓰기를 한 후 2개 파일의 내용을 "c:\t12.txt"파일에 병합을 하는 순서도를 완성하시오.
문제풀이
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <io.h>
#include <stdlib.h>
void main() {
FILE* fp1, * fp2, * fp12;
int i, a;
fp1 = fopen("c:\\t1.txt", "wt");
fp2 = fopen("c:\\t2.txt", "wt");
if (fp1 == NULL) { printf("t1.txt open error : wt \n"); }
if (fp2 == NULL) { printf("t2.txt open error : wt \n"); }
for (i = 2; i <= 21; i++) {
if (i % 2 == 0) {
fprintf(fp2, "%d \n", i);
}
else {
fprintf(fp1, "%d \n", i);
}
}
printf("2 file wirte end \n");
fclose(fp1), fclose(fp2);
fp1 = fopen("c:\\t1.txt", "rt");
fp2 = fopen("c:\\t2.txt", "rt");
fp12 = fopen("c:\\t12.txt", "wt");
if (fp1 == NULL) { printf("t1.txt open error : rt \n"); }
if (fp2 == NULL) { printf("t2.txt open error : rt \n"); }
if (fp2 == NULL) { printf("t12.txt open error : wt \n"); }
while (fscanf(fp1, "%d", &a) != EOF) {
printf("%d ", a), fprintf(fp12, "%d \n", a);
fscanf(fp2, "%d", &a);
printf("%d ", a), fprintf(fp12, "%d \n", a);
}
printf("\n");
printf("merge file read end \n");
fclose(fp1), fclose(fp2), fclose(fp12);
}
실행결과
2 file wirte end
3 2 5 4 7 6 9 8 11 10 13 12 15 14 17 16 19 18 21 20
merge file read end
'Tycoon > 알고리즘의 이해(2): C언어편 (完)' 카테고리의 다른 글
[알고리즘의 이해(2): C언어편] 연습문제 13장 (0) | 2025.01.03 |
---|---|
[알고리즘의 이해(2): C언어편] 연습문제 12장 (0) | 2025.01.03 |
[알고리즘의 이해(2): C언어편] 연습문제 11장 (0) | 2025.01.03 |
[알고리즘의 이해(2): C언어편] 연습문제 10장 (0) | 2025.01.03 |
[알고리즘의 이해(2): C언어편] 연습문제 9장 (0) | 2025.01.03 |