void fileOpen(char * fname)
{
FILE *txt, *newTxt;
char line[256];
char fileName[256];
txt = fopen(fname, "r");
if(txt == NULL)
{
perror("Error opening file");
exit (EXIT_FAILURE);
}
newTxt = fopen("output.txt", "w");
if(newTxt == NULL)
{
perror("Error opening file");
exit(EXIT_FAILURE);
}
//Problem is in the while loop
while(fgets(line, 256, txt) != NULL)
{
if (strncmp(line, "#include", 7) == 0)
{
strcpy(fileName, extractSubstring(line));
fileOpen(fileName);
}
else
fprintf(newTxt, "%s", line); <---- It just prints over itself
}
fcloseall();
}
프로그램의 핵심은 재귀 적 파일 추출입니다. 줄의 처음에 #include를 볼 때마다 파일의 내용이 인쇄됩니다.fprintf 원하는대로 작동하지 않습니다.
어떤 이유로 든 모든 라인에서 변수 "line"은 자체적으로 씁니다. 대신 파일로 출력하는 것이 아니라 새 줄을 인쇄하여 새 줄을 인쇄하는 것이 좋습니다. 올바르게 사용하고 있습니까?
예 : 에 전달되는 명령 줄 인수 yo.txt
을 사용합니다.
yo.txt
에서 :
Hello stackoverflow.
#include "hi.txt"
Thank you!
hi.txt
에서 :
Please help me.
예상 최종 결과 :
Hello stackoverflow.
Please help me
Thank you!
'fprintf'에'\ n '을 추가 하시겠습니까? – Kninnug
'perror (fname)','perror (fname)','perror (fname)'!!!!! 메시지가 적용되는 파일의 이름을 알려주지 못하는 또 다른 오류 메시지가 나타나면 비명을 지릅니다. –
'output.txt'을'w' 모드로 열었습니다. 그렇게 할 때마다 이전에 작성된 모든 내용을 잘라 버립니다. –