Why does this C program crash on compilation?

rajsujayks

CSE Freak!
This program is supposed to read a list of numbers from two files and combine and sort the lists and generate an output file with it. The program is to be invoked from the command line, with the syntax:

X:\> prg_name file1.txt file2.txt file3.txt

where prg_name is the name of the program, file1.txt and file2.txt are the input files and file3.txt is the output file.

The code is:
Code:
//Merging lists
#include <stdio.h>
int main(int argc,char *argv[])
{
    FILE *f1,*f2,*out;
    int m[100],n[100],num[100];
    int i=0,j=0,k=0,l=0;
    int x=0,temp;

    //Opening input files...
    f1=fopen(argv[1],"r");
    f2=fopen(argv[2],"r");
    if((f1==NULL)||(f2==NULL))
    {
        printf("\nEither one or both of the input files are corrupt or does not exist or cannot be opened!");
        printf("\nProgram execution incomplete. Aborting...");
        return 0;
    }
    for(x=1;x<argc;x++)
    {
        if(x==1)
        {
            while(feof(f1)==0)
            {
                fscanf(f1,"%d",&m[i]);
                i++;
            }
        }
        if(x==2)
        {
            while(feof(f2)==0)
            {
                fscanf(f2,"%d",&n[j]);
                j++;
            }
        }
    }

    //Closing input files...
    fclose(f1);
    fclose(f2);

    //Merging arrays...
    for(k=0;k<i;k++)
    {
        num[k]=m[k];
    }
    for(l=0;l<j;l++)
    {
        num[k+l]=n[l];
    }

    //Sorting merged array...
    for(i=0;i<k+l;i++)
    {
        for(j=0;j<k+l;j++)
        {
            if(num[i]<num[j])
            {
                temp=num[i];
                num[i]=num[j];
                num[j]=temp;
            }
        }
    }

    //Creating output file...
    out=fopen(argv[3],"w");
    if(out==NULL)
    {
        printf("\nOutput file cannot be created!\nProgram execution incomplete. Aborting...");
        return 0;
    }
    i=0;
    while(i<k+l)
    {
        fprintf(out,"%d\n",num[i]);
        i++;
    }
    fclose(out);
    printf("\nOutput file generated successfully!");
    return 0;
}

This program crashes on compiling with Code::Blocks. Why?

P.S.: I've included a sample of the expected input and output files for your reference..
 
Last edited:

nims11

BIOS Terminator
Code:
while(feof(out)==0)
    {
        fprintf(out,"%d\n",num[i]);
        i++;
    }

why are you detecting EOF in an file opened for "writing"? this is what is causing the error.
replace it with.

Code:
while(i<k+l)
    {
        fprintf(out,"%d\n",num[i]);
        i++;
    }

this should solve your problem. :)
 
OP
rajsujayks

rajsujayks

CSE Freak!
Changed that... But still it doesn't seem to compile properly.. It still crashes... Upon trial and error, if I comment out all statements from f2=fopen(argv[2],"r");, it compiles without crashing...So the problem must lie there...But what is it..??
 

nims11

BIOS Terminator
^^ hey its working absolutely fine after making that change i suggested and there is no problems with the fopen() statements. i too used codeblocks with mingw32 compiler. you passed the arguments correctly.
take a look, i did it again for you.
*img818.imageshack.us/img818/7585/mergek.jpg

Uploaded with ImageShack.us

you passed the arguments correctly?
 

sakumar79

Technomancer
Also, I would recommend that the output file is opened and checked along with f1 and f2. If it is invalid, all the sorting will be wasted...

Arun
 
OP
rajsujayks

rajsujayks

CSE Freak!
^^ hey its working absolutely fine after making that change i suggested and there is no problems with the fopen() statements. i too used codeblocks with mingw32 compiler. you passed the arguments correctly.
take a look, i did it again for you.
*img818.imageshack.us/img818/7585/mergek.jpg

Uploaded with ImageShack.us

you passed the arguments correctly?

Lots of thanks..! :)

I'm a dud... I had been using the old build (the one I did before you corrected my program..) to check up... Hee..hee.. :mrgreen: Now I got it.. :) Thank You Again...

Also, I would recommend that the output file is opened and checked along with f1 and f2. If it is invalid, all the sorting will be wasted...

Arun

Thanks.. :)
 
Top Bottom