새싹교실/2020/새싹부터나무까지/박소은/실습/20.11.06 (rev. 1.3)
두 파일 동시에 열기 ¶
#include <stdio.h>
#include <string.h>
#pragma warning (disable: 4996)
int main(void)
{
FILE* pFile1, * pFile2;
pFile1 = fopen("data.txt", "r");
pFile2 = fopen("new.txt", "w");
char s[100];
if (pFile1 == NULL) perror("오류");
else
{
while (!feof(pFile1))
{
fgets(s, sizeof(s), pFile1);
fputs(s, pFile2);
}
}
fclose(pFile1);
fclose(pFile2);
return 0;
}
두 파일을 각각 열기 ¶
#include <stdio.h>
#include <string.h>
#pragma warning (disable: 4996)
int main(void)
{
FILE* pFile;
pFile = fopen("data.txt", "r");
char str[100][100] = { NULL }; // ???????
int i = 0;
if (pFile == NULL) perror("오류");
else
{
while (!feof(pFile))
{
fgets(str[i], sizeof(str) / sizeof(str[0]), pFile);
i++;
}
}
fclose(pFile);
pFile = fopen("new.txt", "w");
i = 0;
if (pFile == NULL) perror("오류");
else
{
while (str[i][0] != NULL)
{
fputs(str[i], pFile);
i++;
}
}
fclose(pFile);
return 0;
}