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

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Sep 2006
Posts: 32
Reputation: linq is on a distinguished road 
Solved Threads: 0
linq linq is offline Offline
Light Poster

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

 
0
  #1
Nov 4th, 2006
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)?
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 505
Reputation: Bench has a spectacular aura about Bench has a spectacular aura about Bench has a spectacular aura about 
Solved Threads: 51
Bench's Avatar
Bench Bench is offline Offline
Posting Pro

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

 
0
  #2
Nov 4th, 2006
Reading from a file works the same way as reading from any other istream, such as cin:
  1. //read up to newline
  2. std::string str;
  3. std::getline(cin, str);
  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.
¿umop apisdn upside down?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,653
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1500
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

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

 
0
  #3
Nov 4th, 2006
Originally Posted by Bench View Post
Reading from a file works the same way as reading from any other istream, such as cin:
  1. //read up to newline
  2. std::string str;
  3. std::getline(cin, str);
  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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 32
Reputation: linq is on a distinguished road 
Solved Threads: 0
linq linq is offline Offline
Light Poster

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

 
0
  #4
Nov 4th, 2006
yes,but that is read one line.
how to jump to the second line?
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 505
Reputation: Bench has a spectacular aura about Bench has a spectacular aura about Bench has a spectacular aura about 
Solved Threads: 51
Bench's Avatar
Bench Bench is offline Offline
Posting Pro

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

 
0
  #5
Nov 4th, 2006
Originally Posted by linq View Post
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.
¿umop apisdn upside down?
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 32
Reputation: linq is on a distinguished road 
Solved Threads: 0
linq linq is offline Offline
Light Poster

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

 
0
  #6
Nov 4th, 2006
Originally Posted by Bench View Post
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,653
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1500
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

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

 
0
  #7
Nov 4th, 2006
Originally Posted by linq View Post
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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 32
Reputation: linq is on a distinguished road 
Solved Threads: 0
linq linq is offline Offline
Light Poster

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

 
0
  #8
Nov 4th, 2006
but how to use pointer to move the reading postion? point to what?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,273
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: 378
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

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

 
0
  #9
Nov 4th, 2006
Originally Posted by linq View Post
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?
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 505
Reputation: Bench has a spectacular aura about Bench has a spectacular aura about Bench has a spectacular aura about 
Solved Threads: 51
Bench's Avatar
Bench Bench is offline Offline
Posting Pro

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

 
0
  #10
Nov 4th, 2006
Originally Posted by linq View Post
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)
  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.
¿umop apisdn upside down?
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum


Views: 8887 | Replies: 15
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC