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

Recommended Answers

All 15 Replies

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, replace cin with the name of your ifstream

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, replace cin with the name of your ifstream

and just repeat that three times.

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

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.

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

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.

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

Member Avatar for iamthwee

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?

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.

Member Avatar for iamthwee

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

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?

Member Avatar for iamthwee

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();
}

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

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.

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 posted your code, it could have been corrected in an hour.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.