C++:运用fstream把“保存到txt文件里
时间: 2023-05-31 15:05:52 浏览: 65
以下是一个示例代码,将数据保存到名为“output.txt”的文本文件中:
```c++
#include <fstream>
int main()
{
// 创建输出文件流,并打开文件
std::ofstream outfile("output.txt");
// 写入数据到文件
outfile << "Hello, World!" << std::endl;
outfile << 12345 << std::endl;
outfile << 3.14 << std::endl;
// 关闭文件流
outfile.close();
return 0;
}
```
在上面的示例中,我们首先使用`std::ofstream`创建一个输出文件流,并指定要写入的文件名为“output.txt”。然后,我们可以使用`<<`运算符将数据写入文件。在每个数据后面,我们使用`std::endl`来插入一个换行符。最后,我们调用`close()`方法关闭文件流。
当运行程序后,你可以在你的项目目录下找到一个名为“output.txt”的文件,它应该包含以下内容:
```
Hello, World!
12345
3.14
```
相关问题
C++:运用fstream把引号保存到txt文件里
可以使用以下代码将引号保存到txt文件中:
```c++
#include <fstream>
int main() {
std::ofstream file("text.txt");
file << "\""; //保存双引号
file.close();
return 0;
}
```
在这个例子中,我们使用`std::ofstream`类创建了一个名为“text.txt”的文件,并将一个双引号写入文件中。使用`file.close()`关闭文件。
运用c++语言进行编写这一个人收支管理
好的,下面是一个简单的 C++ 代码示例,实现了上述课程设计的基本要求和拓展功能:
```c++
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
#include <iomanip>
using namespace std;
struct Record {
int id;
string date;
string type;
double amount;
double balance;
string currency;
string category;
string note;
};
vector<Record> records;
void addRecord(bool isIncome) {
Record r;
r.id = records.size() + 1;
r.type = isIncome ? "收入" : "支出";
cout << "请输入日期(格式为YYYY-MM-DD):";
cin >> r.date;
cout << "请输入金额:";
cin >> r.amount;
cout << "请输入货币种类:";
cin >> r.currency;
cout << "请输入分类:";
cin >> r.category;
cout << "请输入备注:";
cin >> r.note;
if (records.empty()) {
r.balance = r.amount;
} else {
Record& last = records.back();
r.balance = last.balance + (isIncome ? r.amount : -r.amount);
}
records.push_back(r);
cout << "添加成功!" << endl;
}
void showRecords(const vector<Record>& rs) {
cout << "+----+------------+--------+----------+----------+--------------+----------------+------------------------+" << endl;
cout << "| ID | 日期 | 类型 | 金额 | 余额 | 货币种类 | 分类 | 备注 |" << endl;
cout << "+----+------------+--------+----------+----------+--------------+----------------+------------------------+" << endl;
for (const auto& r : rs) {
cout << "| " << setw(2) << r.id
<< " | " << setw(10) << r.date
<< " | " << setw(6) << r.type
<< " | " << setw(8) << fixed << setprecision(2) << r.amount
<< " | " << setw(8) << fixed << setprecision(2) << r.balance
<< " | " << setw(12) << r.currency
<< " | " << setw(14) << r.category
<< " | " << setw(24) << r.note
<< " |" << endl;
}
cout << "+----+------------+--------+----------+----------+--------------+----------------+------------------------+" << endl;
}
void showDetail() {
cout << "请输入起始日期(格式为YYYY-MM-DD):";
string start, end;
cin >> start;
cout << "请输入结束日期(格式为YYYY-MM-DD):";
cin >> end;
vector<Record> rs;
for (const auto& r : records) {
if (r.date >= start && r.date <= end) {
rs.push_back(r);
}
}
if (rs.empty()) {
cout << "没有符合条件的记录!" << endl;
} else {
showRecords(rs);
}
}
void showSummary(bool byMonth) {
cout << "请输入年份:";
int year;
cin >> year;
vector<string> dates;
for (const auto& r : records) {
if (stoi(r.date.substr(0, 4)) == year) {
string date = byMonth ? r.date.substr(0, 7) : r.date.substr(0, 4);
if (find(dates.begin(), dates.end(), date) == dates.end()) {
dates.push_back(date);
}
}
}
if (dates.empty()) {
cout << "没有符合条件的记录!" << endl;
return;
}
cout << "+--------------+----------+----------+" << endl;
cout << "| 日期 | 收入 | 支出 |" << endl;
cout << "+--------------+----------+----------+" << endl;
double totalIncome = 0;
double totalExpense = 0;
for (const auto& date : dates) {
double income = 0;
double expense = 0;
for (const auto& r : records) {
if (byMonth && r.date.substr(0, 7) == date || !byMonth && r.date.substr(0, 4) == date) {
if (r.type == "收入") {
income += r.amount;
} else {
expense += r.amount;
}
}
}
totalIncome += income;
totalExpense += expense;
cout << "| " << setw(12) << date
<< " | " << setw(8) << fixed << setprecision(2) << income
<< " | " << setw(8) << fixed << setprecision(2) << expense
<< " |" << endl;
}
cout << "+--------------+----------+----------+" << endl;
cout << "| 总计 | "
<< setw(8) << fixed << setprecision(2) << totalIncome << " | "
<< setw(8) << fixed << setprecision(2) << totalExpense << " |" << endl;
cout << "+--------------+----------+----------+" << endl;
}
void saveToFile() {
ofstream ofs("records.txt");
for (const auto& r : records) {
ofs << r.id << " " << r.date << " " << r.type << " " << r.amount << " " << r.balance << " "
<< r.currency << " " << r.category << " " << r.note << endl;
}
ofs.close();
cout << "保存成功!" << endl;
}
void loadFromFile() {
ifstream ifs("records.txt");
if (!ifs) {
cout << "文件不存在!" << endl;
return;
}
records.clear();
string line;
while (getline(ifs, line)) {
Record r;
sscanf(line.c_str(), "%d %s %s %lf %lf %s %s %s",
&r.id, &r.date[0], &r.type[0], &r.amount, &r.balance, &r.currency[0], &r.category[0], &r.note[0]);
records.push_back(r);
}
ifs.close();
cout << "读取成功!" << endl;
}
int main() {
while (true) {
cout << "请选择操作:" << endl;
cout << "1. 添加收入记录" << endl;
cout << "2. 添加支出记录" << endl;
cout << "3. 显示所有记录" << endl;
cout << "4. 显示明细表" << endl;
cout << "5. 按月统计收支情况" << endl;
cout << "6. 按年统计收支情况" << endl;
cout << "7. 保存数据到文件" << endl;
cout << "8. 从文件读取数据" << endl;
cout << "0. 退出程序" << endl;
int choice;
cin >> choice;
switch (choice) {
case 1:
addRecord(true);
break;
case 2:
addRecord(false);
break;
case 3:
showRecords(records);
break;
case 4:
showDetail();
break;
case 5:
showSummary(true);
break;
case 6:
showSummary(false);
break;
case 7:
saveToFile();
break;
case 8:
loadFromFile();
break;
case 0:
return 0;
default:
cout << "无效的输入!" << endl;
}
}
return 0;
}
```
这段代码使用了结构体 `Record` 来存储每一条记录的信息,使用了 STL 容器 `vector` 来存储所有记录,利用了 C++11 的字符串格式化和输出格式控制来美化输出结果,同时也实现了文件保存和读取的功能。如果你有任何问题,可以随时问我。