二进制文件仅覆盖第一行 C++

发布于 2024-11-05 00:09:08 字数 1838 浏览 1 评论 0原文

所以我有一个创建并初始化的二进制文件。如果我将指针设置为 seekg = 0seekp = 0,那么我可以很好地覆盖该文本行。但是,如果我向前跳转 26 个字节(我的文件一行的大小,并且我已经确认了这一点),它会拒绝覆盖。相反,它只是将其添加到二进制数据之前,并将旧数据进一步推到线上。我希望数据完全被覆盖。

char space1[2] = { ',' , ' '};
int main()
{
    CarHashFile lead;
    lead.createFile(8, cout);
    fstream in;
    char* tempS;
    tempS = new char[25];
    in.open("CarHash.dat", ios::binary | ios::in | ios::out);
    int x = 2000;
    for(int i = 0; i < 6; i++)
        tempS[i] = 'a';
    int T = 30;
    in.seekp(26);        //Start of second line
    in.write(tempS, 6);    //Will not delete anything, will push
    in.write(space1, sizeof(space1));  //contents back
    in.write((char *)(&T), sizeof(T));
    in.write(space1, sizeof(space1));
    in.write(tempS,6);
    in.write(space1, sizeof(space1));
    in.write((char *)&x, sizeof(x));
    //Now we will use seekp(0) and write to the first line
    //it WILL overwrite the first line perfectly fine
    in.seekp(0);
    in.write(tempS, 6);
    in.write((char*) &x, sizeof(x));
    in.write(tempS, 6);
    in.write((char *) &T, sizeof(T));
    return 0;
}

CarHashFile 是一个外部类,当调用 create file 时,它​​会创建一个包含以下内容的二进制文件: "Free, " 1900 “,黑色,$” 0.00f。 引号中的所有内容都作为字符串添加,1900 作为整数添加,0.00f 显然作为浮点数添加。我通过写入添加了所有这些,所以我很确定它是一个实际的二进制文件,我只是不知道为什么它只选择在第一行上写入。我知道文件大小是正确的,因为如果我设置 seekp = 26 它将在第二行的开头打印并将其向下推。创建 space 是为了轻松地将“,”组合添加到文件中,为了简单起见,还有一个 char dol[1] = '$' 数组和一个 char nl[1] = '\n' 让我向二进制文件添加一个新行(只是尝试删除该二进制添加,它强制所有内容到一行,所以据我所知,它是必需的)。

编辑:好吧,它一直在删除该行,只是没有插入新行(有点尴尬)。但现在我不知道如何在文件中插入换行符。我尝试按照最初使用 char nl[1] = { '\n' } 的方式编写它。当我第一次创建文件时这有效,但之后就不行了。还有其他方法可以添加线条吗?我也尝试过 << endl 却什么也没得到。

So I have a binary file that I create and initialize. If I set my pointer to seekg = 0 or seekp = 0, then I can overwrite the line of text fine. However if I jump ahead 26 bytes (the size of one line of my file and something I have certainly confirmed), it refuses to overwrite. Instead it just adds it before the binary data and pushes the old data further onto the line. I want the data completely overwritten.

char space1[2] = { ',' , ' '};
int main()
{
    CarHashFile lead;
    lead.createFile(8, cout);
    fstream in;
    char* tempS;
    tempS = new char[25];
    in.open("CarHash.dat", ios::binary | ios::in | ios::out);
    int x = 2000;
    for(int i = 0; i < 6; i++)
        tempS[i] = 'a';
    int T = 30;
    in.seekp(26);        //Start of second line
    in.write(tempS, 6);    //Will not delete anything, will push
    in.write(space1, sizeof(space1));  //contents back
    in.write((char *)(&T), sizeof(T));
    in.write(space1, sizeof(space1));
    in.write(tempS,6);
    in.write(space1, sizeof(space1));
    in.write((char *)&x, sizeof(x));
    //Now we will use seekp(0) and write to the first line
    //it WILL overwrite the first line perfectly fine
    in.seekp(0);
    in.write(tempS, 6);
    in.write((char*) &x, sizeof(x));
    in.write(tempS, 6);
    in.write((char *) &T, sizeof(T));
    return 0;
}

