c++ - 将字符串中的目录解析为 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;
}
解决方案
推荐阅读
- ffmpeg - 我尝试使用 ffmpeg 将屏幕流式传输到多个源并出现错误
- python-3.x - 如何主动向团队频道发送消息
- scala - 是否可以将演员生成到流中?
- python - 在抓取网络的循环内发出连接数据名
- wpf - 无论是否有焦点,如何始终显示选定行的蓝色?
- java - 有没有办法用方法创建/定义 ArrayCopy
- laravel - 登录 Laravel 后 Session ID 发生变化
- unity3d - 添加 Google Ads 中介时 Android 资源链接失败 (Unity)
- android - Android FragmentManager:自定义动画显示错误的退出片段
- php - 使用未定义的常量 admin - 假定为“admin”(这将在 PHP 的未来版本中引发错误)