Segmentation fault

Discussion in 'C Programming' started by F?bio Botelho, Apr 4, 2005.

  1. 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);
    }
     
    F?bio Botelho, Apr 4, 2005
    #1
    1. Advertising

  2. F?bio Botelho

    Michael Guest

    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
     
    Michael, Apr 4, 2005
    #2
    1. Advertising

  3. F?bio Botelho

    Villy Kruse Guest

    On 3 Apr 2005 23:55:11 -0700,
    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


    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
     
    Villy Kruse, Apr 4, 2005
    #3
  4. 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.
     
    Martin Ambuhl, Apr 4, 2005
    #4
  5. F?bio Botelho

    ac Guest

    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" <> wrote in message
    news:qR64e.30$...
    > 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.
     
    ac, Apr 4, 2005
    #5
  6. F?bio Botelho

    Taran Guest

    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

    > }
     
    Taran, Apr 4, 2005
    #6
  7. "Taran" <> 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.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Apr 4, 2005
    #7
  8. 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
     
    Peter Nilsson, Apr 5, 2005
    #8
  9. 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"?
     
    Peter Shaggy Haywood, Apr 5, 2005
    #9
  10. F?bio Botelho

    Taran Guest

    Keith Thompson wrote:
    > "Taran" <> 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.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
     
    Taran, Apr 5, 2005
    #10
  11. F?bio Botelho

    Grumble Guest

    Taran wrote:

    > 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.
     
    Grumble, Apr 5, 2005
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alex Hunsley
    Replies:
    17
    Views:
    871
  2. Pud
    Replies:
    0
    Views:
    578
  3. Replies:
    0
    Views:
    532
  4. Ivan Vecerina
    Replies:
    0
    Views:
    488
    Ivan Vecerina
    Jun 29, 2003
  5. Vasileios Zografos

    Re: segmentation fault exception handling

    Vasileios Zografos, Jun 30, 2003, in forum: C++
    Replies:
    5
    Views:
    15,624
    Pete Becker
    Jul 1, 2003
Loading...

Share This Page