Failing to read file loaded with ifstream

前端 未结 2 1655
名媛妹妹
名媛妹妹 2021-01-23 05:34
void bot_manager_item::create_games()
{
    games.clear();
    std::ifstream paths_in(\"C:\\\\Users\\\\bill hank\\\\Documents\\\\bot_plugins\\\\directory_listing.txt\",          


        
2条回答
  •  时光取名叫无心
    2021-01-23 06:00

    Out of curiosity, does this code output the contents of the file correctly? If this code works, then the problem is something else. If this code doesn't work, then that likely means that the file either isn't where you specified, or you don't have read permissions on it.

    void bot_manager_item::create_games() {
        std::ifstream paths_in("C:\\Users\\bill hank\\Documents\\bot_plugins\\directory_listing.txt");
    
        char q[5000];
        while (paths_in.getline(q, 5000)) {
            std::cout << q << std::endl;
        }
    }
    

    This code does a few minor things differently.

    1. std::ios::in doesn't need to be explicitly specified for std::ifstream.

    2. it doesn't use is_good, while that should be fine, you can just treat the std::ifstream as a bool which will be true when it is in a good state.

    3. getline() returns a reference to the stream it operated on, so you can just put that whole line in the condition.

    4. cosmetic, but no need to explicitly close the ifstream if it is about to go out of scope.

提交回复
热议问题