读取C++文件时如何保持位置
How to hold place when reading a file in C++?
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_name
和read_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;
}
- 将值指定给向量(2D)的向量中的某个位置
- 使用Unreal C++获取VR耳机的世界位置/方向
- 写入位置0x0000000C时发生访问冲突
- 如何将两个不同矢量的同一位置的两个元素组合在一起
- 函数何时会在c++中包含stack_Unwind_Resume调用
- GL_SHADERSTORAGE_BUFFER位置是否与其他着色器位置冲突
- 我可以信任表示整数的浮点或双精度来保持精度吗
- 如何通过保持第一个插入的位置来对列表中插入的其余元素进行排序?
- C++ 友元函数在内存位置上有何不同?
- 读取C++文件时如何保持位置
- git:将同一文件的两个位置保持同步
- 排序时如何保持数组的位置相同
- 如何在Qt中添加项目时保持在同一滚动位置
- 更改指针指向指针的位置,同时保持其他两个指针的值
- fstream搜索:位置保持在-1
- 保持光标在QTextEdit中的位置
- 如何在缩放视图时保持QGraphicsItem的大小和位置
- 如何保持流位置时,使用gzstream与gzip文件
- 如何保持控件MFC的TopMost位置
- C/ c++: do内置对象,如' int ' ' double '在程序运行期间始终保持在内存中的确切位置