0

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.

zmarine
  • 21
  • 1
  • 6
  • 2
    What does it mean to write a line of text to a binary file? – Drew Hall May 04 '11 at 22:42
  • @Drew Hall, all files are binary. A text file is just a binary file with certain restrictions and conventions and supporting functions. – Mark Ransom May 04 '11 at 22:47
  • Well not line of text. I mean just the contents of one line. The chars are stored as text, everything else is mumbled garbage. – zmarine May 04 '11 at 22:49
  • @Mark: I know that--the question is how you interpret the data. Since we can't see the output code, we don't know if he's interpreting a non-null-terminated string as if it were null terminated, or some other way of misinterpreting the file contents. – Drew Hall May 04 '11 at 22:58
  • @zmarine: How are you examining your file? Are you using a hex editor? – Emile Cormier May 04 '11 at 23:02
  • @Emile: I uh... was not >.>. I downloaded one and it all looks a LOT better, the line spacings are fine, but I noticed they are entirely dependent on bytes per row, which I can custom set. I'm not sure if my grader will use a hex editor or not, so my main question is really: why can't I add a new line to the file and see it in NotePad? I know binary files have no special characters in them, but when I create mine it lets me add newlines no problem. – zmarine May 04 '11 at 23:09
  • @zmarine: Are there non-displayable characters in your file (such as ASCII code 0)? Check with your hex editor. Non-displayable characters might be confusing Notepad. If you need a fill character in your file records, use a displayable character if you want Notepad to display things properly. – Emile Cormier May 04 '11 at 23:22
  • Indeed it would be unusual to refer to "lines" in a "binary file". The very definition (in terms of "binary" vs "text") takes the notion of ASCII lines out of the equation. – Lightness Races in Orbit May 04 '11 at 23:51

2 Answers2

0

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

mattnz
  • 517
  • 2
  • 13
0

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.

zmarine
  • 21
  • 1
  • 6