【问题标题】:VS Express 2015 Win10 Application - ifstream does not open fileVS Express 2015 Win10 应用程序 - ifstream 不打开文件
【发布时间】:2017-07-30 10:31:18
【问题描述】:

VS Express 2015 C++ 中的 fstream、ifstream 和 ofstream 根本不适合我。我试图在 MingGW 中打开一个文件,这当然可以,但在 VS Studio 中它不想打开给定位置的文件。我还将文件“Artikel.txt”放在项目根目录中,但没有任何区别。 尝试使用 errno 检测错误,失败。我也无法在输出中收到错误消息。

void App1::MainPage::initArtikel()
{
    ifstream file;
    file.open(L"C:\\Users\\...\\Documents\\Visual Studio 2015\\Projects\\App1\\Artikel.txt");
    string zeile;
    Artikel neuerArtikel;
    if (file.is_open())
    {
        OutputDebugStringW(L"Artikel.csv wird geöffnet...");
        while (getline(file, zeile))
        {
            size_t pos = 0;
            string token;

            //Erste Spalte "Name"
            pos = zeile.find(CSV_DELIM); 
            token = zeile.substr(0, pos);
            neuerArtikel.Name = stops(token);
            zeile.erase(0, pos + 1);

            //Zweite Spalte "Preis"
            pos = zeile.find(CSV_DELIM);
            token = zeile.substr(0, pos);
            neuerArtikel.Preis = stod(token);
            zeile.erase(0, pos + 1);

            sortiment.push_back(neuerArtikel);
        }
        //file.close();
    }
    else
    {
        OutputDebugStringA(strerror(errno));
    }
}

【问题讨论】:

  • 尝试打开它以提供宽字符串路径。可能路径包含非 Unicode 符号。
  • 检查来自errno的错误代码。
  • edit您的问题并添加相关信息。评论不适合状态更新。

标签: c++ visual-studio-2015 fstream ifstream ofstream


【解决方案1】:

就我而言,OutputDebugStringA(strerror(errno)); 提供:

'权限被拒绝'

如果我使用file.open(Artikel.csv");,则会收到以下错误:

'没有这样的文件或目录'

实际上将文件“Artikel.csv”放在位置:

C:\Users\ ...\Documents\Visual Studio 2015\Projects\App1\Debug\App1\AppX

给我输出:

'Artikel.csv wird geöffnet'

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多