Creating a new file something going wrong

Discussion in 'Perl Misc' started by riderchap@gmail.com, Jul 18, 2006.

  1. Guest

    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.
     
    , Jul 18, 2006
    #1
    1. Advertising

  2. jue Guest

    schrieb:

    >
    > 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
     
    jue, Jul 18, 2006
    #2
    1. Advertising

  3. Brian Wakem Guest

    wrote:

    > 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";

    --
    Brian Wakem
    Email: http://homepage.ntlworld.com/b.wakem/myemail.png
     
    Brian Wakem, Jul 18, 2006
    #3
  4. Ted Zlatanov Guest

    On 18 Jul 2006, wrote:

    > 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
     
    Ted Zlatanov, Jul 18, 2006
    #4
  5. Guest

    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


    Ted Zlatanov wrote:
    > On 18 Jul 2006, wrote:
    >
    > > 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

    >
     
    , Jul 18, 2006
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    5
    Views:
    295
    wahaha
    Jan 10, 2007
  2. Replies:
    1
    Views:
    265
    Richard Tobin
    Jan 10, 2007
  3. barnetod
    Replies:
    2
    Views:
    248
    CBFalconer
    Jan 10, 2007
  4. Protoman
    Replies:
    0
    Views:
    309
    Protoman
    Dec 27, 2006
  5. Replies:
    9
    Views:
    181
Loading...

Share This Page