出错内容请看代码中的汉字注释。
#include "precompile.h"
Status macro_collect(const char *sourcefile,
struct macro_argument **macro_argument_head,
struct macro_no_argument **macro_no_argument_head)
{
int flag1 = 1;
int flag2 = 1;
FILE *fpa;
FILE *fpb;
char curLine[MAX_LINE];
ma *malhead; /* ma --> struct macro_argument */
ma *maltail;
mna *mnalhead;/* mna -->struct macro_no_argument */
mna *mnaltail;
malhead = (ma *)malloc(sizeof(ma));
maltail = malhead;
mnalhead = (mna *)malloc(sizeof(mna));
mnaltail = mnalhead;
if (NULL == (fpa=fopen(sourcefile, "r")) ||
NULL == (fpb=fopen("temp2.c", "w")))
{
printf("Cannot open the file!\n");
return ERROR;
}
while (TRUE)
{
if (feof(fpa))
{
break; // end of file
}
if (NULL == fgets(curLine, MAX_LINE-1, fpa))
{
printf("fgets error!\n");
return ERROR;
}
delete_blank(curLine);
if (1 == flag1)
{
flag1 = 0;
maltail->next = (ma *)malloc(sizeof(ma));
maltail = maltail->next;
}
if (1 == flag2)
{
flag2 = 0;
mnaltail->next = (mna *)malloc(sizeof(mna));
mnaltail = mnaltail->next;
}
/*
* If macro_collect_line() return SUCCESS, which means the current * line's macro information has been collected successfully, then * continue to check the next line.
* Else return UNSUCCESS, which means the current line has no macro, * then copy the current line to the "temp2.c" directly and continue * to check the next line. */
if (UNSUCCESS == macro_collect_line(curLine, &maltail, &mnaltail,
&flag1, &flag2))
{
printf("%s\n",curLine);/* 此处字符串curLine可以正常输出内容,但是接下来的fputs函数却没有向文件temp2.c中写入该字符串。*/
if (EOF == fputs(curLine, fpb))
{
printf("Write file error!\n");
return ERROR;
}
}
}
fclose(fpa);
fclose(fpb);
(*macro_argument_head) = malhead->next;
(*macro_no_argument_head) = mnalhead->next;
return OK;
}
现在正常了,虽然没有修改代码,但是可以写入文件了。至于之前为什么写不进去,没找到原因。