简体   繁体   English

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

[英]Binary file only overwrites first line C++

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.如果我将指针设置为seekg = 0seekp = 0 ,那么我可以很好地覆盖文本行。 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.但是,如果我向前跳 26 个字节(我的文件的一行的大小,并且我已经确认过),它拒绝覆盖。 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 . CarHashFile是一个外部 class ,它在调用创建文件时创建一个包含以下内容的二进制文件: "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.引号中的所有内容都作为字符串添加,1900 作为 int 添加,0.00f 作为浮点数添加。 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.我知道文件大小是正确的,因为如果我设置seekp = 26它将在第二行的开头打印并将其向下推。 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).创建space以轻松将“,”组合添加到文件中,还有一个char dol[1] = '$'数组为简单起见,还有一个char nl[1] = '\n'让我添加一个新行到二进制文件(只是尝试删除该二进制添加并将所有内容强制到一行,所以afaik,它需要)。

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' } .我尝试按照最初使用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.我也尝试in << endl并没有得到任何结果。

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在 in 上的所有操作中添加错误检查和报告。如果没有问题,做一个简单的 seek 然后写

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

lets know what happens让我们知道会发生什么

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.所以 AJ 的回应一点用处都没有。 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.而且我必须有 25 个字符作为作业的一部分,没有名字是 25 个字符长,所以它被垃圾填满了。 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.所以我选择写可能是平均名称(8 个字符),然后再向前跳 25 个字符。 The only real solution I could say that was given here was from Emile, who told me to get a Hex Editor.我可以说这里给出的唯一真正的解决方案是来自 Emile,他告诉我要获得一个 Hex Editor。 THAT really helped.这真的很有帮助。 Thanks for your time.谢谢你的时间。

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

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