使用tellg()确定文件的长度,最终结果为-1

Determining length of file with tellg(), end result is -1

本文关键字:结果 tellg 文件 使用      更新时间:2023-10-16

我试图通过一个增加1的long来查找文件的长度,直到到达文件的末尾。它确实到达文件的末尾,然后读取EOF,就好像它是另一个值一样,导致它变成-1。我不太清楚如何阻止这种情况的发生,并获得文件的实际长度。任何帮助都将不胜感激!

我的代码,当前:

#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;
void input(ofstream&,string &InputStr);
void ErrChek(ifstream&,long&,char&);
int main(int argc, const char * argv[])
{
    char file2[] = "file2.txt";
    ofstream OutFile;
    ifstream InpFile;
    string InputStr;
    char Read;
    int Choice = 0;
    long Last = 0;
    OutFile.open(file2);
    if(OutFile.fail())
    {
        cout << "file named can not be found n";
        exit(1);
    }
    input(OutFile,InputStr);
    OutFile.close();
    InpFile.open(file2);
    cout << InpFile.tellg() << endl;
    if(InpFile.fail())
    {
        cout << "file named can not be found n";
        exit(1);
    }
    ErrChek(InpFile,Last,Read);
    InpFile.close();
    return 0;
}
void input(ofstream &OutFile,string &InputStr) //Gets input from user + writes to file
{
    cout << "Please input 1 sentence for use in the file: ";
    getline(cin,InputStr);
    OutFile << InputStr;
}
void ErrChek(ifstream &InpFile,long &Last,char &Read)
{
    while((Last = InpFile.tellg())!=EOF)
    {
        InpFile.seekg(Last,ios::beg);
        InpFile.get();
        Last = InpFile.tellg();
        cout << InpFile.tellg() << endl;
    }
}

输出:

Please input 1 sentence for use in the file: Test Sentence
0
1
2
3
4
5
6
7
8
9
10
11
12
13
-1

您的逻辑有点错误。tellg()直到文件末尾的get()之后的才会返回EOF。请注意,get()会更改文件位置,但循环条件是读取之前的tellg(),并且您在读取之后再次调用tellg(),期望它与读取之前相同,但不会。

事实上,除了有更干净的方法之外,如果你想用你的方法来做这件事,你的逻辑应该是这样的(有点伪代码):

Last = 0;
while(InpFile.get()!=EOF)
{
    Last = InpFile.tellg();
}
cout << Last << endl;

请注意,您的seekg()是不必要的,因为它将文件指针放在它已经所在的位置,为了简化起见,我已经删除了它。

上面示例中的关键是,您在读取之后获得文件位置,但在达到EOF时不要覆盖Last。我们检查get而不是tellg返回的EOF状态。

想象一个2或3字节的小文件,在脑海中或纸上处理原始代码,以更清楚地了解逻辑问题。

tellg返回当前字符的位置。如果失败,则返回-1。根据您的示例,您可以在迭代流时增加变量,或者使用@NeilKirk的链接

中描述的更简单的方法

也许这不是你想要的答案,但为什么不使用stat()呢?请参阅如何确定C中文件的大小?

关于您的代码,您必须在指定EOF(EOF=-1)之前输出tellg()值

void ErrChek(ifstream &InpFile,long &Last,char &Read)
{
    while((Last = InpFile.tellg())!=EOF)
    {
        cout << Last << endl;
        InpFile.seekg(Last,ios::beg);
        InpFile.get();
    }
}