简体   繁体   English

Tellg()函数C ++的奇怪行为

[英]Strange behaviour of tellg() function c++

I got a problem with tellg() function from std::fstream class. 我在std :: fstream类中使用tellg()函数遇到问题。 Typically it should return the position of the current character in the input stream. 通常,它应该返回当前字符在输入流中的位置。 However, it works very strange for me. 但是,它对我来说很奇怪。 Below is some short sample code: 下面是一些简短的示例代码:

#include <iostream>
#include <fstream>

using namespace std;

int main(void) {
char c;
ifstream czytaj;
czytaj.open("test_file.txt");

cout << czytaj.tellg() << endl;     //is 0 should be 0
czytaj.peek();                      //is 0 should be 0
cout << czytaj.tellg() << endl;     //is 2 should be 0    !!
czytaj.get(c);                      //is 3 should be 3
czytaj.get(c);                      //is 4 should be 4
cout << czytaj.tellg() << endl;     //is 6 should be 4    !!

int r; cin >> r;
return 0;
}

While the txt file looks like follows: 虽然txt文件如下所示:

abcdefghij
kturjbkfvd

After compilation I get output like: 编译后,我得到如下输出:

0
2
6

First use of tellg() works properly, it returns position 0 as its the beginning of the file. 第一次使用tellg()可以正常工作,它将位置0作为文件的开头。 Unfortunatelly, each next use works like it adds +2 to the position. 不幸的是,每次下一次使用都像在位置上增加+2。 As the result I get letters 'c' and 'd' extracted from the stream. 结果,我从流中提取了字母“ c”和“ d”。 Both tellg() and peek() are supposed not to change the position, so I should get letters 'a' and 'b', while the correct result should be: Tellg()和peek()都应该不改变位置,所以我应该得到字母“ a”和“ b”,而正确的结果应该是:

0
0
2

Such things happen if I use encoding ANSI in txt file. 如果我在txt文件中使用ANSI编码,则会发生此类情况。 When I change it for Unicode, it works as it should. 当我将其更改为Unicode时,它应能正常工作。 Also if I use ANSI and additionally binary mode ios::binary, it works properly as well. 另外,如果我使用ANSI和其他二进制模式ios :: binary,它也可以正常工作。 Strange fact is, on my other computer it works fine even with ANSI and without ios::binary. 奇怪的事实是,在我的另一台计算机上,即使使用ANSI并且没有ios :: binary,它也可以正常工作。 Why does it happen? 为什么会发生?

EDIT: Forgot to mention about very important fact. 编辑:忘记提及非常重要的事实。 If I remove from this sample code all lines containing tellg() , the extracting is correct - I get letters 'a' and 'b'. 如果我从此示例代码中删除了所有包含tellg() ,则提取是正确的-我得到字母“ a”和“ b”。

tellg() tells you where the next "get" position in the file is. tellg()告诉您文件中下一个“获取”位置。 Since files in for example Windows that use CR+LF ( '\\r','\\n' ) as newlines have two characters as a newline, where the C++ (and C) standard requires that a newline is LF '\\n' as a single character, when your program reads a CR+LF sequence, the C runtime counts that as one character, but the file position where you get the next character from is two steps forward. 由于在Windows中使用CR + LF( '\\r','\\n' )作为换行符的文件具有两个字符作为换行符,因此C ++(和C)标准要求换行符为LF '\\n'因为单个字符时,程序读取CR + LF序列时,C运行时会将其视为一个字符,但是从中获取下一个字符的文件位置将向前两个步骤。

had the same issue. 有同样的问题。 try to read the filestream binary: 尝试读取文件流二进制文件:

    czytaj.open("test_file.txt",ios::binary);

it helped for me 对我有帮助

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

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