pasting code form a .txt file...

This is a discussion on pasting code form a .txt file... within the C++ Programming forums, part of the General Programming Boards category; Alright, I want it so that the program opens a .txt file and it can past the text in the ...

  1. #1
    Registered User
    Join Date
    Aug 2004
    Posts
    731

    pasting code form a .txt file...

    Alright, I want it so that the program opens a .txt file and it can past the text in the program. So I could basicly put functions in the .txt file and the program reads them as actauly functions and does what the .txt files says.

    This is what I got so far...


    Code:
    #include <iostream.h>
    #include <conio.h>
    #include <fstream.h>
    int main()
    {
        int loop;
        loop=1;
        
        char test;
        
        while(loop==1)
        {
            loop=0;
            
            char player[100];
            
            cout<<"Player: ";
            cin>>player;
            cout<<"\n";
            
            ifstream openfile("test.txt");
            
            while(!openfile.eof())
            {
                openfile.get(test);
            }       
            
            test;
            
            loop=1;
        }
    }

    It's not the best at all right now... or complete... and some varaibles were throwen around that will be used later...

    any help please

  2. #2
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    Erm, I highly doubt that will work.
    Does that even compile?

    Doing what your trying to do is basically what dll's are for.

    I would strongly suggest you learn how to create a dll and how to implement it.

    Dev-C++ comes with example code that you could very easily modify and use.

  3. #3
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    lol that is what I am trying to get around. I don't want to use dlls (not because they hard to learn either!) I want to use simple MODIFYABLE text files. And I know that code won't owrk that is why I said that is what I got so far. And suprisingly that does compile.

  4. #4
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    test;

    Im pretty sure that that wont actually do anything. Thats what I thought would have gave you a compiler error.

    Hmm, you could make a simple scripting engine I suppose.

    It would be simple to read variables from a text file, but pretty complex if you want actual functions.

    If you want to read some actual C++ code at runtime then your program would have to act as a C++ interpreter basically.

    I would look into how to break up strings into tokens.

    Then you could create a simple scripting engine.

    yout txt file
    Code:
    print ( Hi )
    Then you could read in the whole string and break it into tokens by checking for spaces.
    As you go you could check what the token is and execute commands accordingly.

    I think I will make a scripting engine,.. Sounds fun.

  5. #5
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    lol I think I know wha tyou mean by tokens. I got a nice, about, 13 page tut on file I/O printed off and in front of me.

    And I know you can make maps using .txt files so I thought why not functions?

    but I will keep thinking on ways do this and keep reading stuff.

  6. #6
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,264
    Put all the functions you want to call in a dll. Then, you can read function names from a text file, and call those. Let's say your dll has the functions:
    void function1();
    void function2();

    Here is how you would call them dynamically:

    Code:
    #include <windows.h>
    
    int main(int argc, char* argv[])
    {
    	HMODULE		hModule;
    	void		(*somefn)();
    
    	hModule = LoadLibrary("mydll.dll");
    	if(!hModule) return 0;
    
    	somefn = (void (*)())GetProcAddress(hModule,"function1");
    	if(somefn)
    		somefn();
    
    	somefn = (void (*)())GetProcAddress(hModule,"function2");
    	if(somefn)
    		somefn();
    	
    	return 0;
    }

  7. #7
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    Also here is an example of how you could do a simple scripting engine

    Code:
    #include <iostream>
    #include <string>
    #include <sstream>
    
    int main()
    {
        
        std::string script = "print ( hello )";
        std::stringstream split(script);
        std::string token;
        
        while(split >> token) {
            
            if(token == "print") {
                
                split >> token;
                
                if(token == "(") {
                    
                    split >> token;
                    
                    std::cout << token;
                    
                } 
                
            }        
            
        }
        
        std::cin.get();
        return 0;
        
    }
    You could just read in lines from a text file into a string and then create a streamstring out of it.

    There is probably a better way, I just havent found it.

    EDIT:
    Just to explain how you could have a script thats useful.
    You could actually assign variables.

    have a variable called Var1 and in your script
    Code:
    Set ( Var1 = 2 )
    then you could do

    Code:
    if(token == "Var1") {
    
        script >> token;
    
        if(token == "=") {
    
            script >> token;
    
            Var1 = token
       
        }
    
    }
    Or something like that anyway.
    Last edited by Vicious; 09-06-2004 at 10:04 PM.

  8. #8
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    What you're asking is how to write a very very very advanced scripting engine. IMHO, few people on this board would be able to do it. A non-C++ script would be easier to write - like vicious' example. But unless your scripting engine has a LOT of built-in functions, scripting languages generally aren't useful for much except websites, macros, AI, and game consoles since they are (a) slow to interpret, and (b) limited in functionality.

    **EDIT**
    Reading the above posts, if you want to be able to create variables in your scripting language, you can use a std::map. Those containers are truly wonderful.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  9. #9
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    Yeah creating a scripting engine for C++ code would basically be putting a C++ interpretter in your code. It would be extreamely difficult and tedious.

    If you look at a few games like Raven Shield or Unreal Tournament, you can see how scripting can be used.

    But in your case I think the best thing would be DLL's

  10. #10
    ggs
    ggs is offline
    C > C++ duders ggs's Avatar
    Join Date
    Aug 2001
    Posts
    435
    if you are quite serious about writing a small scripting language, it would do you well to inspect LISP. it should be possible without too much difficulty to write a LISP-like language in a short amount of time (although it would be very difficult to write a language with just as much functionality)
    .sect signature

  11. #11
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    Unhappy Here's the issue:

    Your computer can't "run" C++! Your compiler takes your C++ program and makes a machine-language program (.exe file) that your microprocessor can run.

    An interpreter converts the source code to machine-language "on-the-fly" when you run the program... you don't get an exe file.

  12. #12
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    Lol wow I got more help then I thought. But I don't knwo if I said this before the point of my project (Project Chi Chi) is for users to create things for it thus I want plug ins. But I am going to school now so that could take a whole lot of time. I am now liking the dll idea...I went from .txt to dll back to .txt and now to dll. How sad...

  13. #13
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    DougDBug: Nobody ever said you could 'run' C++. In fact, if you read the rest of the thread, you'll realize that we have been talking about scripting languages and interpreters all along. For that matter, you are the first person to bring up the subject of exe files.

    >>An interpreter converts the source code to machine-language "on-the-fly" when you run the program...
    By that I'm assuming you mean 'run the script through the interpreter'. Even then your statement isn't completely accurate; there is no 'conversion' to machine code that takes place; the interpreter reads the script, and according to the parsed input, takes predetermined hardcoded actions which in this case simulate the actions of a true compiled program. If true conversion took place, it would be a compiler and not an interpreter anymore.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  14. #14
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>I am now liking the dll idea...I went from .txt to dll back to .txt and now to dll. How sad...
    That's life

    But seriously, if you're doing plug-ins then definitely go with the .dll's. It's guaranteed to:
    a) Perform better
    b) Be less buggy
    c) Be easier to code
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  15. #15
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    lol alright but the...

    #include <windows.h>

    int main(int argc, char* argv[])
    {
    HMODULE hModule;
    void (*somefn)();

    hModule = LoadLibrary("mydll.dll");
    if(!hModule) return 0;

    somefn = (void (*)())GetProcAddress(hModule,"function1");
    if(somefn)
    somefn();

    somefn = (void (*)())GetProcAddress(hModule,"function2");
    if(somefn)
    somefn();

    return 0;
    }
    dll example just got me lost because the dev-cpp example of a dll looks whole lot different. First of all it has a .h and a .cpp and I don't even know were the .dll comes in! But now I am looking at the dll example and what is...

    Code:
    DllClass::DllClass()
    {
    
    }
    
    
    DllClass::~DllClass ()
    {
    
    }
    Is that were I put the functions or something?

Page 1 of 7 1234567 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-22-2008, 12:19 AM
  2. read from .txt file & put into array?
    By slow brain in forum C Programming
    Replies: 6
    Last Post: 02-25-2003, 05:16 AM
  3. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 05:10 PM

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