reading a text file

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

Join Date: Feb 2008
Posts: 98
Reputation: Nemoticchigga is an unknown quantity at this point 
Solved Threads: 2
Nemoticchigga Nemoticchigga is offline Offline
Junior Poster in Training

reading a text file

 
0
  #1
Sep 8th, 2008
I am attempting to read a text file.

  1. ifstream indata;
  2. indata.open(filename);
  3. if(!indata)
  4. {
  5. // file couldn't be opened
  6. }
  7. else
  8. {
  9. // file opened
  10. }
  11.  
  12. // keep reading until end-of-file
  13. string data;
  14.  
  15. while (getline(indata, data))
  16. {
  17. //this is where i want to store values
  18. }
  19.  
  20. indata.close();

I want to read values from a text file. Im making the text file so I can read it however. This is a simple example of what I was thinking:

////text file/////
1 2 3 4
5 6 7 8
9 10 11 12
////text file/////

How do I store these values into a 2 dimensional array?
Last edited by Nemoticchigga; Sep 8th, 2008 at 1:32 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: reading a text file

 
0
  #2
Sep 8th, 2008
If there is no need for error checking I would read the values from the file as integers as opposed to strings.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 98
Reputation: Nemoticchigga is an unknown quantity at this point 
Solved Threads: 2
Nemoticchigga Nemoticchigga is offline Offline
Junior Poster in Training

Re: reading a text file

 
0
  #3
Sep 8th, 2008
how?
Last edited by Nemoticchigga; Sep 8th, 2008 at 1:43 pm.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,820
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: reading a text file

 
0
  #4
Sep 8th, 2008
Originally Posted by Nemoticchigga View Post
how?
Set up your 2-D array of integers, then read values into the array with the >> operator.

  1. indata >> array[i][j];
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