Forum > Beginners

Question about strings in files

(1/1)

Septe:
I typed in an example from a tutorial that I dug up online.  It was an example on writing files to the disk.  I ran into a message stating that I can't use reference counted types in files.  Looking through the documentation, I forced the program to use short strings with {$H-}.  Now my question or more accurately a confirmation.  I noticed that my data file was 768 bytes.  Since I had three strings being written to the file, I take it that fpc pads the string (being 256 bytes in size) with something, correct?  If I wanted to just print to the length of the strings, is there something that I can look at to research this?

Thanks,
Septe

Leledumbo:

--- Quote from: Septe on April 23, 2015, 04:38:01 pm ---I noticed that my data file was 768 bytes.  Since I had three strings being written to the file, I take it that fpc pads the string (being 256 bytes in size) with something, correct?

--- End quote ---
Nope, 768 / 3 = 256, the correct size of each short string without explicit length. Read the last paragraph in the docs.

rvk:

--- Quote from: Septe on April 23, 2015, 04:38:01 pm ---... I take it that fpc pads the string (being 256 bytes in size) with something, correct?
--- End quote ---
You probably used a typed file (of type string). In that case FPC doesn't write the string to your file but a record containing the string. And that record is of a fixed size. This could be useful if you want to read the 1234'th string in a file of 9999 strings. With a textfile you would need to read the whole file and count the strings read until you get to 1234'th string.


--- Quote from: Septe on April 23, 2015, 04:38:01 pm ---If I wanted to just print to the length of the strings, is there something that I can look at to research this?

--- End quote ---
If you want to write the strings with just the length of the strings you probably want a text-file. Use Filevar: TextFile for that. (and use Writeln(Filevar, Str); to write to that file).

You can read more about that in the wiki:
http://wiki.freepascal.org/File_Handling_In_Pascal
http://wiki.lazarus.freepascal.org/File

Septe:
In this situation, it was a file of StudentRecord of which contains String.  Leledumbo answered my question.  Thanks Leledumbo.

Navigation

[0] Message Index

Go to full version