Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
180 views
in Technique[技术] by (71.8m points)

Reading .txt file into a c++ stream, some strings are consist of two separate words

I have to read a subway network described in a .txt file into a graph consisting of nodes and edges for a university project, so far I've been able to read the file and automatically check for strings, doubles or ints. Now the problem is that some station names consist of two separate words and my code will jump further to the double or int value describing the distance or time and treat that as if it were the second string of the station name.

Here's an excerpt of the .txt file describing the format :

U1 Warschauer Str -> Schlesisches Tor: 0,8 2
U1 Schlesisches Tor -> Gourlitzer Bahnhof: 0,9 2
U2 Pankow -> Vinetastrasse: 0,5 1

Line Station 1 -> Station2: Distance_in_km Travel_time_in_minutes 
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>

int main()
{
    std::ifstream datei("C:\Users\jeff\Desktop\Fortgeschrittene Algorithmen & Programmierung\Berlin.txt");
    std::string zeile;

    while (std::getline(datei, zeile))
    {
        for (int i = 0; i < zeile.size(); i++)
        {
            if (zeile[i] == ',')
            {
                zeile[i] = '.';
            };

            if (zeile[i] == '-' || zeile[i] == '>')
            {
                zeile[i] = ' ';
            };
            
            if (zeile[i] == ':')
            {
                zeile[i] = ' ';
            };


        }


        std::stringstream ausgabe(zeile);
        std::string linie, station1a, station1b, station2a, station2b;
        double c;
        int d;

        ausgabe >> linie >> station1a >> station1b >> station2a >> station2b >> c >> d;
        std::cout << "Linie : " << linie << " StationsnameSource : " << station1a << " " << station1b << " StationsnameDestination : " << station2a << " " << station2b << " Entfernung in km : " << c << " Fahrtdauer in min :  " << d << "
";
    enter code here
        
        
    }

    

    return 0;

}

I've made four strings for the station names, however if the station only has one name it will take the next string and just use that (for example the second station's string name or the double used for the distance)

Does someone have an idea how to solve this ? Any help is much appreciated.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Crude code to show you the principle.

Input file, train.txt:

U1 Warschauer Str -> Schlesisches Tor: 0,8 2
U1 Schlesisches Tor -> Gourlitzer Bahnhof: 0,9 2
U2 Pankow -> Vinetastrasse: 0,5 1

Outputs:

Station1=U1 Warschauer Str , Station2=Schlesisches Tor
Station1=U1 Schlesisches Tor , Station2=Gourlitzer Bahnhof
Station1=U2 Pankow , Station2=Vinetastrasse

This needs tuning but you'll get the idea:

#include <fstream>
#include <string>

using namespace std;

int main()
{
    ifstream ifs;
    string buf;
    int index, startIndex, n;
    char station1[36];
    char station2[36];

    ifs.open("train.txt", ifstream::in);
    while (getline(ifs, buf))           // Get characters into string, discard newline
    {
        //
        // Search for first station terminated by ->
        //
        index = 0;
        while (buf[index] != '-')
         {
            index++;
            if (buf[index] == '>')
                break;
        }
        //
        // Get first station
        //
        strncpy(station1, buf.c_str(), index);
        station1[index] = '';
        index += 3; // Past "-> "
        //
        // Search for next (last) station terminated by :
        //
        startIndex = index;
        while (buf[index] != ':')
            index++;
        //
        // Get next (last) station
        //
        n = index - startIndex;
        strncpy(station2, buf.c_str() + startIndex, n);
        station2[n] = '';
        printf("Station1=%s, Station2=%s
", station1, station2);
    }
    ifs.close();
    return 0;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
...