Thread: Text file versus Binary file

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    31

    Text file versus Binary file

    Hi all...

    Im a little confused about the usage and purpose text and binary files.

    To create a binary file, i need to fopen() a file with binary modes such as rb,wb,ab...etc... and we can only use fread and fwrite..am i right?Can we use fscanf and fprintf for a binary file?

    I see some programs usings fread and fwrite while their file is opened using text file modes,such as ,r,w,a..etc...will it work too?

    Im currently creating a search engine on a student database using files.But im not sure bout which kind of file is more suitable to use ..Any idea?thanks

  2. #2
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    remember that fread and fwrite are basically used to read a block or chunk of data from a file. fread is used to read a block of data from the file. to write a block use fwrite. u can specify the no. of bytes to be read or written to the file, while using that fucntion. An example program which is gvien below is way of how to use a fread fucntion to read a text file all at one and place the contents of the text file into a buffer.

    Code:
    #include<stdio.h>
    
    int main()
    {
        FILE *fp;
        char *buff;
        int size;
        
        if((fp = fopen("test.txt","r"))==NULL)
        {
            printf("Error: File cannot be opened\n");
            getchar();
            return 1;
        }
        else
        {
            fseek(fp,0,SEEK_END);
            size = ftell(fp);
            fseek(fp,0,0);
    
            buff = malloc(size * sizeof(char));
    
            fread(buff,1,size,fp);
            *(buff+size)='\0';
            
            printf("%s",buff);
    
            fclose(fp);
            free(buff);
            
            getchar();
            return 0;
        }
    }   
    
    /*my output
    this is test file
     
    ---- > My test.txt file <----
    this is test file
    */
    if u are using structure to store the reacords of the student and then writing onto the file. it better use binary file. so that u can read and write in terms of records. as i said earlier using fread/fwrite u can write/read a block of data.

    ssharish2005
    Last edited by ssharish2005; 04-15-2006 at 05:29 AM.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    A few minor points:
    1 - You don't open in binary mode. You should here. Depending on your OS, there may or may not be a difference between the two modes. There is in Windows.
    2 - You use fseek with a text file. You shouldn't. Due to the above, you may or may not get the results you want when fseeking a text file. The problem is with newlines and the way they're handled between the two modes.
    3 - You could have just used rewind, rather than using fseek to get to the start of the file.
    4 - Your usage of ftell isn't all it seems either. Its return value is implementation dependant when used on text files.
    5 - You never check the return value of ftell, which could be negative.
    6 - Your usage of the ftell for the size to malloc is also incorrect. It's pure luck you didn't crash. You don't include room in your allocation for the nul character.


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > and we can only use fread and fwrite..am i right?
    There is no such thing as a "b" mode in Unix/Linux, it's all the same thing. The "b" is simply ignored if you use it on these platforms.

    In microsoft land, the "b" mode stops the translation between '\n' and '\r\n'. This is important for applications trying to use fixed-length records, since this translation is going to affect the record length.

    > Can we use fscanf and fprintf for a binary file?
    Yes, but you would need to be careful about what applications you view the text file with. Most programmers editors, wordpad, word etc can cope with files with \n or \r\n endings. Simpler programs like notepad cannot.

    > I see some programs usings fread and fwrite while their file is opened using text file
    > modes,such as ,r,w,a..etc...will it work too?
    Yes, they'll work fine on the normal operating systems.
    They'll work fine too on windows as well, so long as you don't try and use fseek(). The fread() and fwrite() will perform \n to \r\n translation, but fseek() won't.
    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.

  5. #5
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Quote Originally Posted by quzah
    A few minor points:
    1 - You don't open in binary mode. You should here. Depending on your OS, there may or may not be a difference between the two modes. There is in Windows.
    2 - You use fseek with a text file. You shouldn't. Due to the above, you may or may not get the results you want when fseeking a text file. The problem is with newlines and the way they're handled between the two modes.
    3 - You could have just used rewind, rather than using fseek to get to the start of the file.
    4 - Your usage of ftell isn't all it seems either. Its return value is implementation dependant when used on text files.
    5 - You never check the return value of ftell, which could be negative.
    6 - Your usage of the ftell for the size to malloc is also incorrect. It's pure luck you didn't crash. You don't include room in your allocation for the nul character.


    Quzah.
    thanks Quzah for pointing me the mistakes. my bad i han't allocated space for nul.

    ssharish2005

  6. #6
    Registered User
    Join Date
    Sep 2005
    Posts
    31
    Thanks everyone for all the good replies and opinions!

    I've decided to use binary files.But,i have a question here...

    After opening a new file, when i am writing all my student data into that file,do i have to use fseek() after each time i fclose() the file?
    If i do not fclose() it , and the fwrite() again, at which position in the file will i be writing to?Will it be at the beginning or current position?
    If i fclose() it,and then fwrite() again, will it be written at the beginning of the file or continue at where i stoped?

    Hope i've made my question clear here...thanks

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You can just have a loop of fwrite() calls to append one record after another in the file.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  2. A bunch of Linker Errors...
    By Junior89 in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2006, 02:59 PM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. Removing text between /* */ in a file
    By 0rion in forum C Programming
    Replies: 2
    Last Post: 04-05-2004, 08:54 AM