반응형
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// 문제 두 파일 비교하기

void CreateFilesNComp(char* fileName1,char* fileName2)
{
	FILE* file1 = fopen(fileName1, "wt");
	FILE* file2 = fopen(fileName2, "wt");
	if (file1 == NULL || file2 == NULL)
	{
		printf("파일을 열 수 없습니다.\n");
		return -1;
	}

	char words1[100];
	char words2[100];

	printf("d1파일 텍스트 입력 : ");
	fgets(words1, sizeof(words1), stdin);
	printf("d2파일 텍스트 입력 : ");
	fgets(words2, sizeof(words2), stdin);

	fprintf(file1, "%s", words1);
	fprintf(file2, "%s", words2);
	//fgets(words1, sizeof(words1), file1);
	//fgets(words2, sizeof(words2), file2);

	fclose(file1);
	fclose(file2);
	file1 = NULL;
	file2 = NULL;
}

void Comp(char* fileName1, char* fileName2)
{
	FILE* file1 = fopen(fileName1, "rt");
	FILE* file2 = fopen(fileName2, "rt");
	if (file1 == NULL || file2 == NULL)
	{
		printf("파일을 열 수 없습니다.\n");
		return -1;
	}
	char string1[100];
	char string2[100];

	fscanf(file1, "%s", string1);
	fscanf(file2, "%s", string2);

	fclose(file1);
	fclose(file2);
	file1 = NULL;
	file2 = NULL;

	for (int i = 0; i < sizeof(string1); i++)
	{
		if (string1[i] != string2[i])
		{
			printf("두 개의 파일은 일치하지 않습니다!\n");
			return 0;
		}
	}

	printf("두 개의 파일은 완전히 일치합니다!\n");

}

int main()
{
	char* fileName1 = "d1.txt";
	char* fileName2 = "d2.txt";

	CreateFilesNComp(fileName1, fileName2);
	Comp(fileName1, fileName2);
    
    return 0;
}

 

반응형

 

반응형

'연습문제' 카테고리의 다른 글

C 파일 입출력 문제1  (0) 2023.05.24
C 복소수 구하기  (0) 2023.05.24
C언어 주사위 굴리기  (0) 2023.05.22
C언어 달팽이 정렬  (0) 2023.05.22
C언어 90도씩 회전하기  (0) 2023.05.22

+ Recent posts