为什么在使用 `seekg` 后我无法获得相同的字符串?

huangapple go评论57阅读模式
英文:

Why I can not get the same string after using `seekg`?

问题

I am tring to use seekg to move the pos and then read a line from the .txt.
然而,尽管 pos 相同,使用 seekp 后我无法获得相同的字符串(一个完整的行)。

The compiler is MinGW 11.2.0 64-bit on x86 windows 11
编译器是 MinGW 11.2.0 64 位,运行在 x86 Windows 11 上

Here is the test.txt
以下是 test.txt

Here is my code.
以下是我的代码。

And the output
以及输出

For supplement, here is the string s in memory.
此外,在内存中是字符串 s 的补充信息。

I don't know if it's the problem that there are \ts in the string.
我不知道是否因为字符串中存在 \t 而导致了问题。

So how can I solve this problem and get a completed line using seekg? It would be appreciated if someone can give me a hand.
那么,我该如何解决这个问题,使用 seekg 获取完整的一行呢?如果有人可以帮助我,将不胜感激。

英文:

I am tring to use seekg to move the pos and then read a line from the .txt.
However, I can not get the same string(a completed line) after using seekp, though the pos is same.

The compiler is MinGW 11.2.0 64-bit on x86 windows 11

Here is the test.txt

2	33.10	30.10	29.90	33.00	33.20	33.00	32.90	33.10	40.20	40.20	40.00	39.90	40.30	37.20	40.30	36.70
4	33.00	30.10	29.90	33.00	33.10	32.90	32.90	33.00	40.30	40.20	40.00	40.00	40.30	37.30	40.20	36.70
6	33.00	30.10	29.90	33.00	33.10	33.00	32.90	33.00	40.20	40.30	40.10	40.00	40.20	37.20	40.10	36.90

Here is my code.

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main(){
string file_path = "./test.txt";
string s;
// don't use seekg
fstream f(file_path);
    getline(f, s);
    int p = f.tellg();
    cout << "the cur pos is:" << p << endl;
    getline(f, s);
    cout << s << endl;
    getline(f, s);
    cout << s << endl;
    f.close();

// use seekg
    fstream t(file_path);
    t.seekg(p, ios::beg);
    p = t.tellg();
    cout << "the cur pos is:" << p << endl;
    getline(t, s);
    cout << s << endl;
}

And the output

the cur pos is:139
2	33.10	30.10	29.90	33.00	33.20	33.00	32.90	33.10	40.20	40.20	40.00	39.90	40.30	37.20	40.30	36.70
4	33.00	30.10	29.90	33.00	33.10	32.90	32.90	33.00	40.30	40.20	40.00	40.00	40.30	37.30	40.20	36.70
the cur pos is:139
2.90	33.10	40.20	40.20	40.00	39.90	40.30	37.20	40.30	36.70

For supplement, here is the string s in memory.

2\t33.10\t30.10\t29.90\t33.00\t33.20\t33.00\t32.90\t33.10\t40.20\t40.20\t40.00\t39.90\t40.30\t37.20\t40.30\t36.70

4\t33.00\t30.10\t29.90\t33.00\t33.10\t32.90\t32.90\t33.00\t40.30\t40.20\t40.00\t40.00\t40.30\t37.30\t40.20\t36.70

2.90\t33.10\t40.20\t40.20\t40.00\t39.90\t40.30\t37.20\t40.30\t36.70

I don't know if it's the problem that there are \ts in the string.

So how can I solve this problem and get a completed line using seekg? It would be appreciated if someone can give me a hand.

答案1

得分: 1

请记得指定打开模式,例如 ios::binary

我认为我已经找到了问题所在。
当我打开文件时,我应该指定模式,例如 ios::binary
如果没有这个规定,tellg() 返回的偏移量在某些情况下可能不准确。

所以我将

fstream f(file_path);

改为

fstream f(file_path, ios::in | ios::binary);

tellg() 的值变小了,这对我有效。

英文:

Remember to specify the open mode, e.g. ios::binary.

I think I have found the problem.
When I open the file, I should specific the mode, e.g., ios::binary.
Without that specification, the offset which tellg() returns may be not accurate under some conditions.

So I change

fstream f(file_path);

to

fstream f(file_path, ios::in | ios::binary);

And the value of tellg() gets smaller, which works for me.

huangapple
  • 本文由 发表于 2023年6月13日 09:18:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76461151.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定