Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Segmentation fault

Reply
Thread Tools

Segmentation fault

 
 
F?bio Botelho
Guest
Posts: n/a
 
      04-04-2005
Sorry About the english....


This program copy one file to another , but when i run it it's
gives-me an error:
Segmentation fault
I don't understand ... because it pass my test : opens the first file
and create the second.
If any one could give me a help I would be apreciated.
thanks.
Fábim

#include <stdio.h>
#include <stdlib.h>
main(int argc, char *argv[])

{
FILE *fp,*fd;
int ch;
if (argc!=3)
{
printf("Sintaxe:\n\n%s ficheiro-fonte ficheiro-destino\n",argv[0]);
exit(1);
}
fp=fopen(argv[1],"rb");

if (fp=NULL)
{printf("Ficheiro: %s Não válido!\n",argv[1]);
exit(2);}

fd=fopen(argv[2],"wb");
if (fd=NULL)
{
printf("Impossivel criar o ficheiro %s \n",argv[2]);
exit(3);
}

while (( ch = fgetc(fd) )!=EOF)
fputc(ch,fd);

fclose(fd);
fclose(fd);
}
 
Reply With Quote
 
 
 
 
Michael
Guest
Posts: n/a
 
      04-04-2005


F?bio Botelho wrote:
> This program copy one file to another , but when i run it it's
> gives-me an error:
> Segmentation fault
> I don't understand ... because it pass my test : opens the first file
> and create the second.

Hi,
well I'm not sure since i'm not an expert, but I guess
you should change
> main(int argc, char *argv[])

to
int main(int argc, char *argv[])
and add a return statement and at the end
> fclose(fd);
> fclose(fd);

you "double-close" the file referred by fd (and leave fp
unclosed).

The error is produced by the if statements:
> if (fp=NULL)

and
> if (fd=NULL)

which don't check the pointers but assign the the NULL pointer.
Properly you want
if (fd==NULL)
to check for errors.

Michael

 
Reply With Quote
 
 
 
 
Villy Kruse
Guest
Posts: n/a
 
      04-04-2005
On 3 Apr 2005 23:55:11 -0700,
F?bio Botelho <(E-Mail Removed)> wrote:


> Sorry About the english....
>
>
> This program copy one file to another , but when i run it it's
> gives-me an error:
> Segmentation fault


Your program are confusing the input and output file, and finaly you
close the same file twice, which on some systems results in a
segmentation fault. Having two variables, fp and fd, so similar
is prone to give this kind of error.

Villy
 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      04-04-2005
F?bio Botelho wrote:
> Sorry About the english....
>
>
> This program copy one file to another , but when i run it it's
> gives-me an error:
> Segmentation fault
> I don't understand ... because it pass my test : opens the first file
> and create the second.
> If any one could give me a help I would be apreciated.
> thanks.
> Fábim
>
> #include <stdio.h>
> #include <stdlib.h>
> main(int argc, char *argv[])


main returns an int. Saying so is good programming practice and
necessary in C99.

