Dynamic Memory... Need Help

This is a discussion on Dynamic Memory... Need Help within the C++ Programming forums, part of the General Programming Boards category; Just wondering whether it was possible to actually import a piece of text in from a text file and instead ...

  1. #1
    Registered User jimboob's Avatar
    Join Date
    Jun 2004
    Posts
    40

    Dynamic Memory... Need Help

    Just wondering whether it was possible to actually import a piece of text in from a text file and instead of using arrays to allocate data for it, i want to use a variable

    e.g.

    <code>
    if (Inputted_Text == "int")
    {
    // then create a new int with the name chosen in a 2nd parameter
    }
    </code>

    I don't think it can be done like that but i was just curious because i wanted to see if i could declare variables in a text file and then input them.

    TIA

  2. #2
    Registered User
    Join Date
    Nov 2002
    Posts
    126
    Just for the future, code tags are in square braces( [ ), not angle braces. I'm not sure I completely understand what you're trying to do, but I'm assuming you'd like to input certain data types from a file just as you might do it from the keyboard. You can use the ifstream object to do this:

    Code:
    #include <fstream>
    
    using namspace std;
    
    //...
    
    ifstream foo;
    foo.open( "The name of your file" );
    
    //Do some checking to make sure the stream is good, look it up
    
    int bar;
    
    foo >>bar; //Inputs an integer from the file
    Hope that helps...

  3. #3
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    No, you can't create variable names by reading them from a text file. You have to specify variable names when you write the code.

    You could map the name you read in to the value you want to hold with it. That is a similar concept.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,824
    > <code>
    Next time, use square brackets for code tags
    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.

  5. #5
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    This is sort of a long shot since I'm not terribly experienced with templates, but...
    Code:
    #include <string>
    #include <map>
    #include <iostream>
     
    template <typename T>
    void myFunction()
    {
       T var;
       //do something
    }
     
    int main()
    {
       std::map<std::string, void (*)()> funcMap;
       funcMap["unsigned char"] = myFunction<unsigned char>;
       funcMap["unsigned short"] = myFunction<unsigned short>;
       funcMap["unsigned int"] = myFunction<unsigned int>;
       funcMap["unsigned long"] = myFunction<unsigned long>;
       funcMap["long double"] = myFunction<long double>;
       funcMap["char"] = myFunction<char>;
       //etc.
     
       std::string type;
       std::getline(std::cin, type);
     
       funcMap[type]();
     
       return 0;
    }
    As I said, not totally sure if that'll compile or run properly, and it's a cheat anyway.
    Just Google It. √

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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic Linking & Memory usage
    By @nthony in forum C Programming
    Replies: 2
    Last Post: 06-02-2007, 10:57 PM
  2. Dynamic memory allocation...
    By dicorr in forum C Programming
    Replies: 1
    Last Post: 06-24-2006, 04:59 AM
  3. Is it necessary to write a specific memory manager ?
    By Morglum in forum Game Programming
    Replies: 18
    Last Post: 07-01-2002, 02:41 PM
  4. dynamic memory + linked lists
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 02-10-2002, 04:50 PM
  5. Dynamic Memory Allocation for fstream (binary)
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 12-12-2001, 10:52 AM

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