2011-03-11 4 views

답변

4

C++의 경우에는 herehere이 있습니다.

C의 경우 this reference을 확인하십시오. 그것은 파일을 열고, 그것에 무엇인가를 쓴 다음 그것을 읽습니다. 그것은 당신이 찾고있는 것입니다. 또한 this page is great으로 fopen/fread/fwrite에 대해 자세히 설명합니다.

+0

감사합니다.하지만 C가 아닌 c의 응답이 필요합니다. – duaa

+0

Ok 질문이 잘못되었습니다. 나는 단지 초 만에 갱신 할 것이다. – karlphillip

+0

고맙습니다. – duaa

0

편집 : 코드의 향상된 버전입니다. 당신은 단지 하나의 입력 파일과 하나 개의 출력 파일이있는 경우

#include <stdio.h> 
#include <stdlib.h> 
int main(void) 
{ 
    FILE *fs, *ft; 
    int ch; 
    fs = fopen("pr1.txt", "r"); 
    if (fs == NULL) 
    { 
     fputs("Cannot open source file\n", stderr); 
     exit(EXIT_FAILURE); 
    } 
    ft = fopen("pr2.txt", "w"); 
    if (ft == NULL) 
    { 
     fputs("Cannot open target file\n", stderr); 
     fclose(fs); 
     exit(EXIT_FAILURE); 
    } 
    while ((ch = fgetc(fs)) != EOF) 
    { 
     fputc(ch, ft); 
    } 
    fclose(fs); 
    fclose(ft); 
    exit(EXIT_SUCCESS); 
} 
+0

많은 심각한 문제를 수정하여 코드를 편집했습니다. –

0

, 가장 간단한 방법은 freopen을 사용하는 것입니다

#include <cstdio> 
int main() 
{ 
    freopen("input.txt","r",stdin); 
    freopen("output.txt", "w", stdout); 

    /* Now you can use cin/cout/scanf/printf as usual, 
    and they will read from the files specified above 
    instead of standard input/output */ 
    int a, b; 
    scanf("%d%d", &a, &b); 
    printf("%d\n", a + b); 

    return 0; 
} 
귀하의 질문 및 태그는`c`를 말하지만, 제목은 C++`라고
관련 문제