读取C++文件时如何保持位置

How to hold place when reading a file in C++?

本文关键字:何保持 位置 C++ 文件 读取      更新时间:2023-10-16

My infile.txt 写道:

January 31
February 28
March 31
April 30
May 31
June 30
July 31
August 31
September 30
October 31
November 30
December 31

我需要创建两个函数:一个每月从文件中收集(一次一个月(,另一个收集每个月中的天数。这是我到目前为止所拥有的:

#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
string read_month_name();
int read_num_days();
int main() {
  string month_name;
  int num_of_days;
  ifstream myfile;
  myfile.open("infile.txt");
  for ( int i = 0; i < 12; ++i ) {
      month_name = read_month_name();
      num_of_days = read_num_days();
      cout << "There are " << num_of_days << " days in " << month_name << ".n";
  }
}
string read_month_name() {
  string month;
  ifstream myfile;
  myfile.open("infile.txt");
  myfile >> month;
  return month;
}
int read_num_days() {
  int days;
  ifstream myfile;
  myfile.open("infile.txt");
  myfile >> days;
  return days;
}

问题是每次我读取文件时,我只收集"一月";作为字符串和整数。因此,我的输出如下所示:

  There are 0 days in January.
  There are 0 days in January.
  There are 0 days in January.
  There are 0 days in January.
  There are 0 days in January.
  There are 0 days in January.
  There are 0 days in January.
  There are 0 days in January.
  There are 0 days in January.
  There are 0 days in January.
  There are 0 days in January.
  There are 0 days in January.

有没有办法在我的文件中插入占位符.txt以便我总是从上次中断的地方继续阅读?

附言我知道我可以通过在 main(( 函数而不是子程序中读取文件来解决这个问题,但我需要找到一种方法来使其与两个子程序一起工作。

因此,您需要读取string int格式的.txt文件,并且必须使用2个子例程,每种类型一个子例程。此外,您希望能够设置要读取的行数。

首先,您只需要打开文件一次,因为如果您打开并关闭并再次打开,您将从头开始阅读,失去进度。因此,打开一次意味着您必须首先将打开的流传递给一个函数,然后再传递给另一个函数等。当您想要从此类函数返回值时,这会产生问题,因为您必须能够判断读取是否成功。您可以通过返回 bool 并通过引用填充结果变量来做到这一点。

即使您还剩下一些迭代,我也添加了中断,当没有什么可读取的时(请参阅设置为 20 但仅读取 12 的循环(

#include <iostream>
#include <string>
#include <fstream>
bool read_month_name(std::ifstream &, std::string &);
bool read_num_days(std::ifstream &, int &);
int main() {
    // make test file
    std::ofstream ofs("infile.txt");
    ofs << R"(January 31
February 28
March 31
April 30
May 31
June 30
July 31
August 31
September 30
October 31
November 30
December 31)";
    ofs.close();

    std::string month_name;
    int num_of_days;
    std::ifstream myfile;
    myfile.open("infile.txt");

    for (int i = 0; i < 20; ++i) 
    {
        if (read_month_name(myfile, month_name) && read_num_days(myfile, num_of_days))
            std::cout << "There are " << num_of_days << " days in " << month_name << ".n";
        else
            break;
    }
}
bool read_month_name(std::ifstream &myfile, std::string &month)
{
    return myfile >> month ? true : false;
}
bool read_num_days(std::ifstream &myfile, int &days)
{
    return myfile >> days ? true : false;
}

工作演示:http://coliru.stacked-crooked.com/view?id=805dffae9f9ea13f

尝试使用这样的ifstream

#include <iostream>
#include <fstream>
using namespace std;
int main() {
  string month_name;
  int num_of_days;
  ifstream infile("infile.txt");
  while (infile >> month_name >> num_of_days)
    cout << "There are " << num_of_days << " days in " << month_name << ".n";
}

结果就是你所期望的。

编辑:

根据read_month_nameread_num_days都要求的评论,这就是我会这样做的方式:

#include <iostream>
#include <string>
#include <fstream>
using namespace std;
string read_month_name(string line);
string read_num_days(string line);
int main() {
  string str, month_name, num_of_days;
  ifstream file("infile.txt");
  for ( int i = 0; i < 12; ++i ) {
    getline(file, str);
    month_name = read_month_name(str);
    num_of_days = read_num_days(str);
    cout << "There are " << num_of_days << " days in " << month_name << ".n";
  }
  return 0;
}
string read_month_name(string line){
  string month = "";
  for(int i = 0; i < line.size(); ++i) {
    if(line[i] == ' '){
      month = line.substr(0, i);
    }
  }
  return month;
}
string read_num_days(string line){
  string days = "";
  for(int i = 0; i < line.size(); ++i) {
    if(line[i] == ' '){
      days = line.substr(i, line.size());
    }
  }
  return days;
}

感谢您的所有帮助,但答案比我想象的要简单得多。我只需要在主函数之外声明 myfile,并且只在主函数内打开文件一次。问题是我在主函数和每个子函数中打开了我的文件。下面的代码完全按照我的需要运行:

#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
string read_month_name();
int read_num_days();
ifstream myfile;
int main() {
  string month_name;
  int num_of_days;
  myfile.open("infile.txt");
  for ( int i = 0; i < 12; ++i ) {
      month_name = read_month_name();
      num_of_days = read_num_days();
      cout << "There are " << num_of_days << " days in " << month_name << ".n";
  }
}
string read_month_name() {
  string month;
  myfile >> month;
  return month;
}
int read_num_days() {
  int days;
  myfile >> days;
  return days;
}