fread() & fwrite()

This is a discussion on fread() & fwrite() within the C Programming forums, part of the General Programming Boards category; This is my first attempt at using fread() and fwrite(). I am successfully reading one character at a time and ...

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    30

    fread() & fwrite()

    This is my first attempt at using fread() and fwrite(). I am successfully reading one character at a time and trying to replace/write one character at a time to the same file. I can see by the printf statements that the file is opened and reading characters . However, it does not write to the file with the replaced characters.

    Code:
    	for(int i=0; i<200; i++){
    		fread(&bufferIn[i], 1, 1, infile); //<---fread seams to work
    
                            printf("                          %d, %d\n", (unsigned char)bufferIn[i], i); //For testing
    			if ((unsigned char)bufferIn[i] = ....){bufferIn[i] = ...}
    
    		}
    		printf("%d, %d\n", (unsigned char)bufferIn[i], i); //For testing
    		fwrite(&bufferIn[i], 1, 1, infile); //<---fwrite does not seem to work
    	}

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Quote Originally Posted by Roger View Post
    This is my first attempt at using fread() and fwrite(). I am successfully reading one character at a time and trying to replace/write one character at a time to the same file. I can see by the printf statements that the file is opened and reading characters . However, it does not write to the file with the replaced characters.

    Code:
    	for(int i=0; i<200; i++){
    		fread(&bufferIn[i], 1, 1, infile); //<---fread seams to work
    
                            printf("                          %d, %d\n", (unsigned char)bufferIn[i], i); //For testing
    			if ((unsigned char)bufferIn[i] = ....){bufferIn[i] = ...}
    
    		}
    		printf("%d, %d\n", (unsigned char)bufferIn[i], i); //For testing
    		fwrite(&bufferIn[i], 1, 1, infile); //<---fwrite does not seem to work
    	}
    You've omitted 2 major things,
    * How did you open infile?
    * What value does fwrite() return

    I'm assuming "w+", in which case:
    For the modes where both read and writing (or appending) are allowed (those which include a "+" sign), the stream should be flushed (fflush) or repositioned (fseek, fsetpos, rewind) between either a reading operation followed by a writing operation or a writing operation followed by a reading operation.

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    30
    oh geeze, I was fopen ing with "r"!
    Thanks zacks7

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. Replies: 2
    Last Post: 12-26-2008, 04:38 PM
  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