I wonder how to make a easy save function so I can save variables and later reitreve them. And if it's not to hard to do, so the savefiles cannot be opened exept by my programm?
I'm a beginner so try to show me a easy way if there are any
I wonder how to make a easy save function so I can save variables and later reitreve them. And if it's not to hard to do, so the savefiles cannot be opened exept by my programm?
I'm a beginner so try to show me a easy way if there are any
you could save it in a binary file.
im assuming the things you want to save arent dynamic arrays etc
to read a file, you replace ReWrite with Reset and BlockWrite with BlockReadCode:var f: File; // this is the file handle b: Byte; // value to write dw: DWord; // value to write Begin // first we need to assign a filename to the file handle AssignFile(F, 'C:\this is the name of our file'); // now create (and reset to 0 bytes if it allready exists) and open ReWrite(F, 1); // assign some values to b := 100; dw := 2000; // the data i will write is 1 byte and 2 dwords (creating a 9 byte file) // this writes the actual data BlockWrite(F, b, 1); // a byte has a size of 1 BlockWrite(F, dw, 4); // a dword has a size of 4 // if you dont know the size of something you can also do this BlockWrite(F, dw, SizeOf(DWord); // we must free all handles when finished CloseFile(F); end;
I would use SizeOf() all the time, except use the variable name as the parameter instead the type. Then it still works even if the variable type changes.Originally Posted by tux
BlockWrite(F, dw, SizeOf(dw));
Also, if you use strings to store names or others text data... you'll want to limit your string that is read. Or if you wish to store an undeterminate amount of text, be sure to store an index of the length of that string.
Variable string length
[pascal]var CharName: String;
CharNameLen: Integer;
{Manipulate the data in your game/app...}
{Time to save it!}
CharNameLen := Length(CharName);
if (CharNameLen > 0) then
BlockWrite(FileStream, CharName, SizeOf(CharNameLen));[/pascal]
Fixed string length
[pascal]var CharName: String[12];
// Notice that CharNameLen is not declared?
{Manipulate the data in your game/app...}
{Time to save it!}
BlockWrite(FileStream, CharName, SizeOf(CharName));[/pascal]
Can you pass a String type to BlockWrite()? I thought you would have had to do some trickery such as
BlockWrite(F, PChar(MyString)^, Length(MyString));
Oh, and I believe you got the variable length string example wrong.
[pascal]
var
CharName: String;
CharNameLen: Integer;
{ To write a variable-length string }
begin
CharNameLen := Length(CharName);
{ Write the length of the string }
BlockWrite(F, CharNameLen, SizeOf(CharNameLen));
{ Now write the contents of the string }
if CharNameLen > 0 then
BlockWrite(F, PChar(CharName)^, CharNameLen);
end;
{ To read a variable-length string }
begin
{ Read the length of the string }
BlockRead(F, CharNameLen, SizeOf(CharNameLen));
{ Set the string to the required size }
SetLength(CharName, CharNameLen);
{ Read the contents of the string }
if CharNameLen > 0 then
BlockRead(F, PChar(CharName)^, CharNameLen);
end;
[/pascal]
Correct Sly, that's the process to write a string. However the mentioned string beforehand was a 12 byte array, so technically his method was right as well.
WILL's second example was a fixed-length string. His first example was a long string which cannot just be passed to BlockWrite() as-is.Originally Posted by Robert Kosek
Yes, you are right Sly. I forgot to write the CharNameLen value first.
However you do not have to play with PChar's Pascal isn't evil that way. I've never had an issue with writting straight from a string to a BlockWrite function.
Of course I have in the past seen how some have used interesting methods of Null terminating the string stored using a #0 value and you know to move on to the next thing. But such a thing is reminicent of thold DOS days where that extra space was vital with such memory restrictions that no longer exist.
Maybe it is just with the stream methods that you can not pass a long string directly to them. Since BlockWrite is in the System unit, it probably knows how to handle long strings like the ReadLn and WriteLn functions do. TStream.Write() and related methods however do not know how to handle long strings, so you have to do the PChar trickery.
As a side note, I tried to look up BlockWrite in the Delphi 2005 help file. It has no separate help page any more. It no longer even tells you the parameter list for it. Just a list of I/O functions in the System unit. I had to load up Delphi 7 and use its help file to get more than a one line description of the function.
Bookmarks