How to fix this code to read this file in C++?

  c++

Here’s my code, which should be able to read the file, but it gives me this:

Error: Invalid format in file

What’s wrong in this code?

int main()
{
    std::map<std::string, std::vector<std::string>> routes;
    print_rasse();
    std::string inp;
    std::cout << "Give a name for input file: ";
    std::getline(std::cin, inp);
    std::ifstream file(inp);
    std::string row;
    if ( not file ) {
        std::cout << "Error: File could not be read." << std::endl;
        return EXIT_FAILURE;
    }
    while(getline(file, row)){
        if (count(row.begin(), row.end(),';'+!1)){
            std::cout << "Error: Invalid format in file." << std::endl;
            return EXIT_FAILURE;
        }
    }
}

The .txt file which I’m trying to read contains this:

West;Pyynikintori;0
West;Tuulensuu;0.5
West;Keskustori;1.5
West;Koskipuisto;1.8
West;Rautatieasema;2.2
West;Tulli;2.5
West;Sammonaukio;2.8
East;Sammonaukio;
East;Kaleva;0.2
East;Uintikeskus;0.3
East;Kalevanrinne;0.6
East;Hakametsa;1
East;Turtola;3
East;Hallila;5
East;Hervanta;7
East;Hervannan kampus;7.1
South;Hervannan kampus
South;Etela-Hervanta;0.4
South;Hervantajarvi;0.7
Hospital;Sammonaukio
Hospital;Kalevan kirkko;0.1
Hospital;Hippos;0.4
Hospital;TAYS;0.6
Hospital;Kaupin kampus;0.7

Source: Windows Questions C++

LEAVE A COMMENT