Thread: binary text file

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

    binary text file

    Hello
    i have a little problem,i am making a phone book with linked lists,that stores the inputed data in a binary text file.

    i have a probelm with to operations on binary file,
    my data in the binary file stored like this : name,family name,phonenumber...over and over again.

    now,how can i update and delete and entery from the binary file not using a temporary binary file or a linked list ( if possible

    any ideas will be mostly appriciated

    thank you

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    May I suggest that you use SQLite instead? Its author has described SQLite has a replacement for fopen(), and in this case it could well serve as that.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    8
    Quote Originally Posted by laserlight View Post
    May I suggest that you use SQLite instead? Its author has described SQLite has a replacement for fopen(), and in this case it could well serve as that.
    no no,i need a solution within c limits,using c original header files,or smth like that
    :\

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    'binary text file' doesn't make sense, that's an oxymoron.
    Which is it, a binary file, OR a text file? Can you yourself read the whole thing using notepad?

    Please show the record data structure or serialisation code.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    >now,how can i update and delete and entery from the binary file not using a temporary binary file or a linked list
    It is pretty difficult to do what you want without the temp file. It would make your life more easier if you use the temp file then rename the temp file to orginal filename. Otherwise to will have to work along with all these offset value which makes it messy.

    ssharish

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Updating is easy, you just fseek() to the record in question, then do an fwrite()

    Appending is similarly just as easy.

    Deleting a record is easy, so long as you have a flag in the record which indicates 'deleted'. In this case, it's just another kind of update.

    To remove a record, you physically have to copy all the records you want to keep to another file. Some applications call this "compact databases", which is something they do at say program exit (or user choice), rather than every time something happens.
    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.

  7. #7
    Registered User
    Join Date
    Dec 2007
    Posts
    8
    Code:
    void update_file()
    {
    FILE *fp;
    int cmp_name,cmp_family;
    list_char number,familyname,name,sname,sfamilyname;
    fp=fopen("binar.bin","ab+rb");
    printf("Please Enter the name and the family name that you want to update:\n\n");
    printf("please enter name\n");
    scanf("%s",&sname);
    printf("please enter familyname\n");
    scanf("%s",&sfamilyname);
    while(!feof(fp)&&(cmp_name=strcmp(sname,name)!=0))
    {
    fread(name,sizeof(list_char),1,fp);
    fread(familyname,sizeof(list_char),1,fp);
    fread(number,sizeof(list_char),1,fp);
    }
    if(cmp_family=strcmp(familyname,sfamilyname)==0)
    printf("please enter new name\n");
    scanf("%s",&sname);
    printf("please enter new familyname\n");
    scanf("%s",&sfamilyname);
    fseek(fp,-3*sizeof(list_char),SEEK_CUR);
    fwrite(&sname,sizeof(list_char),1,fp);
    fwrite(&sfamilyname,sizeof(list_char),1,fp);
    fwrite(&number,sizeof(list_char),1,fp);
    fclose(fp);
    }
    ---------
    thats what i did some days ago,but its not working so much,any ideas?

  8. #8
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Learn to properly indent your code. You won't be able to spot errors if you write it that way.

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. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. Removing text between /* */ in a file
    By 0rion in forum C Programming
    Replies: 2
    Last Post: 04-05-2004, 08:54 AM