我需要从两个不同的文件中读取并将单词打印到第三个文件中,首先我从file1中读取并将第一个单词保存到第三个文件中,然后从file2中读取并将第一个单词保存到第三个文件中作为第二个单词,以此类推...来自file1的单词在单词前面有"+",而来自file2的单词在单词前面有"-"。我的问题是代码没有停止,仍在工作并向第三个文件中打印"。"(很多点)。谢谢
#include<stdio.h>
#include<stdlib.h>
int main(){
FILE *first, *second, *third;
char ch[256],ch1[256],ch2;
int i=1,count;
char space = ' ';
char minus = '-';
char plus = '+';
first=fopen("prvy.txt", "r");
second=fopen("druhy.txt", "r");
third=fopen("treti.txt", "w");
if(first==NULL || second==NULL || third==NULL)
{
printf("error");
exit(1);
}
while (fscanf(first, "%255s", ch) == 1)
{
count++;
}
while (fscanf(second, "%255s", ch) == 1)
{
count++;
}
printf("%d",count);
for(i;i<=count;i++)
{
if(i%2==1)
{
fputc(plus,third);
ch2=fgetc(first);
while(ch2 != EOF || ch2 != ' ')
putc(ch2,third);
ch2=fgetc(first);
}
else if(i%2==0)
{
fputc(minus,third);
ch2=fgetc(second);
while(ch2 != EOF || ch2 != ' ')
putc(ch2,third);
ch2=fgetc(second);
}
fputc(space,third);
}
fclose(first);
fclose(second);
fclose(third);
return 0;
}
count
未初始化。 - yanoprintf("error")
,考虑采用perror("error")
替代 - 它会提供一个可读的消息,告诉你出了什么问题! - Antti Haapala -- Слава Україніcount ++
将添加到那里存在的任何垃圾值,不可能知道您的for
循环将执行多少次。 - yanofgetc
返回一个整型值。 - Antti Haapala -- Слава Україні