>
> {
> FILE *fp,*fd;
> int ch;
> if (argc!=3)
> {
> printf("Sintaxe:\n\n%s ficheiro-fonte ficheiro-destino\n",argv[0]);
> exit(1);


The portably defined arguments for exit() are 0, EXIT_FAILURE, and
EXIT_SUCCESS. 1 is not in that list.

> }
> fp=fopen(argv[1],"rb");
>
> if (fp=NULL)


You just killed the result of fopen(). End of the road. Poof! Bye-bye.
 
Reply With Quote
 
ac
Guest
Posts: n/a
 
      04-04-2005
After you incorporate the following suggestions into your code and it quits
crashing, but still doesn't work. You may want to make sure you are reading
and writing to correct file pointers.


"Martin Ambuhl" <(E-Mail Removed)> wrote in message
news:qR64e.30$(E-Mail Removed) et...
> F?bio Botelho wrote:
> > Sorry About the english....
> >
> >
> > This program copy one file to another , but when i run it it's
> > gives-me an error:
> > Segmentation fault
> > I don't understand ... because it pass my test : opens the first file
> > and create the second.
> > If any one could give me a help I would be apreciated.
> > thanks.
> > Fábim
> >
> > #include <stdio.h>
> > #include <stdlib.h>
> > main(int argc, char *argv[])

>
> main returns an int. Saying so is good programming practice and
> necessary in C99.
>
> >
> > {
> > FILE *fp,*fd;
> > int ch;
> > if (argc!=3)
> > {
> > printf("Sintaxe:\n\n%s ficheiro-fonte ficheiro-destino\n",argv[0]);
> > exit(1);

>
> The portably defined arguments for exit() are 0, EXIT_FAILURE, and
> EXIT_SUCCESS. 1 is not in that list.
>
> > }
> > fp=fopen(argv[1],"rb");
> >
> > if (fp=NULL)

>
> You just killed the result of fopen(). End of the road. Poof! Bye-bye.



 
Reply With Quote
 
Taran
Guest
Posts: n/a
 
      04-04-2005
F?bio Botelho wrote:
> Sorry About the english....
>
>
> This program copy one file to another , but when i run it it's
> gives-me an error:
> Segmentation fault
> I don't understand ... because it pass my test : opens the first file
> and create the second.
> If any one could give me a help I would be apreciated.
> thanks.
> Fábim
>
> #include <stdio.h>
> #include <stdlib.h>
> main(int argc, char *argv[])


main should have return type int.
int main (int argc, char *argv[])

> {
> FILE *fp,*fd;
> int ch;
> if (argc!=3)
> {
> printf("Sintaxe:\n\n%s ficheiro-fonte ficheiro-destino\n",argv[0]);
> exit(1);
> }
> fp=fopen(argv[1],"rb");
>
> if (fp=NULL)


You wanted to compare fp to NULL but actually you are assigning NULL to
fp. Since fp is assigned to Null (0), 'if' fails and skips the if
block.

> {printf("Ficheiro: %s Não válido!\n",argv[1]);
> exit(2);}


cannot exit as this block is skipped.
opens fd.

> fd=fopen(argv[2],"wb");
> if (fd=NULL)


But close fd here.
You wanted to compare fp to NULL but actually you are assigning NULL to
fd. Since fd is assigned to Null (0), 'if' fails and skips the if
block.

> {
> printf("Impossivel criar o ficheiro %s \n",argv[2]);
> exit(3);


cannot exit as this block is skipped

> }
>
> while (( ch = fgetc(fd) )!=EOF)


What? read from a closed stream? fd is closed here. This is where the
execution thorws an exception "reading from a closed file handle"
Segmentation fault!

Even if fd!=NULL, fd is opened for writing only and cannot be read from
You get one more hit.

> fputc(ch,fd);


Reading from a file and writing to it? This overwrites the next
character, assuming the file is opened in RW,read-write mode. This
doesn't make sense. Duplicating every alternate character!

> fclose(fd);
> fclose(fd);


Closing the same file twice.

Aborting explanations: too many errors.

Instead the comparisons should be:
if (fd==NULL)

A better programming practice
if(NULL==fd)
This way even if you miss the second = for equality, you do not
inadverently assign NULL to a pointer which is a bug and is easier to
find in large projects. (ptr=NULL) is difficult to find.

HTH.
---
Taran

> }


 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      04-04-2005
"Taran" <(E-Mail Removed)> writes:
[...]
>> if (fp=NULL)

>
> You wanted to compare fp to NULL but actually you are assigning NULL to
> fp. Since fp is assigned to Null (0), 'if' fails and skips the if
> block.

[...]
> Instead the comparisons should be:
> if (fd==NULL)
>
> A better programming practice
> if(NULL==fd)
> This way even if you miss the second = for equality, you do not
> inadverently assign NULL to a pointer which is a bug and is easier to
> find in large projects. (ptr=NULL) is difficult to find.


That's one solution, but reasonable people differ on whether reversing
the operands of "==" is good style or a horribly ugly crutch.

Personally, I find it ugly, and it makes the code more difficult to
read; "fd==NULL" just looks more natural to me that "NULL==fd". I
know that some people don't have a problem with it.

As a programmer, you'll just need to be able to cope with both styles,
even if one of them annoys you.

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
Peter Nilsson
Guest
Posts: n/a
 
      04-05-2005
Keith Thompson wrote:
> ...
> That's one solution, but reasonable people differ on whether

reversing
> the operands of "==" is good style or a horribly ugly crutch.
>
> Personally, I find it ugly, and it makes the code more difficult to
> read; "fd==NULL" just looks more natural to me that "NULL==fd". I
> know that some people don't have a problem with it.
>
> As a programmer, you'll just need to be able to cope with both

styles,
> even if one of them annoys you.


If only clc regulars could deal with malloc casting in the same vain...
<g>

--
Peter

 
Reply With Quote
 
Peter Shaggy Haywood
Guest
Posts: n/a
 
      04-05-2005
Groovy hepcat F?bio Botelho was jivin' on 3 Apr 2005 23:55:11 -0700 in
comp.lang.c.
Segmentation fault's a cool scene! Dig it!

>This program copy one file to another , but when i run it it's
>gives-me an error:
>Segmentation fault
>I don't understand ... because it pass my test : opens the first file
>and create the second.


>fd=fopen(argv[2],"wb");

^^ ^
....

>while (( ch = fgetc(fd) )!=EOF)

^^
In addition to what other people have told you, you have another
problem. You're trying to read from a file opened for writing here.
You're trying to read the wrong file.

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
 
Reply With Quote
 
Taran
Guest
Posts: n/a
 
      04-05-2005
Keith Thompson wrote:
> "Taran" <(E-Mail Removed)> writes:
> [...]
> >> if (fp=NULL)

> >
> > You wanted to compare fp to NULL but actually you are assigning

NULL to
> > fp. Since fp is assigned to Null (0), 'if' fails and skips the if
> > block.

> [...]
> > Instead the comparisons should be:
> > if (fd==NULL)
> >
> > A better programming practice
> > if(NULL==fd)
> > This way even if you miss the second = for equality, you do not
> > inadverently assign NULL to a pointer which is a bug and is easier

to
> > find in large projects. (ptr=NULL) is difficult to find.

>
> That's one solution, but reasonable people differ on whether

reversing
> the operands of "==" is good style or a horribly ugly crutch.
>
> Personally, I find it ugly, and it makes the code more difficult to
> read; "fd==NULL" just looks more natural to me that "NULL==fd". I
> know that some people don't have a problem with it.
>
> As a programmer, you'll just need to be able to cope with both

styles,
> even if one of them annoys you.
>
> --
> Keith Thompson (The_Other_Keith) (E-Mail Removed)

<http://www.ghoti.net/~kst>
> San Diego Supercomputer Center <*>

<http://users.sdsc.edu/~kst>
> We must do something. This is something. Therefore, we must do

this.

I remember there was a discussion on clc some time back on what is the
correct/accepted way to tests for equality?
var==val or val==var.

Certainly both of them are correct, but I prefer the latter one for it
reduces the chances of making a rather trivial but hard to find error.
Specially when you work late hours!

--
Taran

 
Reply With Quote
 
 
 
Reply

Thread Tools

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 Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Segmentation fault using Firefox 15.0.2 Keith Lee Firefox 3 04-29-2006 05:45 PM
Xerces on Solaris - Segmentation fault ldvmbs@gmail.com XML 0 05-16-2005 07:21 AM
Xerces XML Parser Segmentation fault with Java Pud XML 0 11-06-2003 05:07 PM
Intel Xeon + Linux + IBM sdk 1.3.1 - getting Segmentation fault Alex Hunsley Java 17 11-06-2003 12:12 AM
Re: segmentation fault exception handling Ivan Vecerina C++ 0 06-29-2003 10:56 PM



Advertisments