Reading specific data from TXT file

I need to know how 4 god sake I mannage to read specific data from txt file.

for example if in my txt file is ==>

1.txt
I want to go home, and , that , is number : 20

and i want to get only "and" "that" and 20 from this line

how do I do that ?

thenks a lot for any advice...
You need to read the entire line and then pick out the parts you want.
I know that ...
I need 2 know how I do that ...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main () {
  string line;
  ifstream myfile ("example.txt");
  if (myfile.is_open())
  {
    while ( myfile.good() )
    {
      getline (myfile,line);
	  cout << line<<endl;
    }
    myfile.close();
  }

  else cout << "Unable to open file"; 

  return 0;
}


what do i neet 2 add??
after line 14 add
1
2
3
4
5
6
7
8
  std::string part;
  std::stringstream ss(line);
  std::getline(ss, part, ',');
// dismiss
  std::getline(ss, part, ',');
// do something (with and)
  std::getline(ss, part, ',');
// do something (with that) 
thanks a lot bro but it fails to compile...

http://www.up2me.co.il/images/76673929.png

please help me I'm in realy mess here...
you need to #include <sstream>
bro god bless you !!!

but now how do I copy the name at the "part" to a string ?
the strcpy() function fail 2 work ...

and really thank you 4 your help
with (std::)string you can simply assign(=) one string to another.

1
2
3
string a = "a";
string b = "b";
a = b


when using namespace std; you can omit that std:: but I won't recommend that
Thank you a lot bro you help me to pass this ...

have a good day !
:)
I have a little problem, what about if I need to copy for example

Station Name : A1

and I need to copy "A1"... ?

i mean to copy after a specific sing like here the ==> :
Last edited on
you can do that with std::stringstream as well or like that:

1
2
3
4
5
6
  std::string x = "Station Name : A1";
  std::string y;

  const std::string::size_type pos = x.find(":");
  if(pos != std::string::npos)
    y = x.substr(pos + 1);
your the king !
It works great!
Topic archived. No new replies allowed.