3

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;
}
Rudolph
  • 65
  • 7

1 Answers1

2

Take a look at the two while loops here:

        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);
        }

The first issue is that ch2 != EOF || ch2 != ' ' will always be true. If ch2 is EOF, the first condition is false but the second is true. If ch2 is a space, the second condition will be false and the first will be true. If it is neither, both are true.

You want to check if ch is both not EOF and not a space:

while(ch2 != EOF && ch2 != ' ')

This would work if not for this:

char ch[256],ch1[256],ch2;

You have ch2 defined as a char, while fgetc returns an int. The value EOF is out of range for a char, so you'll never see that value. Change it to int:

char ch[256],ch1[256]
int ch2;

Your while loops also don't have any braces around them. So what you actually have is:

            while(ch2 != EOF || ch2 != ' ') {
                putc(ch2,third);
            }
            ch2=fgetc(first);

ch2 never changes inside of the loop, so you have an infinite loop. Put braces where they belong:

        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);
            }
        }

On top of this, the file pointers for both first and second are at the end of each file before you enter the for loop, so there's nothing to read.

You need to call rewind(first) and rewind(second) before entering this loop.

Finally, count is not initialized. It could contain some randomly large number, so your loop could run many more times than you intend.

Be sure to set it to 0:

int i=1,count=0;
dbush
  • 205,898
  • 23
  • 218
  • 273