Solved

PHP / FGETS problem

Posted on 2014-01-28
7
699 Views
Last Modified: 2014-01-28
I am currently using FGETS to read through a file, but I seem to have an odd problem - its missing lines? - it looks like for every 1 line it displays, it skips the next 2 lines

The input file is an excel CSV (basically plain text) - but i'm wondering if that could be my issue.

The CSV data has only one column, so has no commas and looks like this:

DataLine1 has 50ish chars
Dataline2 has 50ish chars
Dataline3 has 50ish chars
Dataline4 has 50ish chars
Dataline5 has 80ish chars
Dataline6 has 80ish chars
Dataline7 has 80ish chars
Dataline8 has 80ish chars


The code I am using to read it is as follows:

$file = fopen("files/tempfile.csv", "r") or exit("Unable to open file for reading!");
		 while(!feof($file))
		   {
			   			echo fgets($file) . "</br>";
}
fclose($file);

Open in new window


The output I get is like this:

DataLine1 has 50ish chars
Dataline4 has 50ish chars
Dataline7 has 80ish chars

Any ideas how I can get it to read the file properly, without missing most of the data?

Thanks
0
Comment
Question by:Bergstr
  • 3
  • 2
  • 2
7 Comments
 
LVL 10

Expert Comment

by:ienaxxx
ID: 39814713
Try fgets($file, 4096).
0
 

Author Comment

by:Bergstr
ID: 39814729
Nope, still have the same problem...
0
 
LVL 10

Assisted Solution

by:ienaxxx
ienaxxx earned 200 total points
ID: 39814734
OK then, try this:

ini_set('auto_detect_line_endings', true);
file = fopen("files/tempfile.csv", "r") or exit("Unable to open file for reading!");
		 while(!feof($file))
		   {
			   			echo fgets($file) . "</br>";
}
fclose($file);

Open in new window

0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 11

Expert Comment

by:Amar Bardoliwala
ID: 39814740
Hello Bergstr,

can you please give your original file that you are trying ?

it should help to resolve the problem.

Thank you.

Amar Bardoliwala
0
 

Author Comment

by:Bergstr
ID: 39814780
Think I found my problem - I didn't put the additional code before or after that section in my post, but it appears to be relevant (sorry!)

I'm trying to read from the old file, validate that data (and change it if needs be) then write the data to another file - and that seems to be my problem!


$file = fopen("files/tempfile.csv", "r") or exit("Unable to open file for reading");
$newfile = fopen("files/newfile.csv", 'w') or exit("Unable to open file for writing");
 //Output a line of the file until the end is reached
 while(!feof($file))
   {
   echo fgets($file). "<br>";
   fwrite($newfile, fgets($file));
   }
 fclose($file);
 
}

Open in new window


If I remove the fwrite line, it works fine - soon as I add the one line lines start disappearing!

I guess the new question is, how do I read one file while writing that data back to another, without losing data?

Sorry for being a pain, and thanks for the responses!
0
 
LVL 11

Accepted Solution

by:
Amar Bardoliwala earned 300 total points
ID: 39814804
Hello Bergstr,

Please store your current line in some variable first and use that like following

while(!feof($file))
   {
        $line =  fgets($file);
   
        echo $line. "<br />";

        fwrite($newfile, $line);
   }

Open in new window


currently it seems you are using fgets directly to print and it seems that it might echo first line and add second line in file..
so I can say that in while loop you are fetching 2 lines at a time.
because each time to write fgets it will fetch 1 line from file.

Hope this will help you.

Thank you.

Amar Bardoliwala
0
 

Author Comment

by:Bergstr
ID: 39814816
That seems to have worked, thanks for your help!

I'll accept the above as the solution but also share the points with ienaxxx s they did try and help when my original question didn't provide full details!

Thanks both!
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
PHP Installer 5 37
PHP Curl to output a url 7 46
PHP alternative to file_get_contents('php://input') 4 51
Ajax and PHP 9 29
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

790 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question