Line endings using fprintf
joe61vette
Hello Everyone:
First, I am using IGOR Pro 8.04 on an Intel Mac running 11.5.2.
In a procedure, I read and modify a text file and then write it line by line to a new file using:
do
FReadLine tmpFile, line
...
fprintf inFile, "%s", line
while (1)
FReadLine tmpFile, line
...
fprintf inFile, "%s", line
while (1)
When a unix program reads this text file, it complains there are Windows(DOS) line endings in it.
So, I have tried things like:
last = strlen(line)
line[last] = "\n"
line[last] = "\n"
but with no success.
This is probably very elementary, but I haven't been able to figure it out.
Thanks for the help,
Joe
Most likely the <CR><LF> windows-style line endings were read from the file and are present in your string variable "line".
You might be able to use
line += "\n"
Note that I have not tested this!
Or perhaps
line = ReplaceString("\r\n", line, "\n")
August 17, 2021 at 04:17 pm - Permalink
Thanks John:
I've tried both your suggestions but with no success. First, I checked the original text file and it has the proper unix style endings and can be used as an input file to the program I am trying to run.
When I used ReplaceString, it apparently did nothing. If I just use that, it writes all the lines as one very long line when I open it with the Atom editor.
If I use line += "\n", the lines display properly but it still shows that the line endings are CRLF.
I was unable to find RemoveLineEnding. Is it in one of the procedure folders?
To make this easier, I've attached both my procedure file and the input file that gets processed. The command I use for testing is:
RunTests("PH", "TAMU", 0.3)
Note that I had to change the extension of the input file from"tmp" to "txt" in order to upload it here.
Thanks again for the help,
Joe Kelly
August 18, 2021 at 06:05 am - Permalink
The Details section for FReadLine says that, if you omit /T, it returns CR as terminator. So you need to do this:
line = ReplaceString("\r", line, "\n")
August 18, 2021 at 07:16 am - Permalink
Hi Joe,
First, John probably meant
line += "\n"
but I am not sure this will help you. Looking at your test file I see that all lines are terminated with just a linefeed ("\n"). But somehow your code seems to sneak in a carriage return ("\r") in somewhere. Looking at your script I don't see where this happens. To make sure you do not accidentally write a CR, you could do this:
line = ReplaceString("\r", line, "")
This will just delete all carriage returns before writing the line.
EDIT: OK, Howard got the right answer here!
August 18, 2021 at 07:22 am - Permalink
In reply to The Details section for… by hrodstein
Thanks Howard! The problem was not setting /T=(num2char(10)). Using that worked great.
Ironically, the
line = ReplaceString("\r", line, "\n")
was one of the first things I tried, and for some reason it didn't work.
Thanks again for the great support,
Joe
August 18, 2021 at 07:42 am - Permalink