std::ifstream无法打开c:program files(x86)下的文件

std::ifstream failed to open file under c:program files(x86)

本文关键字:x86 文件 files ifstream std program      更新时间:2023-10-16

我有管理权限,并尝试在windows上打开c:\program files(x86)下的文件。std::ifstream无法打开,错误编号为2。如果我将同一个文件复制到其他位置,它可以正常工作。

#include <iostream>    
#include <cerrno>        
#include <string>
#include <fstream>
int main()
{
    std::ifstream ifs1("C:\Program Files(x86)\somefile.html");
    if (ifs1.is_open())
    {
        std::cout << "File openedn";
    }
    else
    {
        std::cout << "File open failed. error number = " << errno << std::endl;
    }
    return 0;
}  

是因为权限问题吗?

可能是因为这个:

"C:\Program Files(x86)\somefile.html"

实际上应该是:

"C:\Program Files (x86)\somefile.html"

你只是漏掉了一个空格。