Thread: Boundless definitions?

  1. #1
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001

    Boundless definitions?

    I have a program that has to read from a file, and when prompted from the file, declare a variable and fill in information to it supplied by the file. The file could have the program produce a limitless amount of these variables, so how would I have my program declare variables... dynamically, I think the term might be. Using an array has its limits.

  2. #2
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    The easiest way would be to use a std::vector to hold your data. You can keep on inserting data, and it will keep on resizing its internal array to hold it, so long as the system has sufficient memory to store it.

    The following code shows how to read an aribrary number of strings into a vector. You might want to store a different kind of object in the vector, and create them by reading into a string and then parsing the string.

    #include <iostream>
    #include <fstream>
    #include <vector>
    #include <string>
    int main() {
      std::ifstream in("compile.cpp");
      std::string line;
      std::vector<std::string> lines;
      // read all the lines in the file
      while (std::getline(in, line)) {
        // for every line in the file, put it at the back of the vector
      // output all the lines we read before
      for (int i = 0; i < lines.size(); ++i) {
        std::cout << lines[i] << '\n';
      return 0;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 05-24-2009, 02:42 AM
  2. need help with FIFO QueueItem member definitions
    By jackfraust in forum C++ Programming
    Replies: 15
    Last Post: 02-26-2009, 06:48 PM
  3. Replies: 7
    Last Post: 11-17-2008, 01:00 PM
  4. Help on definitions
    By GAMEPROJOE in forum C++ Programming
    Replies: 2
    Last Post: 07-18-2003, 01:59 PM
  5. help writing function definitions
    By jlmac2001 in forum C++ Programming
    Replies: 2
    Last Post: 04-10-2003, 09:44 PM