segmentation fault...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include<stdio.h>

int main(int argc,char *argv[]) {
FILE *fp;
 if (argc==3){
   fp=fopen(argv[1],"w+");
   fprintf(fp,"%s\n",argv[2]);
 }
 else {
   printf("%s\n","This utility coded for UNIX/LINUX based systems can be used for setting small files with data in a hurry.The syntax is as fillowes.");
   printf("%s\n","lw filename_to_open data_to_be written.");
   printf("%s\n","The data to be written , i.e. the second argument must be quote delimited if the data contains spaces.");
   printf("%s\n","Coded by Codezer0.Any bugs or further ideas please senf mail to:");
  }
fclose(fp);
return 0;
}

In the above case when i give the 3 arguments required then the program is working fine. Anything other then the 3 args gives me the statement i enterd but with SEGMENTATION FAULT.can someone help me why am i getting this segmentation fault???
Last edited on
line 15: you are closing a file which may not be open
bazzy you are awesome.. you rock man.. what fool of me.. i as trying to close a file that wouldnt have been opened if argc!=3

thanks
Topic archived. No new replies allowed.