Dynamic Arrays

This is a discussion on Dynamic Arrays within the C++ Programming forums, part of the General Programming Boards category; Hello I'm not that familiar with Dynamic Arrays, I need to create one that is of type class, that reads ...

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    1

    Dynamic Arrays

    Hello I'm not that familiar with Dynamic Arrays, I need to create one that is of type class, that reads data from a file, I've gotten up to the point where I've allocated the array to the appropriate size but I just don't know how to read data from a file into the array. In the interest of space I have only included parts of the code:


    <Problem *problems;//created in class file as private data member>

    <ProblemSet::ProblemSet(istream& is)
    {
    problems = new Problem[n];

    countEntries(is);
    for(int i=0; i<n; i++)
    {
    problems[n]=//need to read in data from file here
    }

    }>

    Any suggesions/help would be greatly appreciated as I don't really understand how dynamic arrays are initialized.
    Thanks!

  2. #2
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    lookup <fstream>

    namely:
    ofstream and ifstream

    btw: use code tags...read the forum sticky
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,794
    What data are you trying to read from the file? Is it binary data or text? What does a "line" of input look like and how does it relate to the Problem objects you are attempting to store? What does a Problem object look like?

    Basic file input can be done with an ifstream (input file stream) object. For text data (whitespace delimited) you would typically use the extraction operator >> just like you would use to get input from the user when using cin. The following example will read/count words (whitespace delimited) from the file specified while outputing the count/word to the console.

    Code:
    #include <fstream>
    #include <string>
    #include <iostream>
    using namespace std;
    
    int main()
    {
        ifstream input("file.txt");  // Try to open a file named file.txt
        string str;
        int count = 0;
    
        if( input.is_open() )        // Check if file was opened
        {
            cout << "File successfully opened." << endl;
            while( input >> str )    // while there is data to get from file...
            {
                ++count;
                cout << "Word " << count << " is: " << str << endl;
            }
            cout << "There were " << count << " words in total." << endl;
        }
        else
        {
            cout << "Could not open file." << endl;
        }
    
    }
    The destructor for the ifstream object will close the file automatically for you. Without knowing how your data looks in the file and how it translates into a Problem object it is impossible to know exactly what you need.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating and freeing dynamic arrays
    By circuitbreaker in forum C++ Programming
    Replies: 8
    Last Post: 02-18-2008, 10:18 AM
  2. Replies: 16
    Last Post: 01-01-2008, 03:07 PM
  3. processing dynamic arrays
    By Mario F. in forum C++ Programming
    Replies: 9
    Last Post: 06-04-2006, 11:32 AM
  4. Dynamic (Numeric) Arrays
    By DavidB in forum C++ Programming
    Replies: 5
    Last Post: 05-03-2006, 07:34 PM

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