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