简体   繁体   中英

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. 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.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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