读取文件中的最后一行(c++中的文件处理)

Reading last line in file(File Handling in C++)

本文关键字:文件 一行 c++ 处理 最后 读取      更新时间:2023-10-16

我正在阅读。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 intuint64_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;
}