954,505 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

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

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)?

linq
Light Poster
32 posts since Sep 2006
Reputation Points: 57
Solved Threads: 0
 

Reading from a file works the same way as reading from any other istream, such as cin:

//read up to newline
std::string str;
std::getline(cin, str);
//read up to whitespace (after a word or character)
std::string str;
cin >> str;


With files, replacecin with the name of your ifstream

Bench
Posting Pro
577 posts since Feb 2006
Reputation Points: 307
Solved Threads: 63
 

Reading from a file works the same way as reading from any other istream, such as cin:

//read up to newline
std::string str;
std::getline(cin, str);
//read up to whitespace (after a word or character)
std::string str;
cin >> str;
With files, replacecin with the name of your ifstream


and just repeat that three times.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

yes,but that is read one line.
how to jump to the second line?

linq
Light Poster
32 posts since Sep 2006
Reputation Points: 57
Solved Threads: 0
 
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.

Bench
Posting Pro
577 posts since Feb 2006
Reputation Points: 307
Solved Threads: 63
 
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

linq
Light Poster
32 posts since Sep 2006
Reputation Points: 57
Solved Threads: 0
 
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.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

but how to use pointer to move the reading postion? point to what?

linq
Light Poster
32 posts since Sep 2006
Reputation Points: 57
Solved Threads: 0
 
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?

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 
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)

#include <iostream>
#include <sstream>

int main()
{
    std::stringstream ss("the quick brown fox jumped over the lazy dog");
    std::string str;
    const int position(3);

    for(int i(0); i!=position; ++i)
        ss >> str;

    std::cout << str;
}


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.

Bench
Posting Pro
577 posts since Feb 2006
Reputation Points: 307
Solved Threads: 63
 

is he/she looking for the 3rd word or line?

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

what is the type of "int position(3) ", different from "int a[3]"

I got one word every line of .txt file, if using this, how to move to the second or third line and read that word out?

linq
Light Poster
32 posts since Sep 2006
Reputation Points: 57
Solved Threads: 0
 

How about this?

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
   ifstream read ( "c:\\yourfile.txt" );

   string my_string;
   int counter = 0;
   while (getline(read, my_string, '\n'))
  {
      if (counter ==2 )
      {
         cout<<"hey you hit the third line " << my_string;
      }
     counter = counter + 1;
 }
  read.close();
  cin.get();
}
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 
what is the type of "int position(3) ", different from "int a[3]"

Yes, those are completely different! the line int position(3); does the same thing as int position = 3;

In case you're wondering why I used the ( ) instead of assignment operator for built-in types is something of a style issue. Personally, I find it more idiomatic to initialise variables using the 'constructor' initialisation syntax rather than the assignment syntax. YMMV, sorry for the confusion :)

Bench
Posting Pro
577 posts since Feb 2006
Reputation Points: 307
Solved Threads: 63
 

Thank you to all of you!
I made a mistake, missed the line "and just repeat that three times" which Bench posted, or I could understand it earlier.

linq
Light Poster
32 posts since Sep 2006
Reputation Points: 57
Solved Threads: 0
 
Thank you to all of you! I made a mistake, missed the line "and just repeat that three times" which Bench posted, or I could understand it earlier.


Really!?!? Then maybe instead of wasting 6 hours, if you had just postedyour code, it could have been corrected in an hour.

WaltP
Posting Sage w/ dash of thyme
Moderator
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You