我正在尝试使用一个函数在 c++ 中将文本文件读入数组
I'm trying to use a function to read text files into arrays in c++
我正在尝试将两个文本文件读取到单独的数组中,但是当我调试时,从文件中读取的数字作为垃圾出现。我认为这是我放置数组的方式,但我不完全确定,或者可能是因为循环中的计数器和数组中的 i 很奇怪?
void read(ifstream &A_bank, ifstream &B_bank, string &n1, string& n2, int &i,
int& j, float &num, float &num1, float &total, float &total1,
float a[], float b[])
{
int counter = 0, counter1 = 0 ;
getline(A_bank,n1);
for(int i = 0; !A_bank.eof();i++)
{
A_bank >> a[i];
total+=a[i];
counter++;
}
getline(B_bank,n2);
for(int j = 0; !B_bank.eof();j++)
{
B_bank>>b[j];
total+=b[j];
counter1++;
}
}
你的问题之一是错误地使用eof()
函数。
请参阅:http://en.cppreference.com/w/cpp/io/basic_ios/eof
eof() 仅在上次读取操作失败时返回 true,而不是在上次读取操作是最后一个可能的读取操作时返回 true。
像这样改变你的两个循环:
for(int j = 0; /*somehow test j here: j < MAX...*/;j++)
{
int br;
if (!(B_bank>>br)) {
break;
}
b[j] = br;
total+=b[j];
counter1++;
}
相关文章:
- 文本文件中的单词链表
- 从命令行c++发送文本文件名
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 2D数组来自文本输入,中间有空格
- 如何将内容数组写入文本文件?
- 无法通过空白将文本文件行分隔为矢量
- 我正在使用嵌套的while循环来解析具有多行的文本文件,但由于某种原因,它只通过第一行,我不知道为什么
- C++将文本文件中的数据读取到结构数组中
- 在指针的帮助下,文本文件中单词的频率
- 将字符指针十六进制转换为字符串并保存在文本文件C++中
- 将值从二维数组输出到文本文件
- 如何在c++中从文本文件中逐行读取整数
- 如何创建一个QTableWidgetItem,用长文本右对齐,左边有省略号
- 从文本文件中读取时钟时间和事件时间并进行处理
- 如何从文本文件中读取值和数组
- 如何在C++中确定文本文件中的元素是字符还是数字
- 如何根据单词在文本中出现的概率输出单词
- 如何在C++中用std::cout正确显示带十六进制的字符串文本
- C++试图读取一个文件并输出到另一个文本文件
- 如何通过套接字将文本文件的内容从服务器发送到客户端