Toggle menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

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

From ZeroWiki
No edit summary
(Repair batch-0006 pages from live compare)
 
(One intermediate revision by one other user not shown)
Line 5: Line 5:
  int main() {
  int main() {
   
   
  char str[100];
  char str[100];
  FILE* pf1, * pf2;
  FILE* pf1, * pf2;
 
 
Line 25: Line 25:


== 두 파일을 각각 열기 ==
== 두 파일을 각각 열기 ==
 
#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;
}

Latest revision as of 01:08, 27 March 2026

두 파일 동시에 열기

#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;
}