fwrite - fread

This is a discussion on fwrite - fread within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> void build (FILE *fp); void print (FILE *fp); int main (void) { FILE *fp; fp = fopen ...

  1. #1
    Registered User PutoAmo's Avatar
    Join Date
    Mar 2002
    Posts
    72

    fwrite - fread

    Code:
    #include <stdio.h>
    
    void build (FILE *fp);
    
    void print (FILE *fp);
    
    int main (void)
    {
     FILE *fp;
    
     fp = fopen ("home/me/filename", "w+b");
    
     build (fp);
    
     print (fp);
    
     fclose (fp);
    
     return 0;
    }
    
    
    void build (FILE *fp)
    {
     int i = 0;
    
     while (i++ < 10)
      fwrite (&i, sizeof (int), 1, fp);
    
     return;
    }
    
    
    void print (FILE *fp)
    {
     int i;
    
     rewind (fp);
    
     while (fread (&i, sizeof (int), 1, fp))
      printf ("%d\n", i);
    
     return;
    }
    This program is ment to create a binary file holding integers 0 to 9. It includes two functions.

    I can't see what is wrong with this code.

    Any help appreciated.
    Last edited by PutoAmo; 03-10-2002 at 01:34 PM.

  2. #2
    ggs
    ggs is offline
    C > C++ duders ggs's Avatar
    Join Date
    Aug 2001
    Posts
    435
    works beautifully for me (I'm in windows, however).

    you might want to check if fp is null after calling fopen, though.
    .sect signature

  3. #3
    Registered User PutoAmo's Avatar
    Join Date
    Mar 2002
    Posts
    72


    This was embarrasing.

    You are right. Problem was in the path. I was attempting to create a file that already is a directory name !!

    Well, thanks for suggestion !

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 2d array and fwrite and fread
    By totalnewbie in forum C Programming
    Replies: 14
    Last Post: 01-10-2009, 03:45 PM
  2. fwrite / fread question
    By Takteek in forum C Programming
    Replies: 5
    Last Post: 11-27-2008, 04:18 AM
  3. Weird problem with fwrite() and fread()
    By piote in forum C Programming
    Replies: 2
    Last Post: 11-13-2004, 03:07 PM
  4. buffer type for fread & fwrite
    By daluu in forum C Programming
    Replies: 5
    Last Post: 05-08-2003, 07:57 PM
  5. fread() and fwrite() ?
    By Limblet in forum C Programming
    Replies: 4
    Last Post: 09-25-2001, 08:36 PM

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