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

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2007
Posts: 15
Reputation: Crouchinho is an unknown quantity at this point 
Solved Threads: 0
Crouchinho Crouchinho is offline Offline
Newbie Poster

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

 
0
  #1
Sep 1st, 2007
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:

  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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,362
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 242
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

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

 
0
  #2
Sep 1st, 2007
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.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 15
Reputation: Crouchinho is an unknown quantity at this point 
Solved Threads: 0
Crouchinho Crouchinho is offline Offline
Newbie Poster

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

 
0
  #3
Sep 1st, 2007
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.

  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
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,362
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 242
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

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

 
0
  #4
Sep 1st, 2007
For input I'm kinda working with something like this.
  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. }
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 15
Reputation: Crouchinho is an unknown quantity at this point 
Solved Threads: 0
Crouchinho Crouchinho is offline Offline
Newbie Poster

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

 
0
  #5
Sep 1st, 2007
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:

  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);

?
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,362
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 242
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

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

 
0
  #6
Sep 1st, 2007
Originally Posted by Crouchinho View Post
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.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 1
Reputation: youhadmeathello is an unknown quantity at this point 
Solved Threads: 0
youhadmeathello youhadmeathello is offline Offline
Newbie Poster

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

 
0
  #7
Sep 1st, 2007
Ok Whose Who
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 15
Reputation: Crouchinho is an unknown quantity at this point 
Solved Threads: 0
Crouchinho Crouchinho is offline Offline
Newbie Poster

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

 
0
  #8
Sep 1st, 2007
I now have a problem with the compiler on that code:

  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?
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,362
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 242
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

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

 
0
  #9
Sep 1st, 2007
I was trying to come up with some fancy output using std::copy, a la this, but the syntax eludes me.

Relevant portions:
  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. }
  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.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,362
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 242
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

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

 
0
  #10
Sep 1st, 2007
while ( ss >> ch )
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC