473,406 Members | 2,345 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

trouble reading from file

hi,

I'm on winXPpro / apache 1.2 / php 4.4.x

I'm experimenting with writing and reading from textfiles via php. I can
create a file with fopen, write to it, but I'm having trouble reading
from the file.

here's a snippet:

if (!$keywfile = file("keyw.txt") || !$keywfile = file("desc.txt"))
{
$error = "couldn't open files<br>";

} else {

foreach($keywfile as $line)
{
$keywords .= $line;
}
foreach($keywfile as $line)
{
$desc .= $line;
}

}

I get the following error: Warning: "Invalid argument supplied for
foreach() in d:\filesystem\tmp5o3n6eulkr.php on line 25"

I checked the file handles with print_r and they both output "1". isn't
the file() function supposed to return an array?I have verified that the
files are not empty.
..soma
Jul 17 '05 #1
4 1817
One quick glance of an experienced eye allowed to understand the blurred
and almost unreadable somaboy mx's handwriting:

[snip]
I get the following error: Warning: "Invalid argument supplied for
foreach() in d:\filesystem\tmp5o3n6eulkr.php on line 25"

I checked the file handles with print_r and they both output "1". isn't
the file() function supposed to return an array?I have verified that
the files are not empty.


Nope, read the manual at www.php.net
You have to use fgets() to get lines from file.

Cheers
Mike
Jul 17 '05 #2
Michał Woźniak wrote:
One quick glance of an experienced eye allowed to understand the blurred
and almost unreadable somaboy mx's handwriting:

[snip]
I get the following error: Warning: "Invalid argument supplied for
foreach() in d:\filesystem\tmp5o3n6eulkr.php on line 25"

I checked the file handles with print_r and they both output "1". isn't
the file() function supposed to return an array?I have verified that
the files are not empty.

Nope, read the manual at www.php.net
You have to use fgets() to get lines from file.

Cheers
Mike


Thanks Mike, fgets() does the job...

..soma
Jul 17 '05 #3
???
Yes, file() will read the entire file into an array
there are a lot more options for reading a file than just fopen and
fgets()

a problem I see is that you're using the same variable ($keywfile) for
both:
if (!$keywfile = file("keyw.txt") || !$keywfile = file("desc.txt"))

and try this format
if ( false == $keywfile = file('keyw.txt') )
{
// couldn't open
}
or

if ( $keywfile = file('keyw.txt') )
{
$keywords = '';
foreach($keywfile as $line)
{
$keywords .= $line;
}
// or instead of the foreach, just use
// $keywords = implode('',$keywfile);
}

Jul 17 '05 #4
BKDotCom wrote:
???
Yes, file() will read the entire file into an array
there are a lot more options for reading a file than just fopen and
fgets()

a problem I see is that you're using the same variable ($keywfile) for
both:
if (!$keywfile = file("keyw.txt") || !$keywfile = file("desc.txt"))

and try this format
if ( false == $keywfile = file('keyw.txt') )
{
// couldn't open
}
or

if ( $keywfile = file('keyw.txt') )
{
$keywords = '';
foreach($keywfile as $line)
{
$keywords .= $line;
}
// or instead of the foreach, just use
// $keywords = implode('',$keywfile);
}

yes, that is some awkward code :( , I noticed and fixed it right after I
posted. But using fgets in combination with feof() fixed the problem of
not being able to iterate over the file contents.

thx,

..soma
Jul 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Robert | last post by:
I am trying to read from a input data file and I am having trouble and ending up with another read at the eof that is garbage. I know that I shouldn't use while(!eof) but I am pre-loading my input...
7
by: laclac01 | last post by:
So I am converting some matlab code to C++. I am stuck at one part of the code. The matlab code uses fread() to read in to a vector a file. It's a binary file. The vector is made up of floats,...
1
by: bas | last post by:
hey, I am having a lot of trouble trying to get this program to work. It is supposed to read integers from a file and place them into the categories for inventory. The problem seems to be that...
1
by: Westbrook, Christopher L (WESTBCL04) | last post by:
I am having some trouble, and I can't figure out why. I'm trying to use the simple keylogger project from source forge, and then create a web interface to open the resulting files, so people from...
1
by: khaleel.alyasini | last post by:
anyone could point me out where did i do wrong? it seems that i can't get back the original Lena image after the IDCT(inverse discrete cosine transform) process. the output raw image is nothing...
2
by: Peter | last post by:
Hello, I am having alot of trouble reading in a date values from a text file which is exported from another application. My program is pretty basic, I read in values from a plain text file...
31
by: tophandasa | last post by:
Hi all, I'm having a trouble reading a binary file as float values.I have to read the data in binary mode, then read every four bytes into a float variable. I have done my search, but i found out...
0
by: Anish G | last post by:
Hi, I have an issue with reading CSV files. I am to reading CSV file and putting it in a Datatable in C#. I am using a regular expression to read the values. Below is the code. Now, it reads...
1
by: sachinkale123 | last post by:
Hi, I am reading excel file and reading values from that I am using provider As : "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = " + Filename + ";Extended Properties=\"Excel 8.0;Hdr=No;IMEX=1\"";...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.