Thread: Execute code from a text file?

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    13

    Execute code from a text file?

    Hi,

    I have coded a very simple save function for a little game I'm messing about with at the moment. The program just writes all the variables to a text file.

    Say I have a "hitpoints" variable, written as:

    Code:
    int hp = 25;
    This will be written to a text file when the save function is called, and will be written like so:

    Code:
    fprintf(file, "hp = %d;", hp);
    This will be written as follows when in saved, text format:

    Code:
    hp = 25;
    Note: 25 may not be the actual value of the variable when it is saved.

    The reason I have written it to the text file this way is because I was hoping I would be able to "load" this straight back into the program and actually execute this line, thus reinitialising the value to what it was when the save function was called.

    My problem is I don't know how to actually execute the lines inside the text file when the program is running.

    Reading this text file back into the program is no problem at all, but I don't want it just printed out to the screen.

    Can anyone help me out?

    Thanks.

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    if you can read it back into the program with 'no problem at all', then it seems youve accomplished what you needed to do. we cant tell you what to do with this variable after it has been read from the saved file (because your the only one who knows what to do with it). maybe you want to extract only the numerical part and assign it to some hp variable? since you know the format of the line should be "hp = X;" where X is some integer number, you can extract a substring of this line. ie starting at position index after "= " and going up to the second last character ";" (last character being newline). the characters between this range would be the hp, that you use "atoi" function to convert this substring to an integer to store in your int hp variable.

    if this isnt what your looking for please give more specifics.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    2
    There is no way to "execute" the lines from a text file. You need to associate the value to the correct variable by yourself.
    Remember that when you compile your code, the compiler transforms all that nice-named variables on memory address.
    hp means nothing to the compiled program except you implement this association (as nadroj suggested).

  4. #4
    Registered User
    Join Date
    Sep 2008
    Posts
    13
    Hey guys,

    Thanks for replying. I am going to try what you've suggested. I didn't think it would be so easy.

    In a nutshell, what I want to do is read the values back into the variables from which they came to begin with. Since the variables are all initialised to values when the program starts, I needed a way of saving them.


    Thanks again.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    > fprintf(file, "hp = %d;", hp);
    Would be reversed with
    fscanf(file, "hp = %d;", &hp);
    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.

  6. #6
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by ricardo0y View Post
    There is no way to "execute" the lines from a text file.
    That's not entirely true. With a buffer overflow you can execute arbitrary code, including stuff you read from a text file. Obviously that's not in any way a good programming technique, but yes, "theoretically" it is possible.

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  7. #7
    Registered User
    Join Date
    Sep 2008
    Posts
    13
    Thanks for all your replies.

    The fscanf function is what I was looking for, thanks Salem!

  8. #8
    Registered User
    Join Date
    Jul 2008
    Posts
    26
    Quote Originally Posted by QuantumPete View Post
    That's not entirely true. With a buffer overflow you can execute arbitrary code, including stuff you read from a text file. Obviously that's not in any way a good programming technique, but yes, "theoretically" it is possible.
    It's also "theoretically" possible that you can include a C compiler in your code to parse the lines and assign the values directly to memory locations, but only insane people would do that for such simple tasks.

    The only practical way is to use scanf, fgets, fread or similar and assign directly.

    Personally if I had a bunch of variables I wanted to save across program runs, I would store them in a structure, and dump that entire structure to disk with fwrite/sizeof, and then read them all back on startup with fread/sizeof. Note that this is only portable across compilers if you save/restore each variable seperately rather than all at once due to structure padding.

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Note that this is only portable across compilers if you save/restore each variable seperately rather than all at once due to structure padding.
    And it's only portable if the variables themselves can be written and read by different compilers. For example, storing multi-byte variables in a file (this basically means anything except chars) won't work if you write from a machine with one endianness and read from a machine with another. Storing a double might not work if the target computer uses a different floating-point format.

    In general, I like to use text files instead of binary files. Yes, binary files are smaller, but text files can be edited by hand if necessary and are a lot more portable.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

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. Replies: 3
    Last Post: 11-08-2005, 07:25 AM
  3. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 10:51 PM
  4. About the Morse code Converter
    By Amber_liam in forum C Programming
    Replies: 17
    Last Post: 05-29-2002, 08:35 AM