Creating a new file something going wrong

R

riderchap

I am opening a new file and writing something to that file and then
closing it. Now note down the size of file, let it be n bytes.
Then when I open the newly created file using vi make a change undo
that change and save and close it, now see the size of the file is n+1
bytes.

Also when I open the Perl program generated file in vi, vi will say
"Last line is not complete", why is that? After the
change-undo-save-close by vi, when I reopen the file in vi the vi is
not saying "Last line is not complete".

Here is the sample test script I used.

open (OUT_FILE, ">testfile");
print OUT_FILE "Hello World";
close (OUT_FILE);

After Perl program run the size of file is 11bytes. (Hello World is 11
characters). After opening in vi insert a charater then delete that
character then save and close. Now the file size is showing as 12
bytes.
What is vi writing there?
Or what character is my Perl program missing to write?
Why vi says "Last line is not complete", when opening the file created
by the Perl program?

Thanks!!!!!!!
Kannan

Note: The file I create through my Perl program has to be used by
another C program and that C program is not reading the last line of
the file correctly. Please help.
 
J

jue

open (OUT_FILE, ">testfile");
print OUT_FILE "Hello World";
close (OUT_FILE);

After Perl program run the size of file is 11bytes. (Hello World is 11
characters). After opening in vi insert a charater then delete that
character then save and close. Now the file size is showing as 12
bytes.
What is vi writing there?

vi writes a "\n" at the end of your "incomplete" line.

Thomas
 
B

Brian Wakem

I am opening a new file and writing something to that file and then
closing it. Now note down the size of file, let it be n bytes.
Then when I open the newly created file using vi make a change undo
that change and save and close it, now see the size of the file is n+1
bytes.

Also when I open the Perl program generated file in vi, vi will say
"Last line is not complete", why is that? After the
change-undo-save-close by vi, when I reopen the file in vi the vi is
not saying "Last line is not complete".

Here is the sample test script I used.

open (OUT_FILE, ">testfile");
print OUT_FILE "Hello World";
close (OUT_FILE);

After Perl program run the size of file is 11bytes. (Hello World is 11
characters). After opening in vi insert a charater then delete that
character then save and close. Now the file size is showing as 12
bytes.
What is vi writing there?
Or what character is my Perl program missing to write?
Why vi says "Last line is not complete", when opening the file created
by the Perl program?

Thanks!!!!!!!
Kannan

Note: The file I create through my Perl program has to be used by
another C program and that C program is not reading the last line of
the file correctly. Please help.


The character will almost certainly be a line feed (ASCII 10).

I don't see why the C program should have a problem with this unless it is
written in such a way that it needs each line to end with a line feed, in
which case make sure you write one at the end of each line in the file.

print OUT_FILE "Hello World\n";
 
T

Ted Zlatanov

I am opening a new file and writing something to that file and then
closing it. Now note down the size of file, let it be n bytes.
Then when I open the newly created file using vi make a change undo
that change and save and close it, now see the size of the file is n+1
bytes.

Also when I open the Perl program generated file in vi, vi will say
"Last line is not complete", why is that? After the
change-undo-save-close by vi, when I reopen the file in vi the vi is
not saying "Last line is not complete".

Here is the sample test script I used.

open (OUT_FILE, ">testfile");
print OUT_FILE "Hello World";
close (OUT_FILE);

After Perl program run the size of file is 11bytes. (Hello World is 11
characters). After opening in vi insert a charater then delete that
character then save and close. Now the file size is showing as 12
bytes.
What is vi writing there?
Or what character is my Perl program missing to write?
Why vi says "Last line is not complete", when opening the file created
by the Perl program?

The last line is not complete (according to vi) because it doesn't end
with a newline character. vi appended it.

Here's the easy way to see the difference:
perl -e'open (OUT_FILE, ">testfile"); print OUT_FILE "Hello World";'
cat testfile
Hello World> vi testfile
.... insert and then delete a character, then save ...
cat testfile Hello World

See how the file ends with a newline the second time?

You can print "Hello World\n" in line 2 of your code to achieve the
same result.

Ted
 
R

riderchap

Thanks folks!!!

I tried all crap ctl Z etc... don't know why didn't try a simple "\n".

Brian Wakem, that C program is written by somebody else and I have to
dig out that big C program to find out what he is doing there.

Thanks again guys.

-Kannan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top