首页 > 解决方案 > 将字符串中的目录解析为 ofstream file.open

问题描述

我正在尝试通过命令行解析目录并根据输入的内容打开文件。命令行参数似乎是正确的,因为我之前打印它以确保,但位置文件似乎没有得到修改的。

如果我传递字符串:A:/Documents/fstreamtest/test.txt(.txt 和 cpp 文件的位置),文件将保持不变

如果我通过字符串: test.txt 它似乎工作

对于不同的目录,我已经尝试过使用 '/' 和 '\\' 。

int main(int argc, char const *argv[])
{
    string str;
    string fileName;
    ofstream file;


    if(argc>=2){
        fileName = argv[1];
        cout << fileName.c_str() << endl;
        file.open(fileName.c_str());
    } else{
        file.open("wordlist.txt");
    }

    cout << "Enter a value..." << endl;
    cin >> str;

    file << "test\n" << "\n";
    vector<char> cha(str.c_str(), str.c_str() + str.size() + 1);

    for(int i = 0; cha[i]!='\0'; i++){
        cout << cha[i] << endl;
    }

    return 0;
}

标签: c++windowsfstreamiostream

解决方案


推荐阅读