writing binary file

This is a discussion on writing binary file within the C Programming forums, part of the General Programming Boards category; Hello, I am working with file I/O at the moment. I am attempting to read in numbers from an ascii ...

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    19

    writing binary file

    Hello,

    I am working with file I/O at the moment. I am attempting to read in numbers from an ascii file and then write them to a binary file. My program is opening the ascii file correctly, and it is opening and writing data to the new file also.

    The issue is when I enter ls -l from the command prompt the "ascii" and "binary" files are the same size. I would expect the binary file to be significantly smaller as I would expect the ascii file to have two bytes for say 66 and the binary to use one byte to represent it. I've been digging through the forums and google and am not quite sure why I am not getting results that I would expect. I have also attached the ascii file, I tried uploading the binary file(tried adding .bin.txt to the file, no extension, only .txt) but it won't allow me to attach it.

    Thank you for any discussion/enlightenment.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    void write_binary(FILE *ascii_pointer, FILE *binary_pointer);
    void close_files(FILE *ascii_pointer, FILE *binary_pointer);
    
    int main(){
    
    	FILE *input_file;	/* Pointer for the ascii input file */
    	FILE *output_file;	/* Pointer for the binary output file */
    	
    	/*open_files(input_file,output_file);*/
    	input_file = fopen("ASCIItest.txt", "r");
    	output_file = fopen("BINARYtest.bin","wb");
    
    	/* pass pointers to read in ascii and write to binary file */
    	write_binary(input_file,output_file);
    
    	/* pass pointers to files to be closed */
    	close_files(input_file, output_file);
    	
    
    return(0);
    }
    
    
    /*
       FUCTION WRITE BINARY                                      
      About: This function reads in characters from an ascii file
             and writes them to a binary file.                   
             This Function accepts two pointers as its arguments 
             -- ascii_pointer == pointer to an ascii file to be  
                                read from.                       
             -- binary_pointer == pointer to a binary file to be 
                                  written to.                    
    */
    
    
    void write_binary(FILE *ascii_pointer, FILE *binary_pointer){
    
    
    	char ch;	/* character variable for reading in numbers from the ascii file */
    	
    
    	/* get a character from the ascii file */
    	ch = fgetc(ascii_pointer);
    
    	/* loop until end of file is reached */
    	while(ch != EOF){
    
    		/* write the character to the file using fwrite */
    		fwrite(&ch, sizeof(ch),1,binary_pointer);
    		/* get a new character */
    		ch = fgetc(ascii_pointer);
    
    	}
    
    }
    
    /* function to close pointers        */
    
    void close_files(FILE *ascii_pointer, FILE *binary_pointer){
    
    	fclose(ascii_pointer);
    	fclose(binary_pointer);
    
    }
    ASCIItest.txt





    ******EDIT*****

    Also I tried opening both files in gedit as well as vim and both were viewable in what seemed to be the original ascii characters, I went over to windows and was able to view both with notepad. I thought I would get something odd with the binary file in windows but I did not. I don't know if this will be helpful but thought I would include it. Thanks.
    Last edited by dtow1; 09-22-2011 at 11:20 PM. Reason: Additional Info

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    All your code really does is copy the file... it makes no translations of any kind... if you want a true binary file, you need to read in each value in your test file using something like fscanf() get them into an int variable then write the int to disk.

    Then you'll see a size difference... but don't be surprised if it gets bigger instead of smaller...

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    19
    Thank you for the reply. That does make sense that I would need to work with the data for anything to change. Do you have any information as to why it would get larger? I keep reading that writing as binary would reduce the size, would I need to say convert them to hex to reap the benefits or using a binary write?

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by dtow1 View Post
    Thank you for the reply. That does make sense that I would need to work with the data for anything to change. Do you have any information as to why it would get larger? I keep reading that writing as binary would reduce the size, would I need to say convert them to hex to reap the benefits or using a binary write?
    Look up fscanf() in your C library documentation (help file?)... it will tell you how to read the text numbers in the file into integers inside the program.

    It will get bigger because your test file contains 2 digit numbers separated by a space (3 bytes each)... but when stored in a binary file you need 4 bytes for each integer 1.

    Converting to hex does nothing. The underlying variable type is always binary ... stored as a series of voltages in a CPU register, translated into human number bases by the compiler, for our convenience.


    1 ... Unless that is you're using an antigue like turbo C in which case an integer is only 2 bytes and considerably less useful.
    Last edited by CommonTater; 09-22-2011 at 11:33 PM.

  5. #5
    Registered User
    Join Date
    Sep 2011
    Posts
    19
    Thank you Tater. Ill look up fscanf() and see if I can get a better grip on this. I appreciate the information.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    No worries... if you have any more problems, post your revised code and we'll see what we can do...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. writing to a binary file
    By Inver28 in forum C Programming
    Replies: 4
    Last Post: 03-02-2008, 01:36 AM
  2. Writing binary data to a file
    By zacs7 in forum C Programming
    Replies: 5
    Last Post: 10-24-2007, 04:00 PM
  3. Writing to a Binary File
    By EvilBaby in forum C Programming
    Replies: 4
    Last Post: 04-12-2004, 05:15 AM
  4. Writing to a Binary File
    By Lah in forum C Programming
    Replies: 1
    Last Post: 10-27-2003, 12:41 AM
  5. Writing to a binary file
    By nz_cutechick in forum C++ Programming
    Replies: 5
    Last Post: 08-15-2002, 06:46 AM

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