은 내가 당신의 질문을 이해 생각하고, 솔루션이 모두 인쇄 기능에 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;
}
주의 해 주십시요입니다. –
"C#"가 아닌 "C"로 질문에 태그를 지정하십시오. C#은 C 언어가 아닙니다 – Gun
"파일 작성"과 관련된 모든 것이 분명히 없습니다. 그것에 대해 당신은 완전히 어둠에 있습니까? – usr2564301