同一文件上的eekp()、seekg()、read() 和write() 问题

发布于 2024-11-09 05:56:48 字数 1511 浏览 0 评论 0原文

我正在处理 C++ 中的文件问题,我无法解决。我正在用寄存器管理一个文件,我正在尝试执行“排序插入”,西班牙语是“Insercción ordenada”,但我不知道英语的翻译。我尝试做的是打开文件末尾的文件,并将寄存器与我想要插入文件中的寄存器进行比较。每个大于新寄存器的寄存器都会被移动一个寄存器的位置到文件末尾。当比较返回新寄存器小于当前选择的寄存器时,我将新寄存器写入正确的位置。

我的具体问题是文件中的最后一个寄存器被读取了两次,因此寄存器被写入了两次。

我想复制我编写的代码,但名称是西班牙语,并且代码包含大量片段来管理 char* 和结构之间的错误和转换...这是一团糟,只会让我们感到困惑。

我认为有人会遇到同样的情况,我会感谢网络上的示例或一些有关如何在同一文件上使用eekp和seekg的文章。

多谢!

或多或少的代码:

int readPosition, writePosition;

readPosition = // Byte where the last register starts in the file.
writePosition = readPosition + registerSize;

f->seekg(readPosition,ios::beg);

oldReg = new char[registerSize];
f->read(oldReg,registerSize);
f->seekp(writePosition,ios::beg);

while () { // if cadena < new register
             // For writing, I need to move the reading pointer after the writing pointer
    f->seekg(readPosition + 2*registerSize,ios::beg);
        f->write(oldReg,registerSize);

        readPosition -= registerSize;
    writePosition -= registerSize;

        f->seekg(readPosition,ios::beg);
        f->seekp(writePosition,ios::beg);

                 delete oldReg;
                 oldReg = new char[registerSize];

        f->read(oldReg,registerSize); 
    }

delete oldReg;

f->seekp(writePosition,ios::beg);
f->seekg(writePosition + registerSize,ios::beg);

f->write(newRegister,registerSize);

这不是真正的代码,而是我的方法所做的。我希望您注意我如何使用eekp()和seekg(),因为我不确定我如何使用它们。我在使用eekp和seekg时遇到了一些问题,并且我注意到这两个指针非常匹配。

I'm dealing with a problem with files in C++ I'm not able to work out. I'm managing a file with registers and I'm trying to do "Sorted Insert", in Spanish is "Insercción ordenada" but I don't know the translation to English. What I try to do is opening the file at the end of it and comparing the register with the one I want to insert in the file. Each register greater than the new is moved one register's position to the end of file. I write the new register in its correct position when the comparison returns that the new is smaller than the current selected.

My concrete problem is the last register in the file is read 2 times and because of it, the register is written 2 times.

I'd like copy the code I've written but the names are in Spanish and the code contains lots of snippets to manage errors and conversions between char*s and structs... It's a mess and it only gets confuse us.

I think someone would have been in the same situation, I will thank examples on the web or some articles about how to use seekp and seekg on the same file.

Thanks a lot!

More or less the code:

int readPosition, writePosition;

readPosition = // Byte where the last register starts in the file.
writePosition = readPosition + registerSize;

f->seekg(readPosition,ios::beg);

oldReg = new char[registerSize];
f->read(oldReg,registerSize);
f->seekp(writePosition,ios::beg);

while () { // if cadena < new register
             // For writing, I need to move the reading pointer after the writing pointer
    f->seekg(readPosition + 2*registerSize,ios::beg);
        f->write(oldReg,registerSize);

        readPosition -= registerSize;
    writePosition -= registerSize;

        f->seekg(readPosition,ios::beg);
        f->seekp(writePosition,ios::beg);

                 delete oldReg;
                 oldReg = new char[registerSize];

        f->read(oldReg,registerSize); 
    }

delete oldReg;

f->seekp(writePosition,ios::beg);
f->seekg(writePosition + registerSize,ios::beg);

f->write(newRegister,registerSize);

This is not the real code but what my method does. I want you notice how I use seekp() and seekg() because I am not sure about the way I use them. I've had several problems with seekp and seekg and I've noticed that the two pointers are very matched.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

§普罗旺斯的薰衣草 2024-11-16 05:56:48

这不起作用

f->seekg(readPosition,ios::beg);
f->seekp(writePosition,ios::beg);

一个文件只有一个位置。当你需要的时候,当你从阅读转向写作时,你必须去做你的寻求,反之亦然。

This doesn't work

f->seekg(readPosition,ios::beg);
f->seekp(writePosition,ios::beg);

A file only has one position. You have to do your seeks when you need them, when changing from reading to writing or vice versa.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文