c++11 - 使用数组从 fstream 读取和写入?
问题描述
MAXSIZE 设置为 100,第一个菜单选项应该检查数组中是否有大小,从文件中读取并写入 title[] 数组的第一个槽。我很迷茫。
switch (menu)
{
case 1:
while (getline(infile) < MAXSIZE)
void readMovies(ifstream &infile, int year[], string title[], int &size){
string tmp_title;
int tmp_year;
while (getline(infile, tmp_title)
{
infile >> tmp_year;
infile.ignore();
year [size] = tmp_year;
title[size] = tmp_title;
size++;
}
break;}
解决方案
这是如何从一个文件复制到另一个文件的 MWE
#include <algorithm>
#include <fstream>
#include <iterator>
int main() {
std::ifstream ifs("input");
std::ofstream ofs("output");
std::copy(std::istream_iterator<int>(ifs),
std::istream_iterator<int>(),
std::ostream_iterator<int>(ofs, ", "));
}
给定input
包含内容的文件1 2 3 4 5
,该output
文件将被填充1, 2, 3, 4, 5,
。
推荐阅读
- python - 异步 SQLAlchemy 会话:使用 AsyncSession() 或 AsyncSession 提交?
- ruby-on-rails - 为什么我的 DateTime 是在 Scope Created Before 失败之前创建的?
- javascript - 来自下一个 js 的验证
- syntax-highlighting - 终端日历 khal:在令人敬畏的 wm 中使用 naughty.notify 生成时突出显示当前日期
- php - 如何检查是否通过函数设置了键
- javascript - 在状态表达式中传递道具
- python - 从最左边的逗号开始到最后一个整数数字的逗号分隔数字部分匹配的正则表达式模式是什么
- hibernate - 找不到符号 [ERROR] 符号:类 EntityEntry [ERROR] 位置:包 org.hibernate.engine
- php - 可以将现有代币用于 Laravel Sanctum
- java - 如何关闭用于启动 .jar 的终端窗口