Page 1 of 1

How To Read Mac Format Text Files

Posted: Tue Mar 29, 2005 3:14 pm
by ebs
The PureBasic file read command ReadString() supports Unix (0x0a <LF> end of line) and DOS (0x0d0x0a <CR><LF> end of line) file formats.

Mac-format text files use 0x0d <CR> as the line terminator. When I try to read a Mac file line-by-line in a loop, Eof() never becomes #True, so I can't tell when the end of file has been reached. Can anyone suggest a simple way to read Mac-format files? I guess I could read the entire file into memory and replace all the <CR> characters with <LF>, but I was hoping for something easier, if possible.

Thanks,
Eric

Posted: Tue Mar 29, 2005 7:37 pm
by griz
I don't have a Mac text file to test this with, but perhaps this works ?

Code: Select all

Procedure.s ReadMacString()
  MacString$ = ""
  linestop = #false
  Repeat
    f = ReadByte()
    If f=0
      linestop = #true
    EndIf
    If f = 13
      linestop = #true
    EndIf
    If linestop = #false
      MacString$+Chr(f)
    EndIf
  Until linestop = #true
  ProcedureReturn MacString$
EndProcedure

rfile=ReadFile(#pb_any,"mactext.txt")
If rfile
  Repeat
    Debug ReadMacString()
  Until Eof(Rfile)
  CloseFile(rfile)
EndIf

Posted: Tue Mar 29, 2005 9:31 pm
by ebs
griz,

Thanks for the suggestion, but I think that reading bytes will be a lot slower than reading each line. I think I remember some posts that showed how to read an entire file into memory. If I can find them, I should be able to read each line from memory. Can anyone point me in the right direction?

Thanks,
Eric

Posted: Tue Mar 29, 2005 9:38 pm
by HeX0R

Code: Select all

File$ = "yourfilehere.txt"
size.l = FileSize(File$)
*Buffer = AllocateMemory(size)
If ReadFile(0, File$)
  ReadData(*Buffer, size)
  CloseFile(0)
EndIf

Posted: Tue Mar 29, 2005 10:28 pm
by ebs
HeXOR,

Thanks for the help! I used your code and read each string from memory. Now it doesn't matter whether the file uses <CR>, <LF>, or <CR><LF> at the end of each line.

Regards,
Eric