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

C++ 读取文件中的最后一行(C+;+;中的文件处理),c++,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()) {

我正在读.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循环进行启动读取


文件>>acnum>>引脚>>fname>>lname>>bal在开始循环之前,以及在完成循环内的所有计算之后,我认为您可以使用

while(file)
{
   body;
 }

因为一旦您读取了所有内容,文件中将没有任何内容可读取,因此它将返回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;
}
//与定义的值进行比较
int初始化_值=10;
int acnum=初始化值;
//定义输入流
ifstream文件;
//使用输入模式将其附加到文件(由于它是输入流而冗余)
打开(“acc.txt”,ios::in);
//检查文件是否打开
如果(!file)cerr>acnum>>pin>>fname>>lname>>bal;
//测试输入
如果(acnum!=accNum){
cout您的文件格式(以空格分隔的值?)已损坏。您无法区分名称条目的结尾。
std::cin
将在下一个空格处停止,因此可能会将名称拆分。读取以下余额将失败,因为它实际上仍然是名称的一部分。 有些名称包含的空格比您预期的多,例如Guido van Rossum。您应该使用
';
'\t'
作为分隔符,并使用getline和stringstream进行读取

也不应该使用<代码>浮点来表示货币,因为表示的固有错误。请考虑这个片段:(也请看下面的链接来进一步阅读。)

#包括
#包括
int main(){
浮动f=123456789;
//打印123456792.000000

std::我想你应该在上下文中向我们展示你的其余代码。这看起来非常混乱,可能会使一个简单的问题变得更加困难。如果你的文本文件在eof处有一个换行符,你可以像以前一样读入它。删除
!=NULL
和额外的括号。完成。似乎是在一个无限循环中。下面是工作代码:-请注意,出于测试目的,我将
fstream
更改为
istringstream
,并使用
long
来避免解析20多亿个帐号值时出错。(它会打印两次错误,因为您打印的行与
accnum
参数不匹配。)@guwop69如果您的问题到目前为止还没有解决,请发布一条消息,我们可以重现您的错误。这条消息在各种方面都被破坏了-与问题中的代码相比,这是一个很大的倒退。特别是,尽管打印了
!file
的消息,它仍然会继续,尝试输入,但在使用它之前没有检查它是否工作(la
acnum==accNum
cout
语句将不会处理空输入,循环终止条件将继续执行换行终止的最后一行,尽管文件中没有更多可用输入。@Tony D about:
if(!file)coutHow I“完全错误”?也许你误解了我的观点,例如,
if(!file){cerr@Tony D,我明白了,谢谢!在这种情况下,最后一行
cout也同样适用"这同样适用于最后一行
cout Good point-文件格式在现实世界中可能会被破坏,有些人的名字中可能没有两个单词,但是对于一个作业,给定的名字和姓氏是分开解析的,这实际上并没有造成麻烦。你的建议是否有效完全取决于什么在
while
循环之前完成,以及正文中的内容…具体来说,代码需要事先尝试读取/解析一行/记录,然后在正文末尾尝试读取/解析另一行/记录。不过,这是重复编码,所以这不是最佳方法(如果你好奇的话,我在问题下面的一条评论显示了工作代码)。是的,这是正确的。非常感谢你,你的回答对我来说很有价值。发布的代码已经正确地处理了输入,有没有终止换行符。剩下的建议反映了harry的建议(尽管你在半小时前),我的评论也适用。干杯。
// 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;
}
#include <iostream>
#include <iomanip>
int main() {
    float f = 123456789;
    // prints 123456792.000000
    std::cout << std::fixed << f << '\n';
}