1

When I read a file from the same folder, everything is fine, but when I'm trying to read the same file from another folder, the output is many zeros.

Tour baseTour;
    std::ifstream file("10_50_5.txt");
    while(!file.eof()) 
        file >> baseTour;
    std::cout << "Udany odczyt danych z pliku\n\n";
    file.close();



std::istream& operator >> (std::istream& is, Tour& tour) {
    //std::cout << "pop size: " << p.POPULATION_SIZE << "   cit num: " << Tour::CITIES_NUMBER <<
    Tour t;
    std::vector<City> ccities;
    std::vector<int> ssalesmen;
    std::vector<Tour> ttours;
    int number, x, y, i, j, sal = 0, index = 0;
    is >> POPULATION_SIZE >> CITIES_NUMBER >> SALESMEN_NUMBER;
    std::cout << POPULATION_SIZE << "\n" << CITIES_NUMBER << "\n" << SALESMEN_NUMBER << "\n";

    for(j = 0; j < CITIES_NUMBER; j++) {
        is >> number >> x >> y;
        std::cout << number << "  " << x << "  " << y << "\n";
        ccities.push_back(City(number, x, y));
    }
    tour.setTour(ccities);
    return is;
}

I have tried with "../data/10_50_5.txt" and "/home/folder/data/10_50_5.txt".

1tm0tm1
  • 19
  • 3
  • 1
    I am not sure why it is not finding the file. By the way, get many zeros because `while(!file.eof())` doesn't work the way you think it does. https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons You should be checking if the file is open after you create the variable and before you start the loop. – Jerry Jeremiah Dec 15 '20 at 21:43

0 Answers0