Question on streams

Discussion in 'C Programming' started by K S M, Aug 24, 2007.

  1. K S M

    K S M Guest

    The code below causes a run time error. The point it breaks down
    is when I call createLog, is there something wrong with the way I open
    and close the streams. There seems to be no issues with it when I
    compile and run this code under linux but when I use Win NT I get
    a "General Protection Fault" SISEGV error.

    Please help.

    strcpy(&file[0],"backup.txt");

    strcpy(&file2[0], file);

    stream = (void *)malloc(4);
    stream = fopen(&file[0], "r");

    if(!stream){
    printf("File %s NOT FOUND! \n\n\t...Program Terminating...\n", file);
    exit(0);
    }

    quit = 0;

    scanf("%c", &entry);

    if(entry =='s'){
    /* backup is a method that I have written... */
    backup(stream);
    fclose(stream);
    free(stream);

    stream = fopen(&file2[0], "r");

    /* Create Log is a Method that I have written... */

    createLog(stream);
    free(stream);
    printf("\n");

    }
     
    K S M, Aug 24, 2007
    #1
    1. Advertising

  2. K S M

    Default User Guest

    K S M wrote:

    > The code below causes a run time error. The point it breaks down
    > is when I call createLog, is there something wrong with the way I open
    > and close the streams. There seems to be no issues with it when I
    > compile and run this code under linux but when I use Win NT I get
    > a "General Protection Fault" SISEGV error.
    >
    > Please help.


    Help us help you by posting a complete, minimal program that
    demonstrates the problem.

    > strcpy(&file[0],"backup.txt");


    What is file, and how is it defined? We don't know.

    > strcpy(&file2[0], file);


    What is file2, and how is it defined? We don't know.

    > stream = (void *)malloc(4);


    What is stream, and how is it defined? We don't know. Why are you
    allocating four bytes of memory for it?

    > stream = fopen(&file[0], "r");


    This just leaked the four bytes from above. What is it you think that
    accomplished?

    > if(!stream){
    > printf("File %s NOT FOUND! \n\n\t...Program Terminating...\n",
    > file); exit(0);
    > }
    >
    > quit = 0;
    >
    > scanf("%c", &entry);


    What is entry, blah blah.

    > if(entry =='s'){
    > /* backup is a method that I have written... */
    > backup(stream);
    > fclose(stream);
    > free(stream);


    There's likely your problem. You freed something that wasn't allocated
    by malloc() or relatives.





    Brian
     
    Default User, Aug 24, 2007
    #2
    1. Advertising

  3. "K S M" <> wrote in message
    news:...
    > The code below causes a run time error. The point it breaks down
    > is when I call createLog, is there something wrong with the way I open
    > and close the streams. There seems to be no issues with it when I
    > compile and run this code under linux but when I use Win NT I get
    > a "General Protection Fault" SISEGV error.
    >
    > Please help.
    >
    > strcpy(&file[0],"backup.txt");
    >
    > strcpy(&file2[0], file);
    >
    > stream = (void *)malloc(4);
    > stream = fopen(&file[0], "r");
    >
    > if(!stream){
    > printf("File %s NOT FOUND! \n\n\t...Program Terminating...\n", file);
    > exit(0);
    > }
    >
    > quit = 0;
    >
    > scanf("%c", &entry);
    >
    > if(entry =='s'){
    > /* backup is a method that I have written... */
    > backup(stream);
    > fclose(stream);
    > free(stream);
    >
    > stream = fopen(&file2[0], "r");
    >
    > /* Create Log is a Method that I have written... */
    >
    > createLog(stream);
    > free(stream);
    > printf("\n");
    >
    > }
    >


    &file2[0] is strictly correct, but betrays a misunderstanding. Arrays decay
    to pointers when you pass them to functions. Therefore fopen() normally
    takes the name of an array, or a hardcoded string literal.
    Strings are just arrays of chars, with a zero on the end to indicate
    termination.

    I suspect that if you post your declarations of file and file2 we will see
    the problem.

    --
    Free games and programming goodies.
    http://www.personal.leeds.ac.uk/~bgy1mm
     
    Malcolm McLean, Aug 24, 2007
    #3
  4. K S M

    pete Guest

    Default User wrote:

    > What is entry,


    It's an unused keyword in K&R C.

    --
    pete
     
    pete, Aug 25, 2007
    #4
  5. K S M

    Army1987 Guest

    On Fri, 24 Aug 2007 23:14:44 +0200, K S M wrote:

    > The code below causes a run time error. The point it breaks down
    > is when I call createLog, is there something wrong with the way I open
    > and close the streams. There seems to be no issues with it when I
    > compile and run this code under linux but when I use Win NT I get
    > a "General Protection Fault" SISEGV error.
    >
    > Please help.
    >
    > strcpy(&file[0],"backup.txt");
    >
    > strcpy(&file2[0], file);
    >
    > stream = (void *)malloc(4);

    What on hell are you doing? malloc returns a void *, so all what
    that cast do is hide errors due to forgetting to include stdlib.h.
    > stream = fopen(&file[0], "r");

    You don't need those four bytes anymore?
    (Maybe stream is a FILE **, you happen to somehow know that
    sizeof (FILE *) is 4, the void* gets converted to FILE **, but
    then here you need *stream = fopen etc.)
    >
    > if(!stream){
    > printf("File %s NOT FOUND! \n\n\t...Program Terminating...\n", file);
    > exit(0);

    Writing to stdout rather than stderr and returning success in this
    case is quite bizarre.
    > }
    >
    > quit = 0;
    >
    > scanf("%c", &entry);

    What's wrong with getchar()?
    >
    > if(entry =='s'){
    > /* backup is a method that I have written... */
    > backup(stream);
    > fclose(stream);
    > free(stream);

    The original pointer to memory allocated by malloc was
    overwritten. You are trying to free a FILE object set up by fopen
    and possibly made useless or destroyed by fclose. On the
    DeathStation 9000 this would "free" your neurons to die at their
    own will. You meant backup(*stream), fclose(*stream)?


    > stream = fopen(&file2[0], "r");
    >
    > /* Create Log is a Method that I have written... */
    >
    > createLog(stream);
    > free(stream);
    > printf("\n");
    >
    > }


    --
    Army1987 (Replace "NOSPAM" with "email")
    No-one ever won a game by resigning. -- S. Tartakower
     
    Army1987, Aug 25, 2007
    #5
    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. Pawel Banys
    Replies:
    1
    Views:
    496
    Joe Smith
    Jul 8, 2004
  2. Jackie

    question about streams

    Jackie, Apr 28, 2006, in forum: Java
    Replies:
    6
    Views:
    446
  3. laniik
    Replies:
    4
    Views:
    414
    laniik
    Apr 8, 2005
  4. John Simeon

    Basic question on streams

    John Simeon, Jul 22, 2007, in forum: C++
    Replies:
    16
    Views:
    558
    Marcus Kwok
    Jul 25, 2007
  5. grocery_stocker

    closures and streams question

    grocery_stocker, Feb 5, 2009, in forum: Perl Misc
    Replies:
    3
    Views:
    116
    Tad J McClellan
    Feb 5, 2009
Loading...

Share This Page