write-line
write-line
I am using "write-line" to write to a file. The problem is that it always append "\r\n" carriage return and new line to each line. How can I not have that happen (or clean it up) on the last line?
Steven
Steven
You can use write-buffer. Then you can format the line however you like:
Code: Select all
(write-buffer file-handle (format "foo bar\n"))
Jeff, thanks for your help.
I am not sure how that will help.
I am processing a list and depending upon certain values writing items of that list to a file.
so for each item on the list that meets certain criteria, I am writing that item as a line in a file.
Therefore I always need a carriage return either at the end of the line or the begining of the line.
If it is at the end of the line then there will be an extra line at the end and if it is in the begining there will be an extra liner in the begininng.
I am probably missing something obvious.
I am not sure how that will help.
I am processing a list and depending upon certain values writing items of that list to a file.
so for each item on the list that meets certain criteria, I am writing that item as a line in a file.
Therefore I always need a carriage return either at the end of the line or the begining of the line.
If it is at the end of the line then there will be an extra line at the end and if it is in the begining there will be an extra liner in the begininng.
I am probably missing something obvious.
newdep thanks,
here is the general flow of the code
process a list of files that if they exist then write them out into a file.
So if there are only 2 items on the list that exist on the computer it the new file should have 2 lines each having the name of a file that exists like
c:\Program Files\Microsoft Office\Office10\EXCEL.EXE
c:\Program Files\Microsoft Office\Office10\winword.exe
without a blank line in front or back.
I am new at this so the answer might be obvious.
But what i am asking is, is there a straight forward way while writing to a file while processing a list to eliminate the extra line , or do you have to clean it up once the file writing is completed.
Sorry if it is confusing
here is the general flow of the code
Code: Select all
(dolist (x thelistbeingprocessed )
(if (file? x) (write-line x out-file) )
So if there are only 2 items on the list that exist on the computer it the new file should have 2 lines each having the name of a file that exists like
c:\Program Files\Microsoft Office\Office10\EXCEL.EXE
c:\Program Files\Microsoft Office\Office10\winword.exe
without a blank line in front or back.
I am new at this so the answer might be obvious.
But what i am asking is, is there a straight forward way while writing to a file while processing a list to eliminate the extra line , or do you have to clean it up once the file writing is completed.
Sorry if it is confusing
Code: Select all
(setq thelistbeingprocessed '("c:\Program Files\Microsoft Office\Office10\EXCEL.EXE" "c:\Program Files\Microsoft Office\Office10\winword.exe" c:\Program Files\Microsoft Office\Office10\xxxx.EXE)
(dolist (x thelistbeingprocessed )
(if (file? x) (write-line x out-file) )
So the "\r\n" is not currently in the list only getting there by writing it out.
I still have troubles understanding the issue.....sorry :-)
But if the list doesn't contain any \r\n and you are using 'write-line
then do only write if the 'x is not empty!
(dolist (x list)
(if (not (empty? x)) (write-line "mine" x)))
PS: you might use a 'trim to clean up white spaces befor the empty check..
But if the list doesn't contain any \r\n and you are using 'write-line
then do only write if the 'x is not empty!
(dolist (x list)
(if (not (empty? x)) (write-line "mine" x)))
PS: you might use a 'trim to clean up white spaces befor the empty check..
-- (define? (Cornflakes))
newdep, thanks again but I guess I am not to clear.
The solution that I found is
This will cut off the final "\r\n" and rewite the file.
The solution that I found is
Code: Select all
(write-file "myfile" (chop (read-file "myfile") 2) )
newdep was right. The $idx variable stores the current iterations. If you check that the current iteration is less than the length of the list, you effectively do not write the extra \r\n to the file.
An easier, more elegant solution would be:
An easier, more elegant solution would be:
Code: Select all
(write-buffer out-file
(join (filter file? thelistbeingprocessed) "\r\n"))
-
- Posts: 2038
- Joined: Tue Nov 29, 2005 8:28 pm
- Location: latiitude 50N longitude 3W
- Contact:
Lutz usually proposes the definitive solution. But I thought - when I first saw this thread - that you needed to apply a bit of lateral thinking to the algorithm - put the line breaks first. A bit like this:
Code: Select all
(print (first the-list))
(dolist (i (rest the-list))
(print "\r\n" i))
-
- Posts: 2038
- Joined: Tue Nov 29, 2005 8:28 pm
- Location: latiitude 50N longitude 3W
- Contact: