I have a function that writes a "block" of code to a file.
The problem is, it writes like it is supposed too, but at the end there are characters I didnt tell it to write(jarbled up garbage).
What causes this to happen and how can I stop it?
Printable View
I have a function that writes a "block" of code to a file.
The problem is, it writes like it is supposed too, but at the end there are characters I didnt tell it to write(jarbled up garbage).
What causes this to happen and how can I stop it?
Usually by opening the file in binary mode
post the code
Does C++ automatically open in binary? If so, can I treat it as a text file?Code:void Obj::WriteRef(char *RefBuffer, long size, char *RouteName)
{
char FilePath[255];
sprintf(FilePath, "C:\\Program Files\\Microsoft Games\\Train Simulator\\Routes\\%s\\%s.ref", RouteName, RouteName);
ofstream REF(FilePath, ios::app);
if(!REF.is_open()) {
cout << endl << "Error Writing Ref";
cin.get();
exit(1);
}
REF << endl << endl;
REF.write(RefBuffer, size);
REF.close();
}
Just to show you whats happening
Thats what it puts out... wich is fine except forCode:
static (
class ("Blah")
shape(Foo)
Align(none)
)
static (
class(Bar)
) pA ÔD@ ”tA ê@
pA ÔD@ ”tA ê@
I cant for the life of me figure out where that last bit is coming from... so odd!
I dont mean to keep bumping this thread or seem impatient, but its driving me bonkers. This is the only little bug I need to work out before the program is finished. I have searched google HIWORDs and LOWORDs and cant find any thing.
Your size is too large and you are writing random characters.
If RefBuffer is a null terminated character string just use:
orCode:REF << RefBuffer;
Otherwise, fix your code so that size is correct. If you still have problems, post the code where size is calculated.Code:REF.write(RefBuffer, strlen(RefBuffer));
Erm, why are you treating what appears to be a text file as a binary file?
You also need to show how you set up your RefBuffer, and how you calculated the length.
I used strlen and it worked. Thank you.
I got the size by opening the file, using seekg to pint at the end, then assigning the value of tellg to a const long called size. The function then returned "size" and I made
char RefBuffer[size];
Thanks Again
I am? I didnt realize it. How can I treat it as text then.Quote:
Erm, why are you treating what appears to be a text file as a binary file?