hi everyone,
the below program is yielding error "segmentation fault",when it is run under unix. This is a file copy program using command line arguements.
Without using command line arguement,i mean,taking filenames directly in the program runs successfully. Can anyone tell me what
"segmentation fault" means in unix?
-------------------

#include<stdio.h>
int main(int argc,char *argv[])
{
   FILE *fp,*ft;
   char ch;
   
   if(argc!=3)
   {
       printf("error");
       exit();
   }
   fp=fopen("argv[1]","rb");
   ft=fopen("argv[2]","wb");
  
   while(!feof(fp))
   {
         ch=fgetc(fp);
         fputc(ch,ft);
   }
   fclose(fp);
   fclose(ft);
 return 0;
}

The next bug is at line 15 -- feof() doesn't work the way you think. Instead, you should code it like this (which will only work with text files. binary files are coded differently because the EOF character could be a valid character in the file.):

while( (ch=fgetc(fp)) != EOF)
{
   fputc(ch,ft);
}

Another bug: failing to check that the two files were successfully opened. If the source file in argv[1] doesn't exist then no point attempting go open the output file and copy. And if the output file can not be created pointer ft will be NULL and the copy loop will fail causing segment violation.

thank you very much. I successfully ran that program in all compilers-unix,tc,vc++.The real bug was-i took double quote "" to argv[1] and argv[2].Although there was no compiler error,but
both files didn't open successfully

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.