读取文件中的最后一行(c++中的文件处理)
Reading last line in file(File Handling in C++)
我正在阅读。txt文件中的行。最后一行出了点问题。它不会读取它。我已经使用了eof()
,这不是一个很好的做法。因此,我尝试使用读取部分作为循环的参数。
在这里:
void browse(int accNum)
{
int acnum,pin;
float bal;
string fname,lname;
ifstream file;
file.open("acc.txt",ios::in);
if(file.is_open())
{
while(file>>acnum>>pin>>fname>>lname>>bal)
{
if(acnum==accNum)
{
cout<<"ACCOUNT NUMBER: "<<acnum<<"nNAME: "<<fname<<" "<<lname<<"nBALANCE: "<<bal;
break;
}
else
{
cout<<"ERROR";
}
}
}
else
{
cout<<"FILE NOT OPEN";
}
file.close();
}
我已经把它改成了while((file >> acnum >> pin >> fname >> lname >> bal) != NULL)
。它仍然不起作用。这个问题的解决方法是什么?下面是数据库的内容:
1111111111 4324 James Doe 300000
2222222222 0123 Eric Doe 10000
1234567899 1234 John Doe 444444
你的文件格式(空格分隔值?)坏了。您无法区分名称条目的结尾。std::cin
将在下一个空格处停止,因此可能会将名称分开。读取下面的余额将失败,因为它实际上仍然是名称的一部分。有些名称包含比您预期的更多的空格,例如,Guido van Rossum。您应该使用';'
或't'
作为分隔符,并使用getline和stringstream进行读取。
同样,您不应该使用float
来表示货币,因为这种表示固有的不准确性。考虑一下这个片段:(也可以看看我放在下面的链接,以便进一步阅读。)
#include <iostream>
#include <iomanip>
int main() {
float f = 123456789;
// prints 123456792.000000
std::cout << std::fixed << f << 'n';
}
最后,正如一位评论者所观察到的,您需要确保使用足够大的整数类型来保存帐户ID。当你不希望看到负值时,也可以考虑使用无符号整数类型,例如unsigned long long int
或uint64_t
。请注意,如果忽略帐户ID的数字属性,而将其存储为字符串,情况可能会更简单。
进一步阅读
- 在c++中分割字符串?
- 如何在c++中读取和解析CSV文件?
- 为什么不使用Double或Float来表示货币? 在c++中存储货币值的最佳方式
- http://en.cppreference.com/w/cpp/io/basic_istream/operator_gtgt
- http://en.cppreference.com/w/cpp/string/basic_string/getline
- 整型在c++中可以存储什么范围的值
- & lt; cstdint>vs & lt; stdint.h>
- http://en.cppreference.com/w/cpp/header/cstdint
似乎你的文件没有结束换行符,即在文件末尾有额外的行。如果您可以更改数据库并添加额外的行,则代码将正常工作。
如果不能,则必须为while循环设置启动读。
file >> acnum >> pin >> fname >> lname >> bal;
在你开始循环之前,同样在你完成循环内的所有计算之后
我想你可以使用,
while(file)
{
body;
}
因为一旦你读完所有内容,文件中就没有东西可读了,所以它会返回NULL到while循环并停止
这是一个可能的实现:
// compare against a defined value
int initilization_value = 10;
int acnum = initilization_value;
// define an input stream
ifstream file;
// attach it to the file with input mode (redundant as it is an input stream )
file.open("acc.txt", ios::in);
// check if file open
if(!file) cerr << "FILE NOT OPEN";
// extract input until the end of file
string line;
while(getline(file, line)){
stringstream s(line);
// in addition to that you can add a loop to differentiate the
// values extracted from s, depending on their type
// (isdigit(), isalpha(), isspace()), to populate your values
// in some special cases (as multiple names etc)
s >> acnum >> pin >> fname >> lname >> bal;
// test the input
if(acnum != accNum){
cout<<"wrong input variable: "<< anNum << 'n';
}
// all OK, exit the while loop
cout <<"ACCOUNT NUMBER: "<< acnum <<"nNAME: "<< fname <<" "<<lname<<"nBALANCE: "<< bal;
break;
}
- 用c++从输入文件中读取另一行
- 读取文件的最后一行并输入到链接列表时出错
- 我正在使用嵌套的while循环来解析具有多行的文本文件,但由于某种原因,它只通过第一行,我不知道为什么
- 如何使代码打印文本文件中的第一行?
- 如何格式化我的文本文件以使其不会一遍又一遍地重复同一行?
- 逐字读取文本文件中的每一行并转换为 int(无限循环或崩溃?
- 将字符串存储到文件下一行的变量中
- 为什么这个.c文件只有一行"../xx/xx.c"?
- 比较两个没有一行共同点的大文件
- 如何在c ++中以一行(水平)打印两个文件的数据?如给定的.这两种形式来自两个不同的文本文件
- 到达分隔符时跳到文本文件的下一行
- 从文本文件中读取一行并将其存储在 C++ 中的 2D 矢量中
- 从文本文件中随机选取一行
- 在获取从文件到矢量的每一行之后,数字将被更改或销毁
- 从文件中的一行读取特定数据-C++
- 从文本文件中的每一行读取数字,并计算数字重复的次数
- 为什么这一行不输出我的文本文件?
- 如何有效地从文件一行又一行地读取数据(相同类型)到多个向量(vectros的向量)
- 将文件一行的每个元素转换为整数
- 将文本文件一行一行地读入并行数组