作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这是一个需要将输出保存到用户指定的数据文件的程序,但是它似乎没有保存,而且我不确定为什么,我对C++来说还比较陌生,因此可以提供任何帮助
cout << "Press 's' then 'Enter' to save the file or any other key then 'Enter' to display";
cin >> save;
if (save != 's')
{
cout << "Ix = " << Ix << "A\n";
cout << "Iy = " << Iy << "A\n";
cout << "Vz = " << Vz << "V\n";
}
else
{
cout << "Please enter a name for your file: \n";
cin >> filename;
cout << " Please enter a directory to save your file in: \n";
cin >> filepath;
ofstream file((filepath + "/" + filename).c_str());
//input is being writen to the file
file << "Ix = " << Ix << "A\n";
file << "Iy = " << Iy << "A\n";
file << "Vz = " << Vz << "V\n";
file << flush;
file.close();
}
最佳答案
欢迎来到SO。
打开文件流时,首先必须检查打开操作是否成功。
您可以这样做:
if(!file) { /* file isn't "good", open seems to have failed */}
/* or */
if(!file.good()) { /* file isn't good */ }
std::ofstream
类将不会自动创建所需的目录。
关于c++ - 用户指定如何保存数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61206625/
我有 json 数据: { "products": [ { "productId" : 0, "productImg" : "../img/product-ph
我是一名优秀的程序员,十分优秀!