Segmentation fault

F

F?bio Botelho

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);
}
 
M

Michael

F?bio Botelho said:
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
 
V

Villy Kruse

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
 
M

Martin Ambuhl

F?bio Botelho said:
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.
 
A

ac

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 said:
F?bio Botelho said:
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.
 
T

Taran

F?bio Botelho said:
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.
 
K

Keith Thompson

Taran said:
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.
 
P

Peter Nilsson

Keith said:
...
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>
 
P

Peter Shaggy Haywood

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"?
 
T

Taran

Keith said:
Taran said:
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.
San Diego Supercomputer Center <*>
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!
 
G

Grumble

Taran said:
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!

Hard to find? Most compilers will emit a warning.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top