使用 ifstream 读取文件时出错

Error in file reading with ifstream

本文关键字:出错 文件 读取 ifstream 使用      更新时间:2023-10-16

我在ifstream函数中遇到问题,我尝试使用 argv[1] 作为参数,但无法加载地图,地图位于主代码的同一文件夹中。我被困在这里,无法调试。

#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
using namespace std;
int main (int argc, char *argv[]){
    int h;
    int w;
    int var;

    string inputLine;
    ifstream f;
    f.open("map.pgm",ios::in);
    if (!f){
        cout << "error" << endl;
        exit(1);
    }

我正在使用Visual Studio 2017

更改此行:

if (!f){

通过这个:

if (!f.is_open()){

顺便说一句,您可以使用GetModule文件名检查当前目录路径