The CarHashFile is an outside class that creates a binary file full of the following contents when create file is invoked: "Free, " 1900 ", Black, $" 0.00f.
Everything enclosed in quotes was added as a string, 1900 as an int, and 0.00f as a float obviously. I added all of these through write, so I'm pretty sure it's an actual binary file, I just don't know why it only chooses to write over the first line. I know the file size is correct because if I set seekp = 26 it will print at the beginning of the second line and push it down. space was created to easily add the ", " combo to the file, there is also a char dol[1] = '$' array for simplicity and a char nl[1] = '\n' that lets me add a new line to the binary file (just tried removing that binary add and it forced everything onto one row, so afaik, its needed).

EDIT: Ok so, it was erasing the line all along, it just wasn't putting in a new line (kind of embarrassing). But now I can't figure out how to insert a newline into the file. I tried writing it the way I originally did with char nl[1] = { '\n' }. That worked when I first created the file, but won't afterwards. Are there any other ways to add lines? I also tried in << endl and got nothing.

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

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

发布评论

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

评论(2

贪恋 2024-11-12 00:09:09

我建议一次迈出一步。代码对我来说看起来不错,但缺乏错误检查将意味着任何行为都可能发生。
为所有操作添加错误检查和报告。
如果没有显示任何问题,请进行简单的查找,然后写入

result = in.pseek(26);
//print result 
result = in.write("Hello World",10);
// print result
in.close();

让我们知道发生了什么

I suggest taking this one step at a time. the code looks OK to me, but lack of error checking will mean any behavior could be happening.
Add error checks and reporting to all operations on in.
If that shows no issues, do a simple seek then write

result = in.pseek(26);
//print result 
result = in.write("Hello World",10);
// print result
in.close();

lets know what happens

春夜浅 2024-11-12 00:09:09

最终的问题不是我对文件流的理解。这是我对二进制文件缺乏理解。换行符把一切都搞砸了,虽然它可以在某个时间点很好地添加,但以后处理它却是一个巨大的麻烦。一旦我删除了它,其他一切就都井然有序了。存在大量错误检查或缺少关闭文件的原因是因为它只是驱动程序代码。它尽可能简单,我真的不在乎当时文件发生了什么,我知道它正在被打开。为什么要浪费我的时间?最终版本在重写主程序时进行了错误检查。就像我说的,我没有得到的是二进制文件,而不是文件流。所以 AJ 的回应根本没有多大用处。作为作业的一部分,我必须有 25 个字符,没有名字长度是 25 个字符,所以它会被垃圾填满。它是该项目的副产品,我对此无能为力,除了尝试用空格填充它之外,这比跳过并从那里开始编写需要更多的时间。所以我选择写出可能的平均名称(8 个字符),然后直接跳到 25 个字符。我可以说这里给出的唯一真正的解决方案来自埃米尔,他告诉我要使用十六进制编辑器。这真的很有帮助。感谢您抽出时间。

The end problem wasn't my understand of file streams. It was my lack of understanding of binary files. The newline screwed everything up royally, and while it could be added fine at one point in time, dealing with it later was a huge hassle. Once I removed that, everything else fell into place just fine. And the reason a lot of error checking or lack of closing files is there is because its just driver code. Its as bare bones as possible, I really didn't care what happened to the file at that point in time and I knew it was being opened. Why waste my time? The final version has error checks, when the main program was rewritten. And like I said, what I didn't get was binary files, not file streams. So AJ's response wasn't very useful, at all. And I had to have 25 characters as part of the assignment, no name is 25 characters long, so it gets filled up with junk. Its a byproduct of the project, nothing I can do about it, other than try and fill it with spaces, which just takes more time than skipping ahead and writing from there. So I chose to write what would probably be the average name (8 chars) and then just jump ahead 25 afterwards. The only real solution I could say that was given here was from Emile, who told me to get a Hex Editor. THAT really helped. Thanks for your time.

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