简体   繁体   English

C ++文件处理中的tellg()是什么?它如何工作?

[英]What is tellg() in file handling in c++ and how does it work?

I tried to access the next characters to be read from the file using tellg() and returns the position correctly if the file has one line of text.. But when the file has more than one line it gives me some abnormal values.. I am attaching my code and the output i got below.. 我尝试使用tellg()访问要从文件读取的下一个字符,如果文件有一行文本,则正确返回位置。但是,如果文件多于一行,则会给我一些异常值。正在附加我的代码和我在下面的输出。

   #include <iostream>
   #include <fstream>

   using namespace std;

   int main()
   {
       char temp;
       ifstream ifile("C:\\Users\\admin\\Desktop\\hello.txt");
       ifile>>noskipws;
       while(ifile>>temp)
       {
           cout<<temp<<" "<<ifile.tellg()<<endl;
       }
   }

   output:
      H 3
      e 4
      l 5
      l 6
      o 7

        8
      W 9
      o 10
      r 11
      l 12
      d 13
      . 14
      . 15

        16
      ! 17
      ! 18
      ! 19

    File : Hello.txt contains 3 lines as given below..

           Hello
           World
           !!!

Don't understand why it starts with 3 in the print statement which should instead start from 1 .. when there are 2 lines it starts printing from 2 .. can anyone explain me..? 不明白为什么它在打印语句中以3开头,而应该从1 ..开始,当有2行从2 ..开始打印时,有人可以向我解释..吗?

As a matter of fact tellg() is not to return the offset of a byte in a stream, but a pos_type descriptor which is reusable by seekg(). 实际上,tellg()不是返回流中字节的偏移量,而是返回pos_type描述符,该描述符可被seekg()重用。 It will match the byte offset if the file is binary, but it is not guaranteed in a text stream. 如果文件是二进制文件,它将与字节偏移匹配,但是不能保证在文本流中。 (In a *ix it will match too, but in Windows there is no direct assignment.) (在* ix中,它也会匹配,但是在Windows中,没有直接分配。)

Open the file in binary mode, because seekg() is used with an offset. 以二进制模式打开文件,因为seekg()与偏移量一起使用。 If the modification of the file happens between two runs of your program, you'll need to store positionEof in a file. 如果文件的修改发生在程序的两次运行之间,则需要将positionEof存储在文件中。

Note: In binary mode you could actually store positionEof in an integer, but I prefer using the explicite type as long as it is possible. 注意:在二进制模式下,您实际上可以将positionEof存储为整数,但是我更喜欢使用显式类型,只要有可能。

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    streampos positionEof;

    // Record original eof position.
    ifstream instream("C:\\Users\\istvan\\Desktop\\hello.txt", ios::in | ios::binary);
    if (instream.is_open()) {
        instream.seekg(0, ios::end);
        positionEof = instream.tellg();     // store the end-of-file position
        instream.close();
    }
    else
        cout << "Record eof position: file open error" << endl;

    // Append something to the file to simulate the modification.
    ofstream outstream("C:\\Users\\istvan\\Desktop\\hello.txt", ios::app);
    if (outstream.is_open()) {
        cout << "write" << endl;
        outstream << "appended text";
        outstream.close();
    }

    // Check what was appended.
    instream.open("C:\\Users\\istvan\\Desktop\\hello.txt", ios::in | ios::binary);
    if (instream.is_open()) {
        instream.seekg(positionEof);    // Set the read position to the previous eof
        char c;
        while ( instream.get(c))
            cout << c;

        instream.close();
    }
    else
        cout << "Check modification: file open error!" << endl;

    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM