How to build read-only file into exe file?

Discussion in 'C Programming' started by Davy, Nov 10, 2005.

  1. Davy

    Davy Guest

    Hi all,

    A read-only data file is read in a C/C++ program.
    And now I use stdio function such as fopen() to fread() to operate the
    file.
    The content of the data file is constant.

    How to build the file into exe file.

    Any suggestions will be appreciated!
    Best regards,
    Davy
     
    Davy, Nov 10, 2005
    #1
    1. Advertising

  2. Davy

    Chris Dollin Guest

    Davy wrote:

    > Hi all,
    >
    > A read-only data file is read in a C/C++ program.
    > And now I use stdio function such as fopen() to fread() to operate the
    > file.
    > The content of the data file is constant.
    >
    > How to build the file into exe file.


    You can't do it in portable C.

    What you /can/ do is to turn the data file into the value of an
    array and work from that.

    It's a shame you can't (portably) use the file-stream reading
    operations to read such an array. (In like circumstances, I
    invented my own pseudostream-reading operations, and made
    implementations that read from FILE* streams and others that
    read from strings/arrays.)

    --
    Chris "one-track" Dollin
    Capability does not imply necessity.
     
    Chris Dollin, Nov 10, 2005
    #2
    1. Advertising

  3. Davy

    mlimber Guest

    Davy wrote:
    > Hi all,
    >
    > A read-only data file is read in a C/C++ program.
    > And now I use stdio function such as fopen() to fread() to operate the
    > file.
    > The content of the data file is constant.
    >
    > How to build the file into exe file.
    >
    > Any suggestions will be appreciated!
    > Best regards,
    > Davy


    Convert the file to a list of characters (or ints or whatever) in an
    array, e.g.,

    unsigned char data[] = { 42, 50, 33, /*...*/ };

    Then, use that array in your program.

    Cheers! --M
     
    mlimber, Nov 10, 2005
    #3
  4. Davy

    Malcolm Guest

    "Davy" <> wrote
    > A read-only data file is read in a C/C++ program.
    > And now I use stdio function such as fopen() to fread() to operate the
    > file.
    > The content of the data file is constant.
    >
    > How to build the file into exe file.
    >

    The term "exe" is a hint that you are compiling under MS Windows.
    The Microsoft Visual C++ compiler does provide facilities for including
    "resources" in your executable. The only C way of converting these into a
    file is to create a temporary woth tmpfile(), fwrite in the data, rewind the
    file, and then pass to the read functions. Horribly inefficient, but that
    matters much less these days than it used to.
     
    Malcolm, Nov 10, 2005
    #4
  5. Davy

    Davy Guest

    Hi,

    Thank you for your help :)

    Yes, I use VC compiler. And can you provide some hint on using
    "resources" in executable?

    Any suggestions will be appreciated!
    Best regards,
    Davy
     
    Davy, Nov 12, 2005
    #5
  6. Davy

    Flash Gordon Guest

    Davy wrote:
    > Hi,
    >
    > Thank you for your help :)
    >
    > Yes, I use VC compiler. And can you provide some hint on using
    > "resources" in executable?
    >
    > Any suggestions will be appreciated!


    The best suggestion we can give in comp.lang.c (and probably
    comp.lang.c++) is to discus your system in a system specific news group.
    I've set follow-ups to be only comp.os.ms-windows.programmer.win32 since
    that is the most likely of the groups you have posted this to.
    --
    Flash Gordon
    Living in interesting times.
    Although my email address says spam, it is real and I read it.
     
    Flash Gordon, Nov 12, 2005
    #6
  7. Davy

    Malcolm Guest

    "Davy" <> wrote
    > Thank you for your help :)
    >
    > Yes, I use VC compiler. And can you provide some hint on using
    > "resources" in executable?
    >

    Not really.
    The situation is that the ANSI C standard, which this newsgroup discusses,
    doesn't provide any really good way of incorporating non-code data into
    programs.
    Small items like strings or tables can be embedded into C source, but one
    you have more than a hundred or so values this approach rapidly breaks down.
    The ANSI way of solving the problem is to write a little program that read
    in binary data (images, sound, 3d graphics coordinates etc) and outputs C
    arrays as text.
    This isn't particularly convenient, so Microsoft have helpfully provided a
    "resource compiler" that effectively does this for you.
    There's nothing wrong with using the Microsoft tool, but it means that your
    program is no longer, strictly, a C program. Thus it becomes off-topic on
    comp.ang.c
     
    Malcolm, Nov 12, 2005
    #7
  8. Davy

    /dev/ Guest

    Davy wrote:

    > Hi,
    >
    > Thank you for your help :)
    >
    > Yes, I use VC compiler. And can you provide some hint on using
    > "resources" in executable?
    >
    > Any suggestions will be appreciated!
    > Best regards,
    > Davy
    >

    Simply add resource script file to the project and then import your file as a custom resource type.
    Using custom resource:

    HINSTANCE hInstance = GetModuleHandle(NULL);
    HRSRC hres = FindResource(hInstance,"name_of_your_resource","your_custom_resource_type");
    DWORD size = SizeofResource(hInstance,hres); // size of your file
    HGLOBAL hglob = LoadResource(hInstance,hres);
    void* p = LockResource(hglob); // now p points to your file in memory
     
    /dev/, Nov 14, 2005
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. vnssoftware
    Replies:
    4
    Views:
    470
    Dmitry R
    Dec 31, 2003
  2. keithb
    Replies:
    2
    Views:
    8,021
    keithb
    Jun 7, 2006
  3. Davy
    Replies:
    7
    Views:
    425
    /dev/
    Nov 14, 2005
  4. Replies:
    5
    Views:
    316
  5. Ricardo Aráoz

    Re: Read dbf file as read only

    Ricardo Aráoz, Aug 26, 2008, in forum: Python
    Replies:
    0
    Views:
    392
    Ricardo Aráoz
    Aug 26, 2008
Loading...

Share This Page