GoTo specific line number in a file?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
rxsid
Forum Commoner
Posts: 82
Joined: Thu Aug 29, 2002 12:04 am

GoTo specific line number in a file?

Post by rxsid »

Hi all,

I've got a page where I'm displaying images from a directory. The images are labled 1.jpg through 524.jpg. This will never change (i.e. names or numbers of images in this directory).
What i've added is a file that contains 'subtitles' to each image. It's just a text file in this format:
1|some words for image 1
2|some words for image 2
...etc
This is the code im using to display the subtitle with the appropriate image:

Code: Select all

$subTitles "some/path/to/the/textFile";
$fo = fopen($subTitles, 'r');

if ($fo) {
	while (!feof($fo)) {
		$fContents = fgets ($fo,4096);
		list($fImgNum,$subtitle) = split ("\|",$fContents);
		if ($fImgNum == $imgNum) {
			echo $subtitle;
			break;
		} 
	}
	fclose ($fo);
} else {
    echo "Error opening the subtitles file";
}
this works fine for my purposes.

I'm wondering though, is there a way to fopen(), or file() then go directly to a specified line number instead of going through the file line by line until the current image/line number is reached? I realize that 524 lines is pretty tiny, but it just seems like there is a more 'direct' way to the line number needed?

Thanks.
User avatar
wtf
Forum Contributor
Posts: 331
Joined: Thu Nov 03, 2005 5:27 pm

Post by wtf »

"... file() returns the file in an array. Each element of the array corresponds to a line in the file, with the newline still attached."
according to this if you should be able to

Code: Select all

$fo = fopen($subTitles);
echo $fo[524 - 1];
Hope it helps
rxsid
Forum Commoner
Posts: 82
Joined: Thu Aug 29, 2002 12:04 am

Post by rxsid »

thanks for the reply wtf. that's the sort of hint/suggestion i was looking for.

i'm using:

Code: Select all

$fContents = file($subTitles);
list($fImgNum,$subtitle) = split ("\|",$fContents[$imgNum-1]);
echo $subtitle;
works great. thanks again.
printf
Forum Contributor
Posts: 173
Joined: Wed Jan 12, 2005 5:24 pm

Post by printf »

I think it would be better to set a fixed width for the file columns, then just seek to that line, it much better than using loops or loading an array that your not even going to really use!


A quick example...

Code: Select all

<?

// the line to get (image number)

$number = 7;

// the file to read

$file_name = './subjects.txt';

// fix length for any one subject line (not including (EOL))

$subject_len = 120;

// system line ending

$systems_eol = "\r\n";

// total line length

$len = ( $subject_len + strlen ( $systems_eol ) );

// open the file

$io = fopen ( $file_name, 'r' );

// go to the line that we want

fseek ( $io, ( $number == 1 ? 0 : ( $len * $number ) - $len ) );

// get the data we want

$subject = trim ( fgets ( $io, $len ) );

// close the file

fclose ( $io );

// show the result

echo $subject;

?>

here is a example file creation routine that you would use for the above code!

Code: Select all

<?

// the file to write to

$file_name = './subjects.txt';

// fix length for any one subject

$subject_len = 120;

// system line ending

$systems_eol = "\r\n";

// line counter example

$i = 0;

// open the file to write to

$io = fopen ( $file_name, 'w' );

while ( ++$i <= 10 )
{
	fputs ( $io, sprintf ( '%' . $subject_len . 's', 'This is subject line number ' . $i ) . $systems_eol );
}

fclose ( $io );

?>

printf
Post Reply