Forum     

Go Back   Digit Technology Discussion Forum > Software > Programming
Register FAQ Calendar Mark Forums Read

Programming The destination for developers - C, C++, Java, Python and the lot


Reply
 
LinkBack Thread Tools Display Modes
Old 09-07-2011, 10:32 PM   #1 (permalink)
CSE Freak!
 
rajsujayks's Avatar
 
Join Date: Aug 2008
Location: Coimbatore
Posts: 81
Exclamation Why does this C program crash on compilation?


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..
Attached Files
File Type: txt file1.txt (15 Bytes, 4 views)
File Type: txt file2.txt (23 Bytes, 4 views)
File Type: txt file3.txt (40 Bytes, 2 views)
__________________
Spoiler:
Dell Inspiron 530 desktop | Intel C2Q Q6600 @ 2.4 Ghz | 2GB DDR2 667Mhz | ASUS ENGTS450 | 250GB SATA2 | Windows 7 Ultimate 64-bit | 350 watt PSU | HP Deskjet F4488 AllInOne | Nokia 6681 Sony Ericsson XPERIA MiniPro (Love you Nokia..! :-( Am sorry..)

Last edited by rajsujayks; 10-07-2011 at 03:40 PM. Reason: Suggestion by nims11 and reasoning on my part...
rajsujayks is offline   Reply With Quote
Advertisements. Register and be a member of the community to get rid of them.
Advertisement

Old 10-07-2011, 08:46 AM   #2 (permalink)
BIOS Terminator
 
nims11's Avatar
 
Join Date: Apr 2008
Location: Ranchi
Posts: 816
Default Re: Why does this C program crash on compilation?

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.
nims11 is online now   Reply With Quote
Old 10-07-2011, 03:39 PM   #3 (permalink)
CSE Freak!
 
rajsujayks's Avatar
 
Join Date: Aug 2008
Location: Coimbatore
Posts: 81
Default Re: Why does this C program crash on compilation?

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..??
__________________
Spoiler:
Dell Inspiron 530 desktop | Intel C2Q Q6600 @ 2.4 Ghz | 2GB DDR2 667Mhz | ASUS ENGTS450 | 250GB SATA2 | Windows 7 Ultimate 64-bit | 350 watt PSU | HP Deskjet F4488 AllInOne | Nokia 6681 Sony Ericsson XPERIA MiniPro (Love you Nokia..! :-( Am sorry..)
rajsujayks is offline   Reply With Quote
Old 10-07-2011, 03:54 PM   #4 (permalink)
BIOS Terminator
 
nims11's Avatar
 
Join Date: Apr 2008
Location: Ranchi
Posts: 816
Default Re: Why does this C program crash on compilation?

^^ 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.
Spoiler:



Uploaded with ImageShack.us


you passed the arguments correctly?
nims11 is online now   Reply With Quote
Old 10-07-2011, 04:29 PM   #5 (permalink)
Human Spambot
 
Join Date: Nov 2004
Location: Madurai
Posts: 2,349
Default Re: Why does this C program crash on compilation?

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
sakumar79 is offline   Reply With Quote
Old 10-07-2011, 05:01 PM   #6 (permalink)
CSE Freak!
 
rajsujayks's Avatar
 
Join Date: Aug 2008
Location: Coimbatore
Posts: 81
Default Re: Why does this C program crash on compilation?

Quote:
Originally Posted by nims11 View Post
^^ 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.
Spoiler:



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.. Now I got it.. Thank You Again...

Quote:
Originally Posted by sakumar79 View Post
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..
__________________
Spoiler:
Dell Inspiron 530 desktop | Intel C2Q Q6600 @ 2.4 Ghz | 2GB DDR2 667Mhz | ASUS ENGTS450 | 250GB SATA2 | Windows 7 Ultimate 64-bit | 350 watt PSU | HP Deskjet F4488 AllInOne | Nokia 6681 Sony Ericsson XPERIA MiniPro (Love you Nokia..! :-( Am sorry..)
rajsujayks is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


 
Latest Threads
- by Charan
- by Sarath
- by clmlbx

Advertisement




All times are GMT +5.5. The time now is 12:31 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.

Search Engine Optimization by vBSEO 3.3.2