943,708 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1190
  • C++ RSS
Sep 8th, 2008
0

reading a text file

Expand Post »
I am attempting to read a text file.

C++ Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 10
Solved Threads: 2
Junior Poster in Training
Nemoticchigga is offline Offline
98 posts
since Feb 2008
Sep 8th, 2008
0

Re: reading a text file

If there is no need for error checking I would read the values from the file as integers as opposed to strings.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Sep 8th, 2008
0

Re: reading a text file

how?
Last edited by Nemoticchigga; Sep 8th, 2008 at 1:43 pm.
Reputation Points: 10
Solved Threads: 2
Junior Poster in Training
Nemoticchigga is offline Offline
98 posts
since Feb 2008
Sep 8th, 2008
0

Re: reading a text file

how?
Set up your 2-D array of integers, then read values into the array with the >> operator.

C++ Syntax (Toggle Plain Text)
  1. indata >> array[i][j];
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,372 posts
since Jan 2008

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: calling DLL from MATLAB: 'No methods for class' error
Next Thread in C++ Forum Timeline: problem trying to use alpha for switch statement





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


Follow us on Twitter


© 2011 DaniWeb® LLC