How To Read Mac Format Text Files

Everything else that doesn't fall into one of the other PB categories.
ebs
Enthusiast
Enthusiast
Posts: 561
Joined: Fri Apr 25, 2003 11:08 pm

How To Read Mac Format Text Files

Post 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
User avatar
griz
Enthusiast
Enthusiast
Posts: 167
Joined: Sun Jun 29, 2003 7:32 pm
Location: Canada

Post 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
ebs
Enthusiast
Enthusiast
Posts: 561
Joined: Fri Apr 25, 2003 11:08 pm

Post 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
User avatar
HeX0R
Addict
Addict
Posts: 1219
Joined: Mon Sep 20, 2004 7:12 am
Location: Hell

Post 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
ebs
Enthusiast
Enthusiast
Posts: 561
Joined: Fri Apr 25, 2003 11:08 pm

Post 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
Post Reply