如何在c++中使用文件名打开文件?

How do I open a file in C++ using just the filename?

本文关键字:文件名 文件 c++      更新时间:2023-10-16

对我来说,在java和c++中编码时,必须获得文件路径并添加额外的''以使编译器不会混淆,这有点麻烦。有人知道我怎么只用文件名就能进行文件输入操作吗?

下面是一些示例代码:
int main(){
    char str[100];
    ifstream inStream;
    ofstream outStream;
    inStream.open("pre-html.txt"); //fails to open without file path
        if(inStream.fail()){
            cout << "File failed to open";
            exit(1);//exit(1) faster than exit(0)
        }
    //reads the data from the file
    while(inStream >> str){
        cout << str;
    }
}

有谁知道我怎么只用文件名来做输出文件操作吗?

在文件所在的目录下启动程序