准确地说,我正在尝试从文件 A 复制到文件 B,每个单词中都没有字母“e”和“t”(the、peter 等),程序运行良好,但在outfile 的结尾我得到了一个奇怪的迹象。
Input: What says Lucentio to this shame of ours?
Output: What says to this shame of ours?˙
(你能看到角色˙
吗?)
我不想要它,我不知道那是什么,但它不是 EOF,我试图将其排除在复制之外,但它不起作用。我需要一些帮助。
代码:
char signHold[1];
int main(int *argc, char** argv)
{
FILE* infile;
FILE* outfile;
char* string = NULL;
if(argc != 3)
{
printf(stderr,"Error: Improper number of arguments");
return EXIT_FAILURE;
}
remove(argv[2]);
infile = fopen(argv[1],"r");
while(feof(infile) == 0)
{
string = getWord(infile);
if(checkDenied(string))
addToFile(outfile, argv[2], string);
addToFile(outfile, argv[2], signHold);
}
fclose(infile);
free(string);
return EXIT_SUCCESS;
}
char* getWord(FILE* ptr)
{
char* tempString;
size_t memSize = 0;
int c;
tempString = expandRealloc(NULL,sizeof(char));
while(c = fgetc(ptr))
{
if(isalpha(c) != 0)
{
tempString = expandRealloc(tempString, (memSize+1)*sizeof(char)+1);
tempString[memSize] = c;
memSize++;
}
else
{
signHold[0] = c;
break;
}
}
tempString[memSize] = '\0';
return tempString;
}
short int checkDenied(const char* str)
{
int i;
i = strspn("e", str);
if(i >= 1)
{
i = strspn("t", str);
if(i >= 1)
{
return EXIT_SUCCESS;
}
}
return EXIT_FAILURE;
}
short int addToFile(FILE* ptr, char* directory, char* text)
{
ptr = fopen(directory,"a+");
fprintf(ptr,"%s", text);
fclose(ptr);
return EXIT_SUCCESS;
}