string concatenation with fgets

string concatenation with fgets

am 25.11.2009 02:30:53 von aurfalien

Hi all,

I'm trying to append some text to what I read from a file.

My code;

$file = fopen("foo.txt", "r");
while (!feof($file)) {
$line = fgets($file);
print $line."sometext";
}
fclose($file);

foo,txt;
a
b
c
d
e
f
g

And when I run the script, it looks like;
a
sometextb
sometextc
sometextd
....


Any ideas?

Thanks in advance,
- aurf

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: string concatenation with fgets

am 25.11.2009 02:52:45 von Ryan

Is this what you want

$file = fopen("test.txt", "r");
while (!feof($file)) {
$line = trim(fgets($file));
print $line."sometext\n";
}
fclose($file);

outputs
asometext
bsometext
csometext

Ref to http://us3.php.net/manual/en/function.fgets.php. "Reading ends
when /length/ - 1 bytes have been read, on a newline (which is included
in the return value), or on EOF (whichever comes first). If no length is
specified, it will keep reading from the stream until it reaches the end
of the line. "


aurfalien@gmail.com wrote:
> Hi all,
>
> I'm trying to append some text to what I read from a file.
>
> My code;
>
> $file = fopen("foo.txt", "r");
> while (!feof($file)) {
> $line = fgets($file);
> print $line."sometext";
> }
> fclose($file);
>
> foo,txt;
> a
> b
> c
> d
> e
> f
> g
>
> And when I run the script, it looks like;
> a
> sometextb
> sometextc
> sometextd
> ...
>
>
> Any ideas?
>
> Thanks in advance,
> - aurf
>


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: string concatenation with fgets

am 25.11.2009 02:55:35 von Nirmalya Lahiri

--- On Wed, 11/25/09, aurfalien@gmail.com wrote:=0A=
=0A> From: aurfalien@gmail.com =0A> Subject: [PHP] str=
ing concatenation with fgets=0A> To: php-general@lists.php.net=0A> Date: We=
dnesday, November 25, 2009, 7:00 AM=0A> Hi all,=0A> =0A> I'm trying to appe=
nd some text to what I read from a file.=0A> =0A> My code;=0A> =0A> $file =
=3D fopen("foo.txt", "r");=0A> while (!feof($file)) {=0A>   =C2=
=A0 $line =3D fgets($file);=0A>     print $line."sometext";=
=0A>     }=0A> fclose($file);=0A> =0A> foo,txt;=0A> a=0A> b=
=0A> c=0A> d=0A> e=0A> f=0A> g=0A> =0A> And when I run the script, it looks=
like;=0A> a=0A> sometextb=0A> sometextc=0A> sometextd=0A> ...=0A> =0A> =0A=
> Any ideas?=0A> =0ASo, what output you actually wants from your prog=
ram? Is it like this....=0Aasometextbsometextcsometext...... or, =
like this....=0Aasometext=0Absometext=0Acsometext =0A---=0A= E0¦¨=
ির্মালৠà¦¯ =
লাহিড়ী [Nirmalya Lahiri]=0A=
+৯১-৯৪৩৩=E0= A7§à§§=
৩৫৩৬ [+91-9433113536] =0A

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: string concatenation with fgets

am 25.11.2009 03:29:17 von aurfalien

On Nov 24, 2009, at 5:55 PM, Nirmalya Lahiri wrote:

> --- On Wed, 11/25/09, aurfalien@gmail.com wrote:
>
>> From: aurfalien@gmail.com
>> Subject: [PHP] string concatenation with fgets
>> To: php-general@lists.php.net
>> Date: Wednesday, November 25, 2009, 7:00 AM
>> Hi all,
>>
>> I'm trying to append some text to what I read from a file.
>>
>> My code;
>>
>> $file = fopen("foo.txt", "r");
>> while (!feof($file)) {
>> $line = fgets($file);
>> print $line."sometext";
>> }
>> fclose($file);
>>
>> foo,txt;
>> a
>> b
>> c
>> d
>> e
>> f
>> g
>>
>> And when I run the script, it looks like;
>> a
>> sometextb
>> sometextc
>> sometextd
>> ...
>>
>>
>> Any ideas?
>>
>
>
> So, what output you actually wants from your program?
>
> Is it like this....
> asometextbsometextcsometext......
>
> or, like this....
> asometext
> bsometext
> csometext
>


