944,029 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 7609
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Sep 1st, 2007
0

How do I read a text file into a 2D vector?

Expand Post »
Guys,

Your help will be appreicated in this matter.

i have to read a text file which has rows and columns into a vector. The text file could be of any size.

000000000
010010001
010010011
001001001

Now, how do I upload this text file on to a 2D vector?

so far, I have come up with this code:

C++ Syntax (Toggle Plain Text)
  1. ifstream in ("block.in");
  2. ofstream out("block.out");
  3.  
  4. vector<vector<int>> block;
  5. for (int i = 0; i < ???; ++i)
  6. block.push_back(*istream_iterator<int>(in));

I don't know if this is correct at all. I am sorry guys, bear with me, I am new to the whole STL thing.

Any help would be appreciated.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Crouchinho is offline Offline
15 posts
since Sep 2007
Sep 1st, 2007
0

Re: How do I read a text file into a 2D vector?

The rows would appear to be newline delimited; columns appear per character. Push back each column in a particular row, then when you are done with the row push it back.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Sep 1st, 2007
0

Re: How do I read a text file into a 2D vector?

Thanks Dave, it gave me a breakthrough.
ok, so now I used the getline function to get the lines and insert into the vector, however, now that the row has been filled, how do i increment the columns?

This is the code I have so far, please bear with me.

C++ Syntax (Toggle Plain Text)
  1. #include <fstream>
  2. #include <vector> // for vector
  3. #include <iomanip> // for setprecision
  4. #include <numeric>
  5. #include <string>
  6. #include <sstream>
  7. using namespace std;
  8.  
  9. void main()
  10. {
  11. string s;
  12. ifstream in ("block.in");
  13. ofstream out("block.out");
  14. vector<vector<int>> block;
  15.  
  16. while(getline(in,s))
  17. block.push_back(*istream_iterator<vector<int>>(in))
  18.  
  19.  
  20. }

I know that I have to use a 'for' loop before I implement the push back statement. However, I do not know where to restrict or end my for loop. Got any ideas
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Crouchinho is offline Offline
15 posts
since Sep 2007
Sep 1st, 2007
0

Re: How do I read a text file into a 2D vector?

For input I'm kinda working with something like this.
C++ Syntax (Toggle Plain Text)
  1. std::vector<std::vector<int> > array;
  2. //
  3. // Input
  4. //
  5. std::string line;
  6. while ( std::getline(file, line) )
  7. {
  8. std::vector<int> row;
  9. char ch;
  10. std::istringstream ss(line);
  11. while ( ss >> ch )
  12. {
  13. row.push_back(ch - '0');
  14. }
  15. array.push_back(row);
  16. }
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Sep 1st, 2007
0

Re: How do I read a text file into a 2D vector?

Thanks for that as well, Dave. You are slowly becoming my favorite person ever!!

However, I wanted to ask you, what specific function does the char ch provide in this code?

and also would my code go tits up if I have this:

C++ Syntax (Toggle Plain Text)
  1. vector<int> row;
  2. char letter;
  3. istringstream ss (row);
  4. while(letter >> ss){
  5. row.push_back(*istream_iterator<int>(in));
  6. }
  7. block.push_back(row);

?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Crouchinho is offline Offline
15 posts
since Sep 2007
Sep 1st, 2007
0

Re: How do I read a text file into a 2D vector?

Click to Expand / Collapse  Quote originally posted by Crouchinho ...
However, I wanted to ask you, what specific function does the char ch provide in this code?
Well, I'm still trying to learn and remember, but I think it goes something like this...

A string is read into line. To push back each represented int, first each character is extracted from the string (using the stringstream) and placed in ch. Then an int is created and pushed back.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Sep 1st, 2007
0

Re: How do I read a text file into a 2D vector?

Ok Whose Who
Reputation Points: 8
Solved Threads: 0
Newbie Poster
youhadmeathello is offline Offline
1 posts
since Sep 2007
Sep 1st, 2007
0

Re: How do I read a text file into a 2D vector?

I now have a problem with the compiler on that code:

C++ Syntax (Toggle Plain Text)
  1. istringstream ss (row);
  2. while(ch >> ss){

Noe, the first line gives me a C2664 error while the second line gives me tons of C2784 errors. Somehow I think we are implicitly trying to insert one variable type into another. How do you suggest I get rid of the compile errors?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Crouchinho is offline Offline
15 posts
since Sep 2007
Sep 1st, 2007
0

Re: How do I read a text file into a 2D vector?

I was trying to come up with some fancy output using std::copy, a la this, but the syntax eludes me.

Relevant portions:
c++ Syntax (Toggle Plain Text)
  1. std::ostream& operator<< ( std::ostream &os, const std::vector<int> &row )
  2. {
  3. std::copy(row.begin(), row.end(),
  4. std::ostream_iterator<int>(std::cout, " "));
  5. return os;
  6. }
c++ Syntax (Toggle Plain Text)
  1. std::copy(array.begin(), array.end(),
  2. std::ostream_iterator<std::vector<int> >(std::cout, "\n"));
I'm gonna hafta break down and reconfigure STLfilt to try to discern my error messages.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Sep 1st, 2007
0

Re: How do I read a text file into a 2D vector?

while ( ss >> ch )
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: matrix division
Next Thread in C++ Forum Timeline: Reading a Registry Key path from a file





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC