2015-01-02 3 views
-1

Hei. 서명 추가를 위해 인쇄 할 수있는 문서를 만들고 있습니다. C 프로그램의 출력물에 문서가 있지만 저장하면 최종 출력이 아닌 코드 만 얻습니다. 이 내 코드입니다 : 대신 코드 만의,C 프로그래밍에서 자동으로 출력을 텍스트 파일에 저장하는 방법은 무엇입니까?

#include <stdio.h> 

int main(void) 

{ 

    FILE*pfile=NULL; 

    pfile=fopen("MY_FILE","r"); 

    printf("Digital Forensics Investigation Documentation Evidence Form\n\n"); 
    printf("Section 1: Evidence Item Record\n"); 

    printf("---------------------------------\n"); 

char array1[20][100]={"\nLab reference Number:","\nCase reference Number:","\nItem reference Number:","\nDevice Description:","\nAdditional Information:","\nSection 2: Investigator Details\n\nDevice accepted by:","\nDate and time received:","-------------------------------------------------------------------------","\n\nSignaure:\n","\n\n------------------"}; 

char array2[20][500]={"TDI-DF","DFI-C001-2014-10-108","EI-201","Western Digital external hard drive,1TB","\nBlack case,with a few scratches on the side and chipped corner.\nNo obvious serial number or other distinguising features.\nPower cable and USB cable still attached.\nReceived from CTO organisation.\nIt was attached to a laptop running windows 8.\nThis laptop was powered of at the time this harddrive was removed.\n\n-------------------------------------------------------------------------","Sara Hansen","30th December 2014 at 15:00"}; 
int i; 

for (i=0;i<20;i++) 

{ 

printf("%s %s \n", array1[i], array2[i]); 

} 

fscanf(pfile,"d% d% d%"); 

fclose(pfile); 

    return 0; 
} 

내가 내 출력은 텍스트 문서 asell에 저장하고자합니다.

내 코드에서 누락 된 것이 무엇인지 알아내어 누구라도 적절한 형식의 텍스트 파일에 저장되도록 도와 줄 수 있습니까?

감사합니다.

fprintf(pfile, "Digital Forensics Investigation Documentation Evidence Form\n\n"); 

확실하지 무엇 fscanf 줄 당신에게 :

pfile = fopen("MY_FILE", "w"); 

그런 다음 예를 들어, printf 대신 fprintf를 사용

+0

주의 해 주십시요입니다. –

+0

"C#"가 아닌 "C"로 질문에 태그를 지정하십시오. C#은 C 언어가 아닙니다 – Gun

+0

"파일 작성"과 관련된 모든 것이 분명히 없습니다. 그것에 대해 당신은 완전히 어둠에 있습니까? – usr2564301

답변

2

당신은 쓸 수 fopen으로 w 모드를 사용할 필요가 이미 성취하려고 노력하고있다.

+0

array1의 레이아웃을 일치하도록 array2의 정의 수정 제안.scanf 것은 내가 시험해 본 것 중 하나 였고, 거기에있을 예정이 아니 었습니다. 하지만 어쨌든 작동하지 않습니다. : P fprintf를 사용하면 전체 프로그램이 작동을 멈 춥니 다. 하지만 텍스트 파일에 코드를 가져 오는 이유는 무엇입니까? 나는 코드에 쓴 정보 만 원한다. 나는 내 결과를 얻는다. – EMB

+0

@JamesMcLaughlin OP 코드의 모든 파일에 쓸 코드가 없습니다. 명확히 해 주시겠습니까? – user3629249

+0

@ user3629249 printf가 fprintf로 변경 될 것입니다. –

0

은 내가 당신의 질문을 이해 생각하고, 솔루션이 모두 인쇄 기능에 printf 호출을 래핑하는 것입니다, 파일과 그래서 여기 stdout

#include <stdio.h> 
#include <stdarg.h> 

void vputtext(FILE *file, const char *const format, va_list ap) 
{ 
    vfprintf(stdout, format, ap); 
    vfprintf(file, format, ap); 
} 

void puttext(FILE *file, const char *const format, ...) 
{ 
    va_list ap; 
    va_list copy; 

    va_start(ap, format); 
    va_copy(copy, ap); 
    vputtext(file, format, ap); 
    va_end(ap); 
} 

