943,706 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 453
  • C++ RSS
Sep 17th, 2008
0

opening file and putting into program

Expand Post »
I'm writing a program that opens a file then sorts it a couple different ways then closes it. Someone in my class said something about using a getline function but I can't figure out how that works. I can't find a way to take a specific line in a text file and then put it into an array. How can I get each number into its own array?



4532 111 2123
324 3211 223
233 4493 992
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
swbuko is offline Offline
12 posts
since Sep 2008
Sep 17th, 2008
0

Re: opening file and putting into program

>>but I can't figure out how that works.
C++ Syntax (Toggle Plain Text)
  1. #include <fstream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. ifstream in("filename.txt");
  8. string line;
  9. // read each line of the file
  10. while( getline( in, line) )
  11. {
  12. //blabla do something this line
  13. }
  14. }


>>I can't find a way to take a specific line in a text file
You have to read each line one at a time until you get to the line that you want.


>>How can I get each number into its own array?
What kind of array? int array?
C++ Syntax (Toggle Plain Text)
  1. #include <fstream>
  2. #include <string>
  3. #include <vector>
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. vector<int> arry; // array of integers
  9. ifstream in("filename.txt");
  10. int num;
  11. // read each line of the file
  12. while( line >> num )
  13. {
  14. arry.push_back(num); // add the number to the array
  15. }
  16. }
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,950 posts
since Aug 2005

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: another alg. problem...
Next Thread in C++ Forum Timeline: Create "Help" in application





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


Follow us on Twitter


© 2011 DaniWeb® LLC