function name from text file?

This is a discussion on function name from text file? within the C++ Programming forums, part of the General Programming Boards category; Hi all, What I want to do is the following: at a certain point in my program I want to ...

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    6

    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?

    Thanks,

    bropolwig

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,592
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    10
    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.

    Code:
    //pass the function name as a string
    void run_function(std::string& fname)
    {
    if(fname == "myfunction")
    myfunction();
    //etc
    }
    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
    Location
    Hermosa Beach, CA
    Posts
    446
    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.

    OR,

    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
    Posts
    319
    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, 07:39 PM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM

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