Hi,

Sorry, I was incomplete :)

I would like;

asometext
bsometext
csometext

Basically, I would like to add whatever text to the end of what I find
in the file.

So if the file contains

a
b
c

I would like;

asometext
bsometext
csometext...

- aurf


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: string concatenation with fgets

am 25.11.2009 03:45:01 von aurfalien

On Nov 24, 2009, at 5:52 PM, ryan wrote:

> Is this what you want
>
> $file = fopen("test.txt", "r");
> while (!feof($file)) {
> $line = trim(fgets($file));
> print $line."sometext\n";
> }
> fclose($file);
>
> outputs
> asometext
> bsometext
> csometext
>
> Ref to http://us3.php.net/manual/en/function.fgets.php. "Reading
> ends when /length/ - 1 bytes have been read, on a newline (which is
> included in the return value), or on EOF (whichever comes first). If
> no length is specified, it will keep reading from the stream until
> it reaches the end of the line. "
>
>
> aurfalien@gmail.com wrote:
>> Hi all,
>>
>> I'm trying to append some text to what I read from a file.
>>
>> My code;
>>
>> $file = fopen("foo.txt", "r");
>> while (!feof($file)) {
>> $line = fgets($file);
>> print $line."sometext";
>> }
>> fclose($file);
>>
>> foo,txt;
>> a
>> b
>> c
>> d
>> e
>> f
>> g
>>
>> And when I run the script, it looks like;
>> a
>> sometextb
>> sometextc
>> sometextd
>> ...
>>
>>
>> Any ideas?
>>
>> Thanks in advance,
>> - aurf
>>
>


OMG, very very cool, thanks Ryan.

- aurf


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: string concatenation with fgets

am 26.11.2009 21:57:52 von aurfalien

So here is my final test code, notice the check for ' ' in the if.

Since I'm on Linux, this has to do with whats between the last LF and
EOF which is nothing but this nothing will get printed out.

$file = fopen("somefile.txt", "r");
while (! feof($file))
{
$names = trim(fgets($file));
if ($names == '')
{
break;
}
print $names."sometext\n";
}
fclose($file);


- aurf

On Nov 24, 2009, at 5:52 PM, ryan wrote:

> Is this what you want
>
> $file = fopen("test.txt", "r");
> while (!feof($file)) {
> $line = trim(fgets($file));
> print $line."sometext\n";
> }
> fclose($file);
>
> outputs
> asometext
> bsometext
> csometext
>
> Ref to http://us3.php.net/manual/en/function.fgets.php. "Reading
> ends when /length/ - 1 bytes have been read, on a newline (which is
> included in the return value), or on EOF (whichever comes first). If
> no length is specified, it will keep reading from the stream until
> it reaches the end of the line. "
>
>
> aurfalien@gmail.com wrote:
>> Hi all,
>>
>> I'm trying to append some text to what I read from a file.
>>
>> My code;
>>
>> $file = fopen("foo.txt", "r");
>> while (!feof($file)) {
>> $line = fgets($file);
>> print $line."sometext";
>> }
>> fclose($file);
>>
>> foo,txt;
>> a
>> b
>> c
>> d
>> e
>> f
>> g
>>
>> And when I run the script, it looks like;
>> a
>> sometextb
>> sometextc
>> sometextd
>> ...
>>
>>
>> Any ideas?
>>
>> Thanks in advance,
>> - aurf
>>
>


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: string concatenation with fgets

am 30.11.2009 16:07:09 von Shawn McKenzie

