0

I am new to C++ and I am reading in a text file. The content of text file is like:

$ (first line)
2 (second)
MY NAME IS (whatever sentence with 10 or below characters)(third)
12 21 (forth)
22 22 (fifth)
221 (sixth)
fly jump run (seventh)
fish animal  (eighth)

So I need to read all of these and store them into different variables line by line and so far I'd manage to store them into string array line by line but how can I store the numbers like 12 21 in forth line into 2 different integer variables such as int b and int c?

and also like last two line how can I store the fly jump run fish animal into 5 different string variables respectively?

Basically Now I am putting them into a string array line by line and trying to access them and take them out of the array and store it.

if (file.is_open()){
        cout<<"Congratulations! Your file was successfully read!";
    while (!file.eof()){
        getline(file,line);
        txt[i]=line;
        i++;
        }
    }

Just want to store every line into variables based on their data type.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
Hanyi Koh
  • 327
  • 1
  • 4
  • 15

2 Answers2

0

The streams support streaming the content directly into the basic data types (int, double etc.). So the istream::operator>>(int&) does the work for you. The below small sample class demonstrates it by reading your sample file into the members -- hope that helps:

class Creature
{
public:

  void read(istream& stream)
  {
    string line;
    stream.ignore(10, '\n');      // skip line 1 (= $)
    stream >> m_integers[0];      // line 2 = single int
    stream.ignore(1, '\n');       // skip end of line 
    getline(stream, m_sentence);  // get the full sentence line .. 
    // and the rest ... we can read that in a single code line ... 
    stream >> m_integers[1] >> m_integers[2] >> m_integers[3] >> m_integers[4]
      >> m_integers[5] >> m_whatCanIdDo[0] >> m_whatCanIdDo[1] >> m_whatCanIdDo[2] >> m_whatIAm[0] >> m_whatIAm[1];
  }

private:
  string m_sentence;
  int    m_integers[6];
  string m_whatCanIdDo[3];
  string m_whatIAm[2];
}; 

Calling the function:

int main()
{
  ifstream file;
  file.open("creature.txt");
  Creature cr;
  cr.read(file);
  file.close();
}
mommos
  • 189
  • 5
0

There are several ways of doing this, but one of the most straightforward is to use a stringstream. To do this, copy the lines you want to tokenize from your txt array into a stringstream. Use the stream extratction operator (>>) to read out each word from that line, separated by a space, into a separate variable.

//Required headers
#include <string>
#include <sstream>

...

string word1, word2;
stringstream words(txt[lineNumber]);

words >> word1 >> word2;
//Process words

For each line you tokenize, you'll have to reset the stream.

//Read in next line
lineNumber++;
//Reset stream flags
words.clear();
//Replace the stream's input string
words.str(txt[lineNumber]);

words >> word1 >> word2;
//Process new words

You can use the same process for both integers and strings. The stream extraction operator will automatically convert strings to whatever data type you give it. However, it's up to you to make sure that the data it's trying to convert is the correct type. If you try to write a string to an int using a stringstream, the stringstream will set a fail bit and you won't get any useful output.

It's a good idea to write your input to a string, and then check whether that string is, in fact, a number, before trying to write it to an integer. But that's an entirely different topic, there are many ways to do it, and there are several other questions on this site that cover it.

bindsniper001
  • 164
  • 2
  • 13