Advice

I need to load info from line I'm getting from txt file

any suggestion?

if you can also write down some code I'l be glad :)
You can read a line from file with getline() http://www.cplusplus.com/reference/string/getline/
getline(file,str);

to get strings and numbers from the line of text, you can use stringstream:
1
2
3
4
5
6
7
8
9
10
11
string line="abc def 123";  

stringstream ss(line);

string txt1,txt2;
int number;

ss >>txt1>>txt2>>number; // extract data from line

cout <<"text: "<<txt1<<" "<<txt2<<endl;
cout <<"number: "<<number<<endl;
Last edited on
the code dose not compile :(

I don't know what to do...

I Have a txt file ....

#station name
Station Name : A1
#octan of fuel 6.54 full service price 6.40 self service
Octan95,6.54,6.40
Octan98,8.30,8.15
#carNum,Octan,numOfLiters,Kind of service
22-334-55,95,31.3,FullService
22-334-55,95,31.3,SelfService
11-444-77,95,12,FullService
11-444-77,95,44.1,FullService
11-444-77,95,11.22,SelfService

and i need to take only the ----A1-----
and after that only the ----octan95----

and so on ...

please try to help me...
What code?
the code
string line="abc def 123";

stringstream ss(line);

string txt1,txt2;
int number;

ss >>txt1>>txt2>>number; // extract data from line

cout <<"text: "<<txt1<<" "<<txt2<<endl;
cout <<"number: "<<number<<endl;


all i have to do is take specific words from the txt file

#station name
Station Name : A1
#octan of fuel 6.54 full service price 6.40 self service
Octan95,6.54,6.40
Octan98,8.30,8.15
#carNum,Octan,numOfLiters,Kind of service
22-334-55,95,31.3,FullService
22-334-55,95,31.3,SelfService
11-444-77,95,12,FullService
11-444-77,95,44.1,FullService
11-444-77,95,11.22,SelfService

[code]Your code goes here[/code] (I wonder if this is going to work)

Basic structure of a c++ program:
1
2
3
4
5
6
7
#include <whatever_library_you_use>

int main() //or with arguments int main(int n, char **args)
{
  //Main logic here
  return 0;
}


To use string, stringstream, cout you need to include the headers: #include<string> , #include<sstream> , #include<iostream> respectively.
Don't forget that they are in the std namespace.

@Null give you an example, not a solution
Last edited on
@Null give you an example, not a solution

I don't understand. Are you saying that I gave a complete solution?
No, I'm saying that you just show him/it/her a way of reading.
Topic archived. No new replies allowed.