aurfalien@gmail.com wrote:
> So here is my final test code, notice the check for ' ' in the if.
>
> Since I'm on Linux, this has to do with whats between the last LF and
> EOF which is nothing but this nothing will get printed out.
>
> $file = fopen("somefile.txt", "r");
> while (! feof($file))
> {
$tmp = trim(fgets($file));
if ($tmp != '')
> {
$names = $tmp;
> }
> print $names."sometext\n";
> }
> fclose($file);
>
>

--
Thanks!
-Shawn
http://www.spidean.com

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: string concatenation with fgets

am 30.11.2009 18:04:07 von aurfalien

Hi Shawn,

Your code looks cleaner then mine so i tried it and got the last entry
in the txt file printed twice.


On Nov 30, 2009, at 7:07 AM, Shawn McKenzie wrote:

> aurfalien@gmail.com wrote:
>> So here is my final test code, notice the check for ' ' in the if.
>>
>> Since I'm on Linux, this has to do with whats between the last LF and
>> EOF which is nothing but this nothing will get printed out.
>>
>> $file = fopen("somefile.txt", "r");
>> while (! feof($file))
>> {
> $tmp = trim(fgets($file));
> if ($tmp != '')
>> {
> $names = $tmp;
>> }
>> print $names."sometext\n";
>> }
>> fclose($file);
>>
>>
>
> --
> Thanks!
> -Shawn
> http://www.spidean.com


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: string concatenation with fgets

am 30.11.2009 18:24:50 von Ashley Sheridan

--=-l/HLsFA1nxGV+3Zd9d2M
Content-Type: text/plain
Content-Transfer-Encoding: 7bit

On Mon, 2009-11-30 at 09:04 -0800, aurfalien@gmail.com wrote:

> Hi Shawn,
>
> Your code looks cleaner then mine so i tried it and got the last entry
> in the txt file printed twice.
>
>
> On Nov 30, 2009, at 7:07 AM, Shawn McKenzie wrote:
>
> > aurfalien@gmail.com wrote:
> >> So here is my final test code, notice the check for ' ' in the if.
> >>
> >> Since I'm on Linux, this has to do with whats between the last LF and
> >> EOF which is nothing but this nothing will get printed out.
> >>
> >> $file = fopen("somefile.txt", "r");
> >> while (! feof($file))
> >> {
> > $tmp = trim(fgets($file));
> > if ($tmp != '')
> >> {
> > $names = $tmp;
> >> }
> >> print $names."sometext\n";
> >> }
> >> fclose($file);
> >>
> >>
> >
> > --
> > Thanks!
> > -Shawn
> > http://www.spidean.com
>
>


Remove the if statement and just print out $tmp. The while loop is going
over one extra time than you need, and on that final iteration, $tmp is
an empty string. The if statement only changes $name if $tmp is empty,
so it leaves it as it was, hence you getting the last line printed
twice. Printing out an empty string in this example won't do anything,
and the if statement is also pretty useless as it just copies the value
to another variable on a condition that will only result in the
side-effect you've noticed.

Thanks,
Ash
http://www.ashleysheridan.co.uk



--=-l/HLsFA1nxGV+3Zd9d2M--

Re: string concatenation with fgets

am 30.11.2009 18:40:01 von aurfalien

--Apple-Mail-5--24755883
Content-Type: text/plain;
charset=US-ASCII;
format=flowed;
delsp=yes
Content-Transfer-Encoding: 7bit

Hi Ash,

Actually I need the if because the code will print out an empty line
and add "sometext" to it.

So without the if check for an empty line, at the end of the loop I'll
get sometext. For example, if the file I am processing called
somename.txt has

a
b
c

in it. I'll have;

asometext
bsometext
csometext

but w/o the if check, I'll also have

sometext

as well.



On Nov 30, 2009, at 9:24 AM, Ashley Sheridan wrote:

> On Mon, 2009-11-30 at 09:04 -0800, aurfalien@gmail.com wrote:
>>
>> Hi Shawn,
>>
>> Your code looks cleaner then mine so i tried it and got the last
>> entry
>> in the txt file printed twice.
>>
>>
>> On Nov 30, 2009, at 7:07 AM, Shawn McKenzie wrote:
>>
>> > aurfalien@gmail.com wrote:
>> >> So here is my final test code, notice the check for ' ' in the if.
>> >>
>> >> Since I'm on Linux, this has to do with whats between the last
>> LF and
>> >> EOF which is nothing but this nothing will get printed out.
>> >>
>> >> $file = fopen("somefile.txt", "r");
>> >> while (! feof($file))
>> >> {
>> > $tmp = trim(fgets($file));
>> > if ($tmp != '')
>> >> {
>> > $names = $tmp;
>> >> }
>> >> print $names."sometext\n";
>> >> }
>> >> fclose($file);
>> >>
>> >>
>> >
>> > --
>> > Thanks!
>> > -Shawn
>> > http://www.spidean.com
>>
>>
>
> Remove the if statement and just print out $tmp. The while loop is
> going over one extra time than you need, and on that final
> iteration, $tmp is an empty string. The if statement only changes
> $name if $tmp is empty, so it leaves it as it was, hence you getting
> the last line printed twice. Printing out an empty string in this
> example won't do anything, and the if statement is also pretty
> useless as it just copies the value to another variable on a
> condition that will only result in the side-effect you've noticed.
>
> Thanks,
> Ash
> http://www.ashleysheridan.co.uk
>
>


--Apple-Mail-5--24755883--

Re: string concatenation with fgets

am 30.11.2009 18:42:42 von Ashley Sheridan

--=-TR6aosVbcBiXORFdnvnr
Content-Type: text/plain
Content-Transfer-Encoding: 7bit

On Mon, 2009-11-30 at 09:40 -0800, aurfalien@gmail.com wrote:

> Hi Ash,
>
> Actually I need the if because the code will print out an empty line
> and add "sometext" to it.
>
> So without the if check for an empty line, at the end of the loop I'll
> get sometext. For example, if the file I am processing called
> somename.txt has
>
> a
> b
> c
>
> in it. I'll have;
>
> asometext
> bsometext
> csometext
>
> but w/o the if check, I'll also have
>
> sometext
>
> as well.
>
>
>
> On Nov 30, 2009, at 9:24 AM, Ashley Sheridan wrote:
>
> > On Mon, 2009-11-30 at 09:04 -0800, aurfalien@gmail.com wrote:
> >>
> >> Hi Shawn,
> >>
> >> Your code looks cleaner then mine so i tried it and got the last
> >> entry
> >> in the txt file printed twice.
> >>
> >>
> >> On Nov 30, 2009, at 7:07 AM, Shawn McKenzie wrote:
> >>
> >> > aurfalien@gmail.com wrote:
> >> >> So here is my final test code, notice the check for ' ' in the if.
> >> >>
> >> >> Since I'm on Linux, this has to do with whats between the last
> >> LF and
> >> >> EOF which is nothing but this nothing will get printed out.
> >> >>
> >> >> $file = fopen("somefile.txt", "r");
> >> >> while (! feof($file))
> >> >> {
> >> > $tmp = trim(fgets($file));
> >> > if ($tmp != '')
> >> >> {
> >> > $names = $tmp;
> >> >> }
> >> >> print $names."sometext\n";
> >> >> }
> >> >> fclose($file);
> >> >>
> >> >>
> >> >
> >> > --
> >> > Thanks!
> >> > -Shawn
> >> > http://www.spidean.com
> >>
> >>
> >
> > Remove the if statement and just print out $tmp. The while loop is
> > going over one extra time than you need, and on that final
> > iteration, $tmp is an empty string. The if statement only changes
> > $name if $tmp is empty, so it leaves it as it was, hence you getting
> > the last line printed twice. Printing out an empty string in this
> > example won't do anything, and the if statement is also pretty
> > useless as it just copies the value to another variable on a
> > condition that will only result in the side-effect you've noticed.
> >
> > Thanks,
> > Ash
> > http://www.ashleysheridan.co.uk
> >
> >
>


Then put the print statement inside the if, not the assignation,
otherwise you will always get that last line!

Thanks,
Ash
http://www.ashleysheridan.co.uk



--=-TR6aosVbcBiXORFdnvnr--