Home | Reviews | GUIpedia | Forum | Fun500
HorvatM | LF to CR or CRLFI'm having a very hard time trying to write code to change a file with LF-only (Unix-style) line endings to CR-only (Mac) so I can read them with Visual Basic and QB. My code goes like this:
Dim BinByte As Byte, BinByteBefore As Byte, BinWhole As String
Open InputFile For Binary As #1 For i = 1 To LOF(1) Get #1, Seek(1), BinByte If Seek(1) <> 1 Then Get #1, Seek(1) - 1, BinByteBefore If BinByte = 10 And (BinByteBefore <> 13) Then BinByte = 13 'change LF to CR only if it's not part of a CR&LF combo BinWhole = BinWhole & Chr(BinByte) 'append new byte to the whole string Next i Close #1 Open OutputFile For Output As #1 Print #1, BinWhole 'write the converted text Close #1 Any help would be appreciated. (Bonus points if you manage it to convert to CR-LF) | 2010-05-22 | 5:23 AM |
trolly | Re:LF to CR or CRLFTry this it read the input file, when it read a CR or LF character, it write the line string to the ouput file, when it read an other character, it append the character to the line string SUB Convert (infile as string, outfile as string) DIM taille AS INTEGER DIM car AS STRING * 1 DIM chaine AS STRING dim fic as integer dim fic2 as integer IF infile = "" THEN EXIT SUB fic = FREEFILE OPEN infile FOR BINARY AS fic fic2 = FREEFILE OPEN outfile FOR OUTPUT AS fic2 taille = LOF(fic) DO WHILE NOT EOF(fic) GET #fic, , car 'if it find a "\r" or "\n" chacater, it write the readed line to the output file, and flush the line string IF car = CHR$(10) OR car = CHR$(13) THEN PRINT #fic2, chaine chaine = "" ELSE 'if it find a diferent character , it append the character to the line string IF car <> CHR$(13) AND car <> CHR$(0) AND car <> "" AND car <> CHR$(10) THEN chaine = chaine + car END IF LOOP 'it write the last line to the ouput file chaine = chaine + CHR$(10) PRINT #fic2, chaine CLOSE fic2 CLOSE fic END SUB Note : CR alone is used in MacOS until OS9, OsX use the unix LF ("\n") chacater for new line since it's based on freebsd | 2010-05-22 | 6:04 AM |
HorvatM | Re:LF to CR or CRLFIt doesn't work. The output file is the same as the input file, only with a bunch of CRLFs at the end. | 2010-05-22 | 7:52 AM |
trolly | Re:LF to CR or CRLFthe goal to my code is to convert ln to crln if you want to convert ln to cr try, in the loop PRINT #fic2, chaine;chr$(13); it'll write the readed line with a cr character without a ln character what do you exacly want? convert from ln to cr, of from ln to crln. | 2010-05-22 | 11:21 AM |
HorvatM | Re:LF to CR or CRLFLF to CR+LF. LF to CR only will also do. | 2010-05-22 | 12:23 PM |
trolly | Re:LF to CR or CRLFnormaly, my code below convert lf to cr+lf, i use it in the qml browser to convert donwloaded text file, before to parse it. maybe are you not using windows or dos? | 2010-05-22 | 3:54 PM |
HorvatM | Re:LF to CR or CRLFProblem solved. Here's the new code in case anyone else needs it (you don't have to credit me if you use it). It could (and should) be optimized and cleaned up a lot. I'll leave that as an exercise.
Dim BinTemp As Byte, BinTempBefore As Byte, BinWhole As String Open WhatFileFor Binary As #1 For i = 1 To LOF(1) Get #1, Seek(1), BinTemp BinWhole = BinWhole & Chr(BinTemp) Next i Close #1 For i = 1 To Len(BinWhole) If i <> 1 Then If Asc(Mid(BinWhole, i, 1)) = 10 Then If Asc(Mid(BinWhole, i - 1, 1)) <> 13 Then Mid(BinWhole, i, 1) = Chr(13) End If End If End If Next i OpenWhatFile For Output As #1 Print #1, BinWhole Close #1 | 2010-05-22 | 5:34 PM |
trolly | Re:LF to CR or CRLFDim BinTemp As Byte, BinTempBefore As Byte, BinNew as byte
BinNew=13 Open WhatFileFor Binary As #1 while not eof(1) Get #1,, BinTemp if BinTemp=10 and binbefore <> 13 then SEEK #1,SEEK(1)-1 PUT #1,,BinNew end if BinBefore=BinTemp WEND CLOSE #1 it went out of my brain right now, I do not know if it works, but you can test it | 2010-05-23 | 6:29 AM |
Re:LF to CR or CRLFIf anyone wants to know, there are 1,000 converters, but If I were you I would use Notepad++ (google is your friend) It has syntax highlighting for every programming language known to man, and has an auto view for CR, LF, or CRLF. It also has a feature that allows you to convert the file between the types. ...And did I mention it's free? | 2010-05-31 | 1:19 PM |
trolly | Re:LF to CR or CRLFthe reason why we need a converting code, is not to read text files or something like. But it's to make the qml bowser, so, we need to convert the text format inside the code. Notepad++ or other such programs are useless in our case | 2010-06-01 | 5:44 PM |
2021 Brandon Cornell