Thread: How to read unknown total of int data from text file to a 2-dim array in C or C++?

  1. #1
    Registered User
    Join Date
    Sep 2005

    How to read unknown total of int data from text file to a 2-dim array in C or C++?

    I want to create a function read data of type integer or double from a text file and stores them in a 2-dimensional array. But the number of the data in the text file is unknown during the program execution. Therefore the size of the array is also unknown.

    I have problems in reading the correct data value of type int and double and stores them in the array. I also can't determine the correct total of columns and rows for the array.

    My data in the text file looks something like this:


    Currently,the data in my file consists of 32 rows and 34 columns, and I tried using vector as shown below. But the program didn't return the correct rows and columns values. It read the row as 28 and column as 1 only. Also the program seem didn't able to read the data value into the vector as array data. No data is displayed on screen.

    #include <fstream>   // for ifstream
    #include <iostream>  // for cout
    #include <vector>    // for vector
    #include <iomanip>   // for setw
    #include <string>    // for string
    #include <sstream>   // for stringstream
    template <typename T>
    void ReadMatrix(const char * fname , std::vector< std::vector<T> > & matrix)
        using namespace std;
        T value;
        string line;
        ifstream in(fname);
        vector<T> v;
        while (getline(in,line))
            stringstream ss(line);
            while (ss >> value)
    template <typename T>
    void PrintMatrix(std::ostream & out, const T & matrix)
        for (int i=0; i<matrix.size(); ++i)
            for (int j=0; j<matrix[i].size(); ++j)
                out << std::setw(10) << matrix[i][j];
            out << "\n";
    typedef std::vector< std::vector<int> > IMATRIX;
    int main()
        IMATRIX imatrix;
        // optionally reserve to make reading in a little more efficient
        return 0;

    Later, I need to scan each row of data for 0s and stores them in text file. then I access the text file to normalize all 0s by rows. Therefore I need to save the information about the location of the 0s(which rows and columns). After the normalization, I need to replace the 0s in the later text file with the new value at the correct location. How to save the location value(rows and pixels) and corresponds them with their new normalized value?

    Thanks in advance.

  2. #2
    Registered User
    Join Date
    Aug 2005
    Your code for reading and writing a vector of ints from and to a file should work fine. But it looks like you are using the ReadMatrix function to read some data from a file that did not use the PrintMatrix function to save the data.
    The ReadMatrix function expects a whitspace between the values. The sampledata you provided has only 1 value per line so ReadMatrix returns a columnsize of 1.


    BTW I would modify the PrintMatrix function to make it work with any numerical type.

    template <typename T>
    void PrintMatrix(std::ostream & out, const T & matrix){
        for (int i=0; i<matrix.size(); ++i) {
            for (int j=0; j<matrix[i].size(); ++j) {
                out << matrix[i][j] << " ";
            out << "\n";

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. Replies: 3
    Last Post: 05-13-2007, 08:55 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM
    By Registered in forum C++ Programming
    Replies: 1
    Last Post: 07-15-2002, 03:23 PM