int main(void) 
{ 
    FILE *pfile; 

    pfile = fopen("MY_FILE", "w"); 
    if (pfile == NULL) 
     return -1; 

    puttext(pfile, "Digital Forensics Investigation Documentation Evidence Form\n\n"); 
    puttext(pfile, "Section 1: Evidence Item Record\n"); 

    puttext(pfile, "---------------------------------\n"); 

    const char *array1[10] = { 
     "\nLab reference Number:", 
     "\nCase reference Number:", 
     "\nItem reference Number:", 
     "\nDevice Description:", 
     "\nAdditional Information:", 
     "\nSection 2: Investigator Details\n\nDevice accepted by:", 
     "\nDate and time received:", 
     "-------------------------------------------------------------------------", 
     "\n\nSignaure:\n","\n\n------------------" 
    }; 

    const char *array2[10] = { 
     "TDI-DF", 
     "DFI-C001-2014-10-108", 
     "EI-201", 
     "Western Digital external hard drive,1TB", 
     "\nBlack case,with a few scratches on the side and chipped corner.\nNo obvious serial number or other distinguising features.\nPower cable and USB cable still attached.\nReceived from CTO organisation.\nIt was attached to a laptop running windows 8.\nThis laptop was powered of at the time this harddrive was removed.\n\n-------------------------------------------------------------------------", 
     "Sara Hansen", 
     "30th December 2014 at 15:00", 
     "", 
     "", 
     "" 
    }; 

    int i; 
    for (i = 0 ; i < 10 ; i++) 
     puttext(pfile, "%s %s\n", array1[i], array2[i]); 
    fclose(pfile); 

    return 0; 
} 

당신은 또 다른 중요한 오류가 발생한 예를 들어 당신의 for 루프를 i > 19까지 반복하고 배열의 20 개 문자열을 모두 초기화하지 않은 경우 해당 값을 printf으로 전달할 때 정의되지 않은 동작입니다.

또한 파일을 열고 "r"을 읽고 파일에 씁니다. 파일을 덮어 쓰려면 "w"을 추가하고 추가하려면 "a"을 사용하십시오.

이제 오류 발생 가능성이 적은 작업을 수행하는 방법을 제안 했으므로이 방법을 사용하면 크기가 다른 위험이 없으며 적절한 수를 추가 할 수 있습니다. C 및 C#은 완전히 다른 언어입니다 - 태그를 지정할 때 모든

에서 코드를 수정하지 않고 마지막 {NULL, NULL} <- sentinel 전에 배열 요소는 여기가

#include <stdio.h> 
#include <stdarg.h> 

void vputtext(FILE *file, const char *const format, va_list ap) 
{ 
    vfprintf(stdout, format, ap); 
    vfprintf(file, format, ap); 
} 

void puttext(FILE *file, const char *const format, ...) 
{ 
    va_list ap; 
    va_list copy; 

    va_start(ap, format); 
    va_copy(copy, ap); 
    vputtext(file, format, ap); 
    va_end(ap); 
} 

struct KeyValue 
{ 
    const char *key; 
    const char *value; 
}; 

int main(void) 
{ 
    FILE *pfile; 
    int i; 

    pfile = fopen("MY_FILE", "w"); 
    if (pfile == NULL) 
     return -1; 

    puttext(pfile, "Digital Forensics Investigation Documentation Evidence Form\n\n"); 
    puttext(pfile, "Section 1: Evidence Item Record\n"); 

    puttext(pfile, "---------------------------------\n"); 

    const struct KeyValue entries[] = { 
     {"\nLab reference Number:", "TDI-DF"},  
     {"\nCase reference Number:", "DFI-C001-2014-10-108"}, 
     {"\nItem reference Number:", "EI-201"}, 
     {"\nDevice Description:", "Western Digital external hard drive,1TB"}, 
     {"\nAdditional Information:", "\nBlack case,with a few scratches on the side and chipped corner.\nNo obvious serial number or other distinguising features.\nPower cable and USB cable still attached.\nReceived from CTO organisation.\nIt was attached to a laptop running windows 8.\nThis laptop was powered of at the time this harddrive was removed.\n\n-------------------------------------------------------------------------"}, 
     {"\nSection 2: Investigator Details\n\nDevice accepted by:", "Sara Hansen"}, 
     {"\nDate and time received:", "30th December 2014 at 15:00"}, 
     {"-------------------------------------------------------------------------", ""}, 
     {"\n\nSignaure:\n", ""}, 
     {"\n\n------------------", ""}, 
     {NULL, NULL} 
    }; 

    for (i = 0 ; entries[i].key != NULL ; ++i) 
     puttext(pfile, "%s %s\n", entries[i].key, entries[i].value); 
    fclose(pfile); 

    return 0; 
} 
+0

모든 해답을 가져 주셔서 감사합니다. 내가 그것을 원했던 것에 따라 그것은 작고 약간의 변경 후 그것이 작용한다! printf에서 fprintf로 바뀌었고 마지막에 pfile을 추가했습니다 ... fprintf (pfile, "% s % s \ n"); 이제 문서에 있습니다. 대단히 감사합니다! :) – EMB

+0

@EMB 답변을 수락 할 수 있습니다 [this] (http://stackoverflow.com/tour) –

관련 문제