trailing blank line in a text file

Olga olga at urbantimes.net
Wed Jul 21 03:11:42 UTC 2004


> On Tue, 2004-07-20 at 19:30, Olga wrote:
>> > On Tue, 2004-07-20 at 21:39, Olga wrote:
>> >> No in vi and nano I do not see anything. But in gedit I see the
>> >> following
>> >> (for example)
>> >>
>> >> 1 Mike
>> >> 2 John
>> >> 3 Adam
>> >> 4
>> >>
>> >> The above is an example of what a file looks like with lines
>> numbered.
>> >> (numbers are not actually part of the file). I want the file to be
>> only
>> >> 3
>> >> lines long (as an example); however, what I get is 4 lines where line
>> 4
>> >> is
>> >> an empty line. I can eliminate only using mc (F4). In nano, vim, vi
>> it
>> >> is
>> >> invisible, but in gedit (gui editor) it shows what I displayed above.
>> I
>> >> can backspace after 4, it bring me to Adam, but when I save the file
>> and
>> >> reopen it, the output is exactly the same as I am showing now (empty
>> >> line
>> >> 4 stays).
>> >>
>> >
>> > Let's try a different approach....
>> >
>> > After you save the file in whichever editor, can we confirm that it
>> > actually has the "four lines" or really just has "three lines"?
>> >
>> > How about running "wc -l filename" and see how many lines it
>> reports...
>> > This will help to confirm for you whether the file truly has the blank
>> > line, or is just a visual construct of the editor.
>> >
>> > --Rob
>> >
>>
>> wc -l returns 3 lines. But when I process the file in php (save each
>> line
>> in to an array and then produce a count), it reports 4 lines as does
>> gedit.
> ----
> Actually, php doesn't do that, the code in your php program does that
> and thus, you can adjust the code to do what you want.
>
> Craig
>
>

You are right, it's the php code. But I don't see anything wrong with the
following:

$x=0;
$ar=array();
$f = fopen ("../employee.txt", "r");
while (!feof ($f)) {
    $ar[$x] = fgets($f, 4096);
    $x++;
}

The count of $ar returns 4 even though there are actually 3 records. So I
am not sure if you would call it an expected behaviour.





More information about the fedora-list mailing list