首页 > 解决方案 > 使用数组从 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;}

标签: c++11

解决方案


这是如何从一个文件复制到另一个文件的 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,


推荐阅读