write-line

Machine-specific discussion
Unix, Linux, OS X, OS/2, Windows, ..?
Locked
SHX
Posts: 37
Joined: Fri Feb 16, 2007 11:06 pm

write-line

Post by SHX »

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

Jeff
Posts: 604
Joined: Sat Apr 07, 2007 2:23 pm
Location: Ohio
Contact:

Post by Jeff »

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
=====
Old programmers don't die. They just parse on...

Artful code

SHX
Posts: 37
Joined: Fri Feb 16, 2007 11:06 pm

Post by SHX »

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.

newdep
Posts: 2038
Joined: Mon Feb 23, 2004 7:40 pm
Location: Netherlands

Post by newdep »

I dont realy get it yet ;-) but ill try..

If you are appening the "\r\n" then use write-file/write-buffer instead of write-line

if you data already has '\r\n" and want to remove it use replace, chop or regex
or filter..etc..
-- (define? (Cornflakes))

SHX
Posts: 37
Joined: Fri Feb 16, 2007 11:06 pm

Post by SHX »

newdep thanks,

here is the general flow of the code

Code: Select all

    (dolist (x thelistbeingprocessed ) 	
   		   (if (file? x) (write-line x out-file) )
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

newdep
Posts: 2038
Joined: Mon Feb 23, 2004 7:40 pm
Location: Netherlands

Post by newdep »

like this?


(setq lines '( "this is line one\r\n" "\r\nthis is another line"))
(dolist (x lines)
(replace "\r\n" x "")
(append-file "mine" x) ;; or (write-line "mine" x)
)
-- (define? (Cornflakes))

SHX
Posts: 37
Joined: Fri Feb 16, 2007 11:06 pm

Post by SHX »

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 if an item on the list exists write out the file name with each file name being a new line.

So the "\r\n" is not currently in the list only getting there by writing it out.

newdep
Posts: 2038
Joined: Mon Feb 23, 2004 7:40 pm
Location: Netherlands

Post by newdep »

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..
-- (define? (Cornflakes))

SHX
Posts: 37
Joined: Fri Feb 16, 2007 11:06 pm

Post by SHX »

That is what i am doing.

And the write-line puts "\r\n" at the end of each line which is good until it writes the last line which should not have the "\r\n"

Thanks again for your help.

newdep
Posts: 2038
Joined: Mon Feb 23, 2004 7:40 pm
Location: Netherlands

Post by newdep »

Aaaaaaaaaaaaa now i get it ;-)

oke here is the fix...


(if (>= (+ 1 $idx) (length list)) (write-file or append-file...;; not write-line!
-- (define? (Cornflakes))

SHX
Posts: 37
Joined: Fri Feb 16, 2007 11:06 pm

Post by SHX »

newdep, thanks again but I guess I am not to clear.

The solution that I found is

Code: Select all

(write-file "myfile" (chop (read-file "myfile") 2) )
This will cut off the final "\r\n" and rewite the file.

newdep
Posts: 2038
Joined: Mon Feb 23, 2004 7:40 pm
Location: Netherlands

Post by newdep »

That is indeed someting different ..Enjoy ;-)
-- (define? (Cornflakes))

Jeff
Posts: 604
Joined: Sat Apr 07, 2007 2:23 pm
Location: Ohio
Contact:

Post by Jeff »

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:

Code: Select all

(write-buffer out-file
  (join (filter file? thelistbeingprocessed) "\r\n"))
Jeff
=====
Old programmers don't die. They just parse on...

Artful code

cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Post by cormullion »

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))

Jeff
Posts: 604
Joined: Sat Apr 07, 2007 2:23 pm
Location: Ohio
Contact:

Post by Jeff »

join will put a carriage return only between items in the list.
Jeff
=====
Old programmers don't die. They just parse on...

Artful code

cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Post by cormullion »

oops - confused your code with Lutz's ... :-) Sorry jeff! I was really replying to the 8:21 pm post, but I got sidetracked...

SHX
Posts: 37
Joined: Fri Feb 16, 2007 11:06 pm

Post by SHX »

Jeff,

Thanks, exactly what I was looking for.

Steven

Locked