c++ - 在字符串上使用 += 运算符以添加 .txt 用于文件输出
问题描述
我目前正在上大学的第一个 CompSci 课程,我的一个作业要求我输入一个名称,然后使用该名称创建一个具有相同名称但末尾带有“.txt”的文件。
它打印出它应该命名的内容,但我在尝试输入的名称下找不到任何文件。
有谁知道出了什么问题?
#include<iostream>
#include<fstream>
#include<iomanip>
#include<math.h>
using namespace std;
int main()
{
string des_name;
string ship_name;
float ship_mass;
float e_thrust;
const float g0 = 9.8;
cout << "Designer name: " << endl;
getline(cin , des_name);
cout << "Ship Name: " << endl;
cin >> ship_name;
ship_name += ".txt";
cout << "Ship Mass: " << endl;
cin >> ship_mass;
cout << "Engine Thrust: " << endl;
cin >> e_thrust;
cout << "File written for " << ship_name;
ofstream output_file;
output_file.open(des_name.c_str());
output_file << "##########" << ship_name << "##########" << endl << endl;
output_file << "Designed by: " << setw(10) << des_name;
}
解决方案
嗯,您打开一个基于 的文件des_name
,但它ship_name
是您附加.txt
到的:
ship_name += ".txt";
:
output_file.open(des_name.c_str());
我会把这个异常作为一个起点:-)
推荐阅读
- storage - 重命名 App Inventor 项目会清除 TinyDB 吗?
- php - 限制树枝循环
- python - 如何确定在 SLURM 中超出 python 脚本步骤内存的哪个点
- spring-mvc - 将 SSO 与具有本机身份验证的应用程序集成
- javascript - 在Vue中单击路由器链接上的激活方法
- c++ - 当 C++/11 auto 关键字更有效或总是按值传递时,是否将参数推导出为按引用传递?
- python - 根据其他多个条件获取列值
- apache-spark - Apache Spark 错误无法找到或加载主类 C:\spark\jars\aircompressor-0.8.jar
- java - REST webservice call for HTTPS link - 400 Bad Request error
- r - 识别R中两列中的对