Re: Problem when recording with append mode

new topic     » goto parent     » topic index » view thread      » older message » newer message
jimcbrown said...
sergelli said...

For some reason that does not matter here, some programs write files without '\n' in your last line.

...

Simply use a '\n' before starting the recordings is not solving also, because there are files that comes with '\n' at the end...

How do I resolve this issue?

Thanks in advance

You'll need to open the file and (if it exists) read the last character to check if it is a newline or not.

Some example code on how to do this:

EDIT: update after testing

include std/io.e --4.0 and up 
--include file.e --3.11 or earlier 
 
-- returns 1 if a newline must be added, 0 otherwise 
function check_newline(sequence file) 
	integer h, c 
	atom p 
	h = open(file, "r") 
	if h = -1 then 
		return 0 
	end if 
	if not seek(h, -1) then 
		p = where(h) 
		if not seek(h, p - 1) then 
			c = getc(h) 
			if c != '\n' and c != -1 then 
				close(h) 
				return 1 
			end if 
		end if 
	end if 
	close(h) 
	return 0 
end function 
procedure append_newline(sequence file) 
	integer h 
	if check_newline(file) then 
		h = open(file, "a") 
		if h != -1 then 
			puts(h, "\n") 
			close(h) 
		end if 
	end if 
end procedure 
new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu