Thread: Inserting EOF in Binary File using C

  1. #1
    Registered User
    Join Date
    May 2012
    Location
    India
    Posts
    12

    Inserting EOF in Binary File using C

    Hi,

    I am facing problem in inserting EOF character at the end of the binary file. I read CTRL+Z for windows but it doesn't work.

    First of all I want to ask that does fclose(SOME FILE POINTER) not automatically insert EOF at the end of file????

    Below is the approx. code that i am writing, Kindly tell me whether it is correct or is there a better way to put EOF:

    Code:
    int add_Book()
    {
            fp = fopen("book.lib","a");
            book bk; //Book Structure
            int add = 1;
    
            while(add == 1)
            {
    
             printf("Enter 1 for adding book and 2 to quit!!");
             scanf("%d",&add);
       
                    if(add==2)
                    {
                           Printf("Press CTRL+Z to quit!!!!");
                           scanf("%s%s",bk.author,bk.name);
                           fwrite(&bk,sizeof(bk),1,fp);
                           fclose(fp);
                    }
                     
                     else if(add == 1)
                     {
                          printf("Enter the name of book    ");
                          scanf("%s",bk.name);
                          printf("Enter the author     ");
                          scanf("%s",bk.author);
                          fwrite(&bk,sizeof(bk),1,fp);
                          fclose(fp);
                     }
    }

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    fclose() flushes pending output (if any) and closes the file. You do not need to insert any EOF marker at the end. Functions that read the file will report EOF (or some derived condition, depending on how the function works) when they encounter end of file.

    The representation of end-of-file is system dependent (it depends on host system, disk drivers, etc). Unless you are writing a device driver for a storage device, you do not need to worry about how end-of-file is actually represented. That is taken care of by I/O functions.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    May 2012
    Location
    India
    Posts
    12
    But I am caught in an infinte loop while reading the file to display the contents and though even if EOF at the end of the file is not necessary please still tell me how to....

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Maybe you should...oh, I don't know...show us how you're opening and *reading* the file?

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by Dushyant View Post
    But I am caught in an infinte loop while reading the file to display the contents and though even if EOF at the end of the file is not necessary please still tell me how to....
    EOF isn't actually a value inside of files, you know.

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Dushyant View Post
    But I am caught in an infinte loop while reading the file to display the contents and though even if EOF at the end of the file is not necessary please still tell me how to....
    No need to roll your eyes. You have asked a meaningless question.

    If you are getting into an infinite loop reading a file, the problem is in a mismatch between code that wrote the file and code that reads the file. Such problems would not be fixed by somehow writing EOF at the end of file. They would be fixed by debugging your code.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inserting value to a sorted binary tree?
    By HappySmileMan in forum C Programming
    Replies: 3
    Last Post: 02-15-2009, 09:34 AM
  2. Inserting Nodes at Binary Trees
    By blondeamon in forum C++ Programming
    Replies: 3
    Last Post: 12-25-2007, 08:27 AM
  3. Inserting words into a binary search tree
    By lime in forum C Programming
    Replies: 9
    Last Post: 08-02-2003, 10:02 PM
  4. Inserting infix into a binary tree
    By Nakeerb in forum C++ Programming
    Replies: 20
    Last Post: 01-20-2003, 05:03 PM
  5. inserting characters into a binary tree
    By sballew in forum C Programming
    Replies: 4
    Last Post: 12-06-2001, 04:08 PM