Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8556631
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T15:23:47+00:00 2026-06-11T15:23:47+00:00

Okay, I was going through a few programming exercises, and got stuck with one

  • 0

Okay, I was going through a few programming exercises, and got stuck with one involving reading a file. What I need to do is read in a certain set of lines into a 2D array, the lines length and amount of line varies, but I know it beforehand.

So the file is formatted like this:

There are two numbers, n and m, where 1 <= n, m <= 20,

Now n and m come in the file formatted like so: n m (there is a space between the two numbers)

Now after that line there are n lines of integers with m elements each. So for example an input is like so: (The numbers are in the range) 0 <= # <= 50

5 3
2 2 15
6 3 12
7 3 2
2 3 5
3 6 2

So from this the program knows there are 15 elements, and can be held in an array like so:
int foo[5][3]

So how do I read in the file like this? And, lastly, the file has multiple sets of input after one another. So it might go: (2, 2 is info for first set, and 3, 4 for the second set of inputs)

2 2
9 2
6 5
3 4
1 2 29
9 6 18
7 50 12

How do I read this kind of input from a file in C++?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T15:23:48+00:00Added an answer on June 11, 2026 at 3:23 pm

    First, you should have a matrix/grid/2darray class of some sort

    //A matrix class that holds objects of type T
    template<class T>
    struct matrix {
        //a constructor telling the matrix how big it is
        matrix(unsigned columns, unsigned rows) :c(columns), data(columns*rows) {}
        //construct a matrix from a stream
        matrix(std::ifstream& stream) {
            unsigned r;
            stream >> c >> r;
            data.resize(c*r);
            stream >> *this;
            if (!stream) throw std::runtime_error("invalid format in stream");
        }
        //get the number of rows or columns
        unsigned columns() const {return c;}
        unsigned rows() const {return data.size()/c;}
        //an accessor to get the element at position (column,row)
        T& operator()(unsigned col, unsigned row) 
        {assert(col<c && row*c+col<data.size()); return data[data+row*c+col];}
    protected:
        unsigned c; //number of columns
        std::vector<T> data; //This holds the actual data
    };
    

    And then you simply overload operator<<

    template<class T>
    std::istream& operator>>(std::istream& stream, matrix<T>& obj) {
        //for each row
        for(int r=0; r<obj.rows(); ++r) {
            //for each element in that row
            for(int c=0; c<obj.cols(); ++c)
                //read that element from the stream
                stream >> obj(c, r);  //here's the magic line!
        }
        //as always, return the stream
        return stream;
    }
    

    Fairly straightforward.

    int main() {
       std::ifstream input("input.txt");
       int r, c;
       while(input >> c >> r) { //if there's another line
           matrix<char> other(c, r); //make a matrix
           if (!(input >> other)) //if we fail to read in the matrix
               break;  //stop
           //dostuff
       }
       //if we get here: invalid input or all done 
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it okay to get a read-only collection from an aggregate without going through
Okay, I am going to go through ALL my steps thus far just so
Okay so I'm trying to learn Java client/server stuff, and am going through the
I'm going through a book focusing on x86 programming (Professional Assembly Language, WROX 2005).
Okay, so I've noticed, going through some programs other people have written (for education
Okay, I've got this WCF service going. It has a public access, which is
Okay, I know this question doesn't show research effort, but I've been going through
Okay, so I'm going to take the off chance that someone here has used
Okay, im not going to piddle foot around and dance around what im doing.
Okay, this is going to be hard to explain but here goes nothing: Lately

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.