Thread: file id

  1. #1
    Registered User trekker's Avatar
    Join Date
    Mar 2002
    Posts
    46

    file id

    i want my program to be able to figure out if an input file
    is one that was generated from it.
    How can i use the magic number technique ?
    Are there any other portable ways ?

    TIA
    to boldy code where...

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    c++ imposes no structure on files it is entirely up to the programmer to structure his files exactly as he sees fit. So invent your own file format that will uniquely identify data files as belonging to your app.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Registered User trekker's Avatar
    Join Date
    Mar 2002
    Posts
    46
    that's exactly what i want to do.
    i know the structure should be of my choice,
    but i'm asking for any common or popular ways to do so.
    I just want the doc for them
    to boldy code where...

  4. #4
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    One of my books told me ways to make magic numbers for files, I will look at it and post it when I get home this afternoon.
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Much like graphics files or other common file formats, write a file header first. The first N bytes are this, the next N bytes are that... etc. Then add your data after it. You may want to jump over to Wotsit and look at a few file formats to get an idea of how things are commonly done.

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

  6. #6
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Well there is a simple way with structures:

    Code:
    struct MyFileHeader
    {
      char  ID[10];       
      ...
      ..
    };

    ID could be any 10 characters. If the ID does not match your program's ID that it places in the string, it was not created by your program.

  7. #7
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    One method for choosing a magic number is choosing the first four letters of a program name(i.e, list) and then convert them to hexadecimal: 0x6C607374 then add 0x808080 to the number, producing the magic number of 0xECE0F3F4

    this algorythm generates a magic number that is probably unique. The high bit is set on each byte to make the byte non-ANCII and avoid confusion between caps and ascii files
    from Practicle C by, Steve Oualline

    That is one way to go
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  2. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  3. Making a LIB file from a DEF file for a DLL
    By JMPACS in forum C++ Programming
    Replies: 0
    Last Post: 08-02-2003, 08:19 PM
  4. Hmm....help me take a look at this: File Encryptor
    By heljy in forum C Programming
    Replies: 3
    Last Post: 03-23-2002, 10:57 AM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM