1. Problem when recording with append mode

For some reason that does not matter here, some programs write files without '\n' in your last line. Thus, the file looks like this:

Line 1\n 
Line 2\n 
Line 3 

When I open the file with the statement: open ("filename", "a") and record more lines, the result is:

Line 1\n 
Line 2\n 
Line 3Line 4\n 
Line 5\n 

Simply use a '\n' before starting the recordings is not solving also, because there are files that comes with '\n' at the end, and in this case the result would be this:

Line 1\n 
Line 2\n 
Lane 3\n 
\n 
Line 4\n 
Line 5\n 

How do I resolve this issue?

Thanks in advance

new topic     » topic index » view message » categorize

2. Re: Problem when recording with append mode

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.

new topic     » goto parent     » topic index » view message » categorize

3. Re: Problem when recording with append mode

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 message » categorize

4. Re: Problem when recording with append mode

I will test your suggestion.

thank you

new topic     » goto parent     » topic index » view message » categorize

5. Re: Problem when recording with append mode

sergelli said...

I did it before making the recording.

open ("fileName", "r") 
LIN1 = gets (fn) 
if not find (10 LIN1) then 

But, it seems, that gets (fn) puts that character '\n' in all lines. Even if the original did not. So, I have no way of knowing if this character existed in the file.

You'll need to use getc(), instead of gets(), to do this check.

new topic     » goto parent     » topic index » view message » categorize

6. Re: Problem when recording with append mode

jimcbrown said...

You'll need to use getc(), instead of gets(), to do this check.

I used the getc() function and your code inside my program and now everything is working perfectly.

Many thanks to jimcbrown

Sergio

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu