Recent

Author Topic: Question about strings in files  (Read 3007 times)

Septe

  • Jr. Member
  • **
  • Posts: 68
Question about strings in files
« on: April 23, 2015, 04:38:01 pm »
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

  • Hero Member
  • *****
  • Posts: 8835
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Question about strings in files
« Reply #1 on: April 23, 2015, 05:13:23 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?
Nope, 768 / 3 = 256, the correct size of each short string without explicit length. Read the last paragraph in the docs.

rvk

  • Hero Member
  • *****
  • Posts: 6953
Re: Question about strings in files
« Reply #2 on: April 23, 2015, 05:51:31 pm »
... I take it that fpc pads the string (being 256 bytes in size) with something, correct?
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.

If I wanted to just print to the length of the strings, is there something that I can look at to research this?
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
« Last Edit: April 23, 2015, 05:53:13 pm by rvk »

Septe

  • Jr. Member
  • **
  • Posts: 68
Re: Question about strings in files
« Reply #3 on: April 24, 2015, 01:50:59 am »
In this situation, it was a file of StudentRecord of which contains String.  Leledumbo answered my question.  Thanks Leledumbo.

 

TinyPortal © 2005-2018