Thread: function name from text file?

  1. #1
    Registered User
    Join Date
    Feb 2006

    function name from text file?

    Hi all,

    What I want to do is the following: at a certain point in my program I want to execute a function. The name of this function is stored in a text file. Is it possible to read this text file and execute the read string as a function?



  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    Which OS / Compiler ?

    How big a set of functions are you talking about

    What about the security implications of allowing any function to be called.

    Do all the functions have the same prototype

    Got any more details of what you're trying to do?
    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.

  3. #3
    Registered User
    Join Date
    Aug 2005
    You can't really call a function by its name as the function names are discarded in the final executable (unless you leave the debug symbols in)... so I'd just use a if statement block to compare the string you've read from the file to a list of possible functions to call. This will work as long as the number of functions isn't very large, otherwise it will become tedious.

    //pass the function name as a string
    void run_function(std::string& fname)
    if(fname == "myfunction")
    Alternatively, you could use an initialized array with the list of possible function names and and a pointer to the function, then iterate through the list, comparing the string to the function name, and if it matches, call the function.

  4. #4
    Registered User
    Join Date
    Aug 2002
    Hermosa Beach, CA
    If you're talking about windows, you could:
    1) Write a .dll with the desired entry points, presumably all functions conform to some interface, i.e. they have the same parameters.
    2) Use LoadLibrary and GetProcAddress API calls to get a function pointer
    3) Call the desired function. Your text file could even contain two values, 1) the .dll to use, 2) the entry point in the .dll to use.


    You could explicitly create/implement some interface via COM, and then the COM object would have a version independent progid, which is a string, and you could use that to instantiate the object, which would provide access to a whole family of routines in that interface.
    The crows maintain that a single crow could destroy the heavens. Doubtless this is so. But it proves nothing against the heavens, for the heavens signify simply: the impossibility of crows.

  5. #5
    Registered User
    Join Date
    Nov 2002
    so if you can get a it to run a function from a txt , that means you could edit the function at run time by changing stuff in the txt to do somethign diferent correct?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  2. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. Outputting to a File Modified Text
    By alpha in forum C++ Programming
    Replies: 8
    Last Post: 11-24-2003, 08:39 PM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM