Like Tree1Likes

Read binary, change a value, write to binary

This is a discussion on Read binary, change a value, write to binary within the C Programming forums, part of the General Programming Boards category; Originally Posted by mammoth Here's the output of what gets stored (with the top row as normal): Code: # Num ...

  1. #16
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by mammoth View Post
    Here's the output of what gets stored (with the top row as normal):
    Code:
      #   Num  Description           Type     Qty   Price    Cost   Profit
    ========================================================================
      1  0002  Tacos                    2       4    8.00    7.00     4.00
      2-858993460  ╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
    ╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠xS,■↕-858993460-858993460-9255963134931783100000000000000000000
    0000000000000000000000000.00-925596313493178310000000000000000000000000000000000
    00000000000.00    -0.00
    Any idea why that is happening?
    Yep... you didn't clear the struct before creating the data...

    When adding a new record you should always use mset(&struct,0,sizeof(struct)); before adding data to it.

    Code:
    mset(&recs,0,sizeof(recs));
    recs.prodtype = 0;
    fseek(bf, spot, SEEK_SET);
    fwrite(&recs, sizeof(recs), 1, bf);
    }
    Also the seek and write functions should be inside your conditional to prevent it from writing garbage if the operator enters an invalid record number.



    When editing (eg. changing pricess and such...) you will still get some interspersed garbage if you shorten a string, but numbers are fixed size and should stay clean... for example if you replace "Taco Bell Tacos" with "Hot Dogs"... the record in raw view will show... "Hot Dogs l Tacos" ... this is of no consequence as long as your strings are properly null terminated.

    Plus... if a record is deleted, you should not display it at all... maybe just the searched number and "<-deleted->" at most
    Last edited by CommonTater; 05-12-2011 at 12:05 PM.

  2. #17
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by mammoth View Post
    I kind of liked the loop method better, it's cleaner, but imagine that as data sets increase towards infinity random access trumps the loop for read times.
    Which is why this type of storage is favored in a great many applications... There's no arguing down the speed of Binary Search and Random Access.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. read / write binary files
    By Dark_Phoenix in forum C++ Programming
    Replies: 5
    Last Post: 06-21-2009, 08:56 AM
  2. Read and write binary file?
    By Loic in forum C++ Programming
    Replies: 2
    Last Post: 10-29-2008, 06:31 PM
  3. Read/Write Binary Files
    By Ideswa in forum C++ Programming
    Replies: 11
    Last Post: 08-29-2006, 08:23 AM
  4. binary read/write of objects
    By arjunajay in forum C++ Programming
    Replies: 25
    Last Post: 06-06-2005, 06:35 AM

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