Read txt file, and use the information from the text file

I have a text file that looks like this

Stop sign 58.327628, 8.546318
58.311932, 8.522903
Speed sign 58.2999645, 8.477316

The numbers are coordinates and the words are different traffic signs. If there is no text then there is no traffic sign at this coordinate.


I will use these traffic signs and coordinates further in the program.

that in the end it comes up in cmd "the stop sign at the coordinates 63.4363445, 8.346345 missing."

thanks for your help, hope you understand
So what do you want help with?

Opening a text file?
Reading a line from a file?
Reading all the lines from a file?
Figuring out where the word "sign" is in a line?
...

I want to use the information from the text file in the program.
so that I can write
if (coordinates && !stopsign) {
cout << "it lacks a stop sign at coordinates x, y";
} else {
the stop sign is there;
}

So I need to know that c ++ understands what is in the text file.
so that I can store the different coordinates and signs as variables
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
50
51
52
53
54
55
56
57
58
59
60
61
#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;

struct Sign
{
   string text;
   double x = 0, y = 0;
};

istream &operator >> ( istream &str, Sign &sign )
{
   string line;
   getline( str, line );   if ( !str ) return str;

   // Now you have to parse that line
   // I'm going to assume there are no digits in the text
   int pos = line.find_first_of( "0123456789" );
   if ( pos == string::npos )
   {
      return str;
   }
   else if ( pos == 0 )
   {
      sign.text = "None";
   }
   else
   {
      sign.text = line.substr( 0, pos );
      int q = sign.text.find_last_not_of( " " );
      if ( q != string::npos ) sign.text = sign.text.substr( 0, 1 + q );
   }

   char comma;
   stringstream( line.substr( pos ) ) >> sign.x >> comma >> sign.y;

   return str;
}

//================================================

int main()
{
// ifstream in( "signs.txt" );
   stringstream in( "Stop sign 58.327628, 8.546318   \n"
                    "58.311932, 8.522903             \n"
                    "Speed sign 58.2999645, 8.477316 \n" );

   vector<Sign> signs;
   for ( Sign s; in >> s; ) signs.push_back( s );

   cout << fixed << setprecision( 6 );
   for ( auto s : signs )
   {
      cout << "Sign: " << left << setw( 15 ) << s.text << "  at x: " << s.x << "  y: " << s.y << '\n';
   }
}


Sign: Stop sign        at x: 58.327628  y: 8.546318
Sign: None             at x: 58.311932  y: 8.522903
Sign: Speed sign       at x: 58.299965  y: 8.477316
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include<iostream>
#include<fstream>
#include<string>
using namespace std;

int main()
{
    ifstream in("proj.cpp");
    string line;
    while ( getline(in,line) ) {
        cout << "Line=" << line << endl;
    }
    return 0;
}



$ g++ proj.cpp
$ ./a.out 
Line=#include<iostream>
Line=#include<fstream>
Line=#include<string>
Line=using namespace std;
Line=
Line=int main()
Line={
Line=    ifstream in("proj.cpp");
Line=    string line;
Line=    while ( getline(in,line) ) {
Line=        cout << "Line=" << line << endl;
Line=    }
Line=    return 0;
Line=}


Now start looking through say https://en.cppreference.com/w/cpp/string/basic_string to learn how to pick apart a string to find what interests you.
Topic archived. No new replies allowed.