fread, fwrite and fclose

This is a discussion on fread, fwrite and fclose within the C Programming forums, part of the General Programming Boards category; Guys, please help me understand what wrong with the below code. I just trying a simple read from an existing ...

  1. #1
    Dragon Rider jas_atwal's Avatar
    Join Date
    Nov 2007
    Location
    India
    Posts
    54

    fread, fwrite and fclose

    Guys, please help me understand what wrong with the below code. I just trying a simple read from an existing file and output it to terminal.

    Code:
    #include<stdio.h>
    
    int main() {
       FILE *filed;
       char *buffer;
    
       filed = fopen("ffile.txt", "r");
       fread(buffer, 64, 5, filed);
       fwrite(buffer, 64, 5, stdout);
       fclose(filed);
          exit(0);
    }
    When i compile and execute the above code I get the following:

    Code:
    Segmentation fault (core dumped)
    Definately its something with syntax of fwrite and fread.. but what?

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    230
    I don't know if this is the problem but are you sure the file is opened successfully? you should validate it first.

  3. #3
    Dragon Rider jas_atwal's Avatar
    Join Date
    Nov 2007
    Location
    India
    Posts
    54
    Guys I am able to find out what it was.... it was syntax in fread and fwrite. I changed the following lines of code and it worked great:

    Code:
    #include<stdio.h>
    
    int main() {
       FILE *filed;
       char *buffer;
    
       filed = fopen("ffile.txt", "r");
       fread(&buffer, 64, 5, filed);
       fwrite(&buffer, 64, 5, stdout);
       fclose(filed);
          exit(0);
    }

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by jas_atwal View Post
    Guys I am able to find out what it was.... it was syntax in fread and fwrite. I changed the following lines of code and it worked great:
    Really? It's not meant to work that well. You probably should, rather, allocate some memory of 5 * 64 for your buffer - right now, you are storing 320 bytes into the location that holds buffer, which is definitely not the right thing to do.

    If you put
    Code:
    char abc[] = "abc";
    after char *buffer;
    and
    Code:
    printf("abc = %s", abc);
    after your fclose, I bet you that you won't get "abc" printed!

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Dragon Rider jas_atwal's Avatar
    Join Date
    Nov 2007
    Location
    India
    Posts
    54
    Quote Originally Posted by matsp View Post
    Really? It's not meant to work that well. You probably should, rather, allocate some memory of 5 * 64 for your buffer - right now, you are storing 320 bytes into the location that holds buffer, which is definitely not the right thing to do.

    If you put
    Code:
    char abc[] = "abc";
    after char *buffer;
    and
    Code:
    printf("abc = &#37;s", abc);
    after your fclose, I bet you that you won't get "abc" printed!

    --
    Mats
    That is an educative input. Thanks Mats!


    And you are correct. I get Segmentation fault as output when i use the above two lines of code.
    Last edited by jas_atwal; 12-13-2007 at 12:04 PM.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,915
    Quote Originally Posted by jas_atwal View Post
    Code:
    #include<stdio.h>
    
    int main() {
       FILE *filed;
       char *buffer;
    
       filed = fopen("ffile.txt", "r");
       fread(&buffer, 64, 5, filed);
       fwrite(&buffer, 64, 5, stdout);
       fclose(filed);
          exit(0);
    }
    That code is even more flawed than the first.
    Do you even have compiler warnings enabled?
    fread and fwrite does not take a char**, they take const char* and char* respectively.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Dragon Rider jas_atwal's Avatar
    Join Date
    Nov 2007
    Location
    India
    Posts
    54
    Quote Originally Posted by Elysia View Post
    That code is even more flawed than the first.
    Do you even have compiler warnings enabled?
    fread and fwrite does not take a char**, they take const char* and char* respectively.
    Ok.. some changes I can think of
    Code:
    #include<stdio.h>
    
    int main() {
       FILE *filed;
       char buffer[3200];
    
       filed = fopen("ffile.txt", "r");
       fread(&buffer, 64, 5, filed);
       fwrite(&buffer, 64, 5, stdout);
       fclose(filed);
    
          exit(0);
    }

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,915
    Better, but you're still passing char** to the functions. Do you have warnings enabled? It should warn you.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Dragon Rider jas_atwal's Avatar
    Join Date
    Nov 2007
    Location
    India
    Posts
    54
    Quote Originally Posted by Elysia View Post
    Better, but you're still passing char** to the functions. Do you have warnings enabled? It should warn you.
    strange I am getting no warnings:

    Code:
    [jaspreet@td189 C]$ gcc -o ffile ffile.c
    [jaspreet@td189 C]$
    how do I check if warnings are enabled or not.?

    and when I use &buffer in fread is it similar to char **? I am confused.
    Last edited by jas_atwal; 12-13-2007 at 03:06 PM.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,915
    People suggest you use -Wall.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Dragon Rider jas_atwal's Avatar
    Join Date
    Nov 2007
    Location
    India
    Posts
    54
    Quote Originally Posted by Elysia View Post
    People suggest you use -Wall.
    I used -Wall. I get a lot of output this time while compiling. But none seems related to char**. I'll appreciate if you will have a look at this.

    Code:
    collect2: ld terminated with signal 11 [Segmentation fault]
    ffile(.data+0x8): In function `__data_start':
    : multiple definition of `__dso_handle'
    /usr/lib/gcc/x86_64-redhat-linux/3.4.6/crtbegin.o(.data+0x0): first defined here
    ffile(.init+0x0): In function `_init':
    : multiple definition of `_init'
    /usr/lib/gcc/x86_64-redhat-linux/3.4.6/../../../../lib64/crti.o(.init+0x0): first defined here
    ffile(.text+0x0): In function `_start':
    : multiple definition of `_start'
    /usr/lib/gcc/x86_64-redhat-linux/3.4.6/../../../../lib64/crt1.o(.text+0x0): first defined here
    ffile(.fini+0x0): In function `_fini':
    : multiple definition of `_fini'
    /usr/lib/gcc/x86_64-redhat-linux/3.4.6/../../../../lib64/crti.o(.fini+0x0): first defined here
    ffile(.got.plt+0x0): multiple definition of `_GLOBAL_OFFSET_TABLE_'
    /usr/lib/gcc/x86_64-redhat-linux/3.4.6/../../../../lib64/crt1.o(.got.plt+0x0): first defined here
    ffile(.rodata+0x0): multiple definition of `_IO_stdin_used'
    /usr/lib/gcc/x86_64-redhat-linux/3.4.6/../../../../lib64/crt1.o(.rodata+0x0): first defined here
    ffile(.data+0x0): In function `__data_start':
    : multiple definition of `__data_start'
    /usr/lib/gcc/x86_64-redhat-linux/3.4.6/../../../../lib64/crt1.o(.data+0x0): first defined here
    /tmp/ccFXIQ0i.o(.text+0x0): In function `main':
    : multiple definition of `main'
    ffile(.text+0xb8): first defined here
    /usr/lib/gcc/x86_64-redhat-linux/3.4.6/../../../../lib64/crt1.o(.dynamic+0x0): multiple definition of `_DYNAMIC'
    ffile(.dynamic+0x0): first defined here

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,915
    I'm not well versed with that compiler. I don't understand much of its errors and warnings and I don't have any experience with Linux either.
    But I don't get why it doesn't complain about char**, since it should.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,761
    > Better, but you're still passing char** to the functions. Do you have warnings enabled? It should warn you.
    1. It isn't a char **, it is a pointer to an array. Given the code in post #7, the type of &buffer is actually char (*)[3020], not char **.

    2. There's no reason why it would ever warn you, because fread/fwrite accept a void* as the first parameter. So long as it's a pointer (of any type), then all would be well.

    Also, 64*5 is 320, not 3020

    > I get a lot of output this time while compiling.
    It looks like you might have forgotten the "-o" option on the command line.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  14. #14
    Dragon Rider jas_atwal's Avatar
    Join Date
    Nov 2007
    Location
    India
    Posts
    54
    Quote Originally Posted by Salem View Post
    > Better, but you're still passing char** to the functions. Do you have warnings enabled? It should warn you.
    1. It isn't a char **, it is a pointer to an array. Given the code in post #7, the type of &buffer is actually char (*)[3020], not char **.

    2. There's no reason why it would ever warn you, because fread/fwrite accept a void* as the first parameter. So long as it's a pointer (of any type), then all would be well.

    Also, 64*5 is 320, not 3020

    > I get a lot of output this time while compiling.
    It looks like you might have forgotten the "-o" option on the command line.
    Thank you Salem

  15. #15
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    You can make your life easier by enabling warnings all of the time . . . .
    Code:
    $ alias gcc='gcc -W -Wall'
    That should work, in bash at least. Then, whenever you type "gcc", you're actually executing "gcc -W -Wall". (You might even want to use "gcc -W -Wall -ansi -pedantic".)

    You can even insert that into your ~/.bashrc so you don't have to type it all of the time . . . .
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fclose slow
    By Rmantingh in forum C Programming
    Replies: 4
    Last Post: 04-08-2010, 12:01 PM
  2. Trouble writing to file using fwrite()
    By yougene in forum C Programming
    Replies: 4
    Last Post: 12-30-2008, 05:13 PM
  3. Replies: 2
    Last Post: 12-26-2008, 04:38 PM
  4. Israeli politics mixed with C
    By ronenk in forum C Programming
    Replies: 4
    Last Post: 10-17-2004, 01:46 PM
  5. fwrite - fread
    By PutoAmo in forum C Programming
    Replies: 2
    Last Post: 03-10-2002, 01:48 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21