Thread: Dynamic Memory... Need Help

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

    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


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

    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.


  2. #2
    Registered User
    Join Date
    Nov 2002
    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:

    #include <fstream>
    using namspace std;
    ifstream foo; "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
    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
    The edge of the known universe
    > <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.

  5. #5
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    This is sort of a long shot since I'm not terribly experienced with templates, but...
    #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>;
       std::string type;
       std::getline(std::cin, 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, 09:57 PM
  2. Dynamic memory allocation...
    By dicorr in forum C Programming
    Replies: 1
    Last Post: 06-24-2006, 03: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, 01: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