string and file

Hi!
I'm trying to read lines from a text file. The file is like:

name1 surname1 number
number_1 number_2 .. number_n
name2 surname 2 number
..

I have to print on screen
<name surname average_number>
for each person.

I thought to save each line in a string variable but I don't know how to find each word in a line.

Last edited on
This is a start:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
#include <fstream>
#include <string>







int main()



{

	std::string line;

	std::ifstream myfile ("p022_names.txt");

	if (myfile.is_open())

    {

    	while ( getline (myfile,line))

    	{

    		std::cout << line << std::endl;

    	}

    	myfile.close();

  	}

  	else

  	{ 

  		std::cout << "Unable to open file"; 

	}

  	return 0;

}

Last edited on
You have several options for getting words out of a line. You can repeatedly call your string's find member function to find each space (and then get substrings), but that's needlessly complicated.

There's a simpler solution: just create an std::istringstream: std::istringstream wordstream(your_line); //#include <sstream> From there, your istringstream behaves quite similarly to std::cin, although it reads from a string instead of standard input. You can use the >> operator or repeatedly call std::getline with the third parameter set to ' '.

Alternatively, if you have access to Boost, boost::split is also an option.

Happy coding!
-Albatross
Sorry Albatross, but I don't understand how to use istingstream.
Can you make example?
I'm not good with c++..
I'm not good with c++..

You'll never be good with C++ until you start to actually write code yourself. If you want to use an stringstream, you should first "research" that term to see how you might be able to use that idea in your own code.

Last edited on
I'm not good with c++.

Do you know how to use <iostream>? Extraction of data from std::cin using operator>>, insertion of data into std::cout using operator<<?

If you can do that you already know quite a lot to use a std::stringstream.

https://en.cppreference.com/w/cpp/io/basic_istream/operator_gtgt

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <iomanip>
#include <sstream>
 
int main()
{
    // create a string
    std::string input = "41 3.14 false hello world";

    // create an input stream (like std::cin) that contains the string's contents
    std::istringstream stream(input);

    int n;
    double f;
    bool b;
 
    // extract the data and stuff into the variables
    stream >> n >> f >> std::boolalpha >> b;

    // display the extracted data
    std::cout << "n = " << n << '\n'
              << "f = " << f << '\n'
              << "b = " << std::boolalpha << b << '\n';
 
    // extract the rest using the streambuf overload
    stream >> std::cout.rdbuf();
    std::cout << '\n';
}
LearnCpp's tutorial page on std::stringstream:

https://www.learncpp.com/cpp-tutorial/184-stream-classes-for-strings/
Topic archived. No new replies allowed.