U E D R , A S I H C RSS

새싹교실/2020/새싹부터나무까지/민기범/실습/20.11.06

두 파일 동시에 열기

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

int main() {

	char str[100];
	FILE* pf1, * pf2;
	
	pf1 = fopen("input.txt", "r");
	pf2 = fopen("output.txt", "w");

	if (pf1 == NULL) perror("파일 열기 실패\n");
	else {
		printf("파일 열기 성공\n");
		while (feof(pf1) == 0) 
			fputs(fgets(str,sizeof(str),pf1), pf2);
	}

	fclose(pf1);
	fclose(pf2); 

	return 0;
}

두 파일을 각각 열기

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

int main() {

	char str[100][100] = { NULL, };
	FILE* pf1, * pf2;

	pf1 = fopen("input.txt", "r");
	
	if (pf1 == NULL) perror("파일 열기 실패\n");
	else {
		printf("파일 열기 성공\n");
		int i = 0;
		while (feof(pf1) == 0) {
			fgets(str[i], sizeof(str)/sizeof(str[0]), pf1);
			i++;
		}			
	}
	fclose(pf1);


	pf2 = fopen("output.txt", "w");
	if (pf2 == NULL) perror("파일 열기 실패\n");
	else {
		printf("파일 열기 성공\n");
		int i = 0;
		while (str[i][0] != NULL) {
			fputs(str[i], pf2);
			i++;
		}
	}
	fclose(pf2);

	return 0;
}
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-16 14:07:53
Processing time 0.0125 sec