Member Avatar for SoreComet

Hi Guys,

I was trying to code in C for the following.

You are given with 2 files, file1.txt and file2.txt. The contents of the file contain INTERGER values. Add the contents of the file and write them to another file file3.txt.

My code is as follows

#include <stdio.h>
#include <stdlib.h>

int main()
{
    static int data,data1,sum;

    FILE *f1,*f2,*f3;
    f1 = fopen("C:\\file1.txt","r");
    f2 = fopen("C:\\file2.txt","r");
    f3 = fopen("C:\\file3.txt","w");

    while(( fscanf(f1,"%d",&data) != EOF ) || ( fscanf(f2,"%d",&data1) != EOF ))
    {
        if(data == EOF && data1 == EOF)
        {
            break;
        }
        else if(data == EOF)
        {
            sum += data1;
        }
        else if(data1 == EOF)
        {
            sum += data;
        }
        else
        {
            sum = sum + data + data1;
        }
    }

    fprintf(f3,"%d\n",sum);

    fclose(f1);
    fclose(f2);
    fclose(f3);

    return 0;
}

Contents of File1 --> 123
Contents of File2 --> 456

But after execution, when I open File3, it has the results as

702

Please help me with the code

P.S : I tried to add the contents without the while loop (i.e)., it can be applied for only one Input on each file and it worked fine. But I want to add the contents of the file that has multiple numbers in it.

Please help me

You seem to think that if the end of file is reached, fscanf will store EOF in the given variable. It won't - it will simply keep the variable unchanged. The only way to tell whether fscanf successfully read a value is to check the return value of fscanf - you can't tell by looking at the variable.

The easiest way to make your code work would be to simply have two loops after each other: One that goes through the first file and one that goes through the second. To avoid code duplication, you should define a function that takes a file as an argument and then sums that file. Then you can simply call that function once with the first file as the argument and once with the second.

commented: Never thought of this! thank you +0

replace while(( fscanf(f1,"%d",&data) != EOF ) || ( fscanf(f2,"%d",&data1) != EOF ))

to while(( fscanf(f1,"%d",&data) != EOF ) && ( fscanf(f2,"%d",&data1) != EOF ))

hope this helps you . . .

That won't actually fix the issue and it will cause another issue: The loop will now quit as soon as one of the files' end has been reached, so if the files contain a different number of entries, it won't read all the numbers.

I know that
if both file will not at EOF then code will work otherwise seperate code will have to write

Member Avatar for SoreComet

@sepp2k Thanks a TON man! I finally got my code working

Here is my code

#include <stdio.h>
#include <stdlib.h>

int getfromfile(FILE **fp)
{
    int data = 0;
    int sum = 0;
    while(fscanf(*fp,"%d",&data) != EOF)
    {
        sum += data;
    }
    return sum;
}

int main()
{
    int data = 0;
    int data1 = 0;

    FILE *f1,*f2,*f3;
    f1 = fopen("C:\\file1.txt","r");
    f2 = fopen("C:\\file2.txt","r");
    f3 = fopen("C:\\file3.txt","w");

    data = getfromfile(&f1);
    data1 = getfromfile(&f2);

    fprintf(f3,"%d",(data+data1));

    fclose(f3);
    fclose(f2);
    fclose(f1);

    return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.