944,078 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 12529
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Nov 4th, 2006
0

how to move to the second line in C++ .txt file reading?

Expand Post »
if I have three words in there lines in a .txt file, and I want to read them to three string, how to do this using c++ ifstream? how to read every line(word)?
Reputation Points: 57
Solved Threads: 0
Light Poster
linq is offline Offline
32 posts
since Sep 2006
Nov 4th, 2006
0

Re: how to move to the second line in C++ .txt file reading?

Reading from a file works the same way as reading from any other istream, such as cin:
C++ Syntax (Toggle Plain Text)
  1. //read up to newline
  2. std::string str;
  3. std::getline(cin, str);
C++ Syntax (Toggle Plain Text)
  1. //read up to whitespace (after a word or character)
  2. std::string str;
  3. cin >> str;
With files, replace cin with the name of your ifstream
Last edited by Bench; Nov 4th, 2006 at 7:31 am.
Reputation Points: 307
Solved Threads: 62
Posting Pro
Bench is offline Offline
565 posts
since Feb 2006
Nov 4th, 2006
0

Re: how to move to the second line in C++ .txt file reading?

Click to Expand / Collapse  Quote originally posted by Bench ...
Reading from a file works the same way as reading from any other istream, such as cin:
C++ Syntax (Toggle Plain Text)
  1. //read up to newline
  2. std::string str;
  3. std::getline(cin, str);
C++ Syntax (Toggle Plain Text)
  1. //read up to whitespace (after a word or character)
  2. std::string str;
  3. cin >> str;
With files, replace cin with the name of your ifstream

and just repeat that three times.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,957 posts
since Aug 2005
Nov 4th, 2006
0

Re: how to move to the second line in C++ .txt file reading?

yes,but that is read one line.
how to jump to the second line?
Reputation Points: 57
Solved Threads: 0
Light Poster
linq is offline Offline
32 posts
since Sep 2006
Nov 4th, 2006
0

Re: how to move to the second line in C++ .txt file reading?

Click to Expand / Collapse  Quote originally posted by linq ...
yes,but that is read one line.
how to jump to the second line?
you can't "jump", file access is serial. if you need a certain position, read the first line(s) and discard it/them until you reach the position in the file that you need.
Reputation Points: 307
Solved Threads: 62
Posting Pro
Bench is offline Offline
565 posts
since Feb 2006
Nov 4th, 2006
0

Re: how to move to the second line in C++ .txt file reading?

Click to Expand / Collapse  Quote originally posted by Bench ...
you can't "jump", file access is serial. if you need a certain position, read the first line(s) and discard it/them until you reach the position in the file that you need.

I just want to ask how to reach the position which I want
Reputation Points: 57
Solved Threads: 0
Light Poster
linq is offline Offline
32 posts
since Sep 2006
Nov 4th, 2006
0

Re: how to move to the second line in C++ .txt file reading?

Click to Expand / Collapse  Quote originally posted by linq ...
I just want to ask how to reach the position which I want
Just repeat the code Bench posted until you get to the desired line. You will have to use a loop and a counter integer to keep track of the line number that was just read. If you are confused about loops -- which is understandable -- you should read about them in your text book.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,957 posts
since Aug 2005
Nov 4th, 2006
0

Re: how to move to the second line in C++ .txt file reading?

but how to use pointer to move the reading postion? point to what?
Reputation Points: 57
Solved Threads: 0
Light Poster
linq is offline Offline
32 posts
since Sep 2006
Nov 4th, 2006
0

Re: how to move to the second line in C++ .txt file reading?

Click to Expand / Collapse  Quote originally posted by linq ...
but how to use pointer to move the reading postion? point to what?
Looks like you need some code right? Otherwise you're just going to be going around in circles?
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Nov 4th, 2006
0

Re: how to move to the second line in C++ .txt file reading?

Click to Expand / Collapse  Quote originally posted by linq ...
but how to use pointer to move the reading postion? point to what?
Imagine file access is like an old fashioned mono-directional cassette tape player (ie, no rewinding!) - the only way to find the position of a chunk of data is to play through the tape from the start, and read each chunk of data sequentially.

The chunk of data you read is always at the "head" position - The tape moves forward, rather than the head (and since you can't rewind, the data which has already passed the head is gone from the stream). You can choose to either store the data at the head or discard it. The tape rolls on once the head has read whatever's at that position. Whatever you do, you *must* read it in order to move the tape on.

here's a quick example of reading the 3rd word in a stringstream (Which is just another 'flavour' of streams - fstreams work in exactly the same way)
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <sstream>
  3.  
  4. int main()
  5. {
  6. std::stringstream ss("the quick brown fox jumped over the lazy dog");
  7. std::string str;
  8. const int position(3);
  9.  
  10. for(int i(0); i!=position; ++i)
  11. ss >> str;
  12.  
  13. std::cout << str;
  14. }
This example outputs the word "brown" - the first 2 words, "the" and "quick" were also read, but discarded, since the only word I was interested in was the 3rd one.
Last edited by Bench; Nov 4th, 2006 at 10:51 am.
Reputation Points: 307
Solved Threads: 62
Posting Pro
Bench is offline Offline
565 posts
since Feb 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

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: Mersenne Primes Help!!
Next Thread in C++ Forum Timeline: type convert problem 2





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


Follow us on Twitter


© 2011 DaniWeb® LLC