I need to read from 2 different files and print words to third one, first i read from file1 and save first word to file third, then read from file2 and save first word from here to third file as second word and so on.. and also words from file1 have "+" at the start of the word and from file2 have "-". My problem is that code doesnt stop and is still working and printig to third file "."(lot of dots). thanks
#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;
}