Closing a binary file before it ends

This is a discussion on Closing a binary file before it ends within the C Programming forums, part of the General Programming Boards category; hi everyone i'm working on a school project (a phonebook) using a binary file. one of the procedure's is to ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    3

    Closing a binary file before it ends

    hi everyone

    i'm working on a school project (a phonebook) using a binary file.

    one of the procedure's is to delete a person from the phonebook file. i've been working on an idea for this and in short it has to do with taking the last person in the file and copying him over the person to be deleted and then closing the file just before the last person(thus making the file smaller)

    the only problem is i cant close the file not at the end. i position my file pointer just before the last person in the file( using fseek(phonefile,-sizeof(node),SEEK_END) ) and then i do fclose(phonefile) to close the file just before him. but when i check the file he is still there!

    meaning that an EOF was not assigned to the place i wanted but rather to the end of the file.


    ideas anyone?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    That's not quite how it works. Files grow automatically, but to truncate a file you need to do more work in one way or another.

    You can do a few different
    1. Set a flag or some other way to indicate that "this is a deleted item" in the record. If you want to, you could then create some sort of "linked list" of free entries.

    2. use "truncate()" or some similar function: http://www.hmug.org/man/2/ftruncate.php
    I don't think this is portable to Windows, but that may of course not be much of a problem, or a big problem, depending on what your system is.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Windows has this function: http://msdn2.microsoft.com/en-us/library/aa365531.aspx

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    3
    its a dos program i dont have any handles to files

    i dont think the linked list idea is what our teacher asked cause he specificly said to delete from the file.

    does the ftruncate work on dos or only unix cause i dont have the unistd.h library in my visual c++ 6

  5. #5
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    /me sighs.

    There is a difference between a DOS program written explicitly for DOS and a Windows CONSOLE program that is run in what APPEARS to be a DOS window. If you're using Visual C++ 6, then it's a Windows compiler. It also sucks. Try to get with the times if that is an option and use something like MinGW.

    In concepts of files, the file is going to stay the same way until you start writing to it. So reading has no effect on a file (unless your idea of reading a book is to burn the pages). Simply read in whatever you need to read in, perform whatever calculations in memory that you need to perform, and then write the entire contents of the file back. It's not the most elegant, but at least it works. Later, after you complete that, you can work on better ways of seeking to the correct entry and shifting the other elements back to where they should be.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Well, considering that "Visual C++ 6" can not produce "DOS" applications [it produces Windows Console applications], you should be able to use the SetEndOfFile() that I suggested in the second post.

    Or perhaps this works better:
    http://msdn2.microsoft.com/en-us/lib...yb(VS.80).aspx

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by MacGyver View Post
    /me sighs.

    There is a difference between a DOS program written explicitly for DOS and a Windows CONSOLE program that is run in what APPEARS to be a DOS window. If you're using Visual C++ 6, then it's a Windows compiler. It also sucks. Try to get with the times if that is an option and use something like MinGW.

    In concepts of files, the file is going to stay the same way until you start writing to it. So reading has no effect on a file (unless your idea of reading a book is to burn the pages). Simply read in whatever you need to read in, perform whatever calculations in memory that you need to perform, and then write the entire contents of the file back. It's not the most elegant, but at least it works. Later, after you complete that, you can work on better ways of seeking to the correct entry and shifting the other elements back to where they should be.
    Yes, I agree, read the entire file in, modify, create a new file, write it out, will be the obvious solution - assuming the project allows this. And seeing the almost complete absence of simple ways to do this directly with the "FILE *" products, this is likely to be the correct way. One would assume that the teacher is not requesting quite obscure file handling procedures.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User
    Join Date
    Dec 2007
    Posts
    3
    the teacher is quite obscure himself

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,588
    Quote Originally Posted by MacGyver View Post
    It also sucks. Try to get with the times if that is an option and use something like MinGW.
    Or Visual Studio 2008 Express
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  2. Print file in binary mode
    By vikernes in forum C++ Programming
    Replies: 6
    Last Post: 02-24-2006, 11:43 PM
  3. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 09:46 AM
  5. 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

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