如何在C ++中读取文件时占有一席之地?

时间:2018-03-08 20:38:18

标签: c++ file ifstream

我的infile.txt上写着:

#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.

问题在于每次我阅读文件时,我都会收集&#34; 1月&#34 ;;作为字符串和整数。因此我的输出如下:

struct Particle
{
  double x, y, z; // x,y,z positions
  double vx, vy, vz; // x,y,z velocities
};

有没有办法可以在我的infile.txt中插入一个占位符,这样我总是可以读取我离开的地方?

  

P.S。我知道我可以通过读取main()函数中的文件而不是子程序来解决这个问题,但是 我需要找到一种方法使它适用于两个子程序。 < / strong>

3 个答案:

答案 0 :(得分:0)

尝试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;
}

答案 1 :(得分:0)

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

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

当没有任何东西可以读取时,我添加了break,即使你还有一些迭代(参见循环设置为20但只读取12)

int

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

答案 2 :(得分:0)

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

#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;
}