Viewed   70 times

I'm trying to read a specific line from a text file using php. Here's the text file:

foo  
foo2

How would I get the content of the second line using php? This returns the first line:

<?php 
$myFile = "4-24-11.txt";
$fh = fopen($myFile, 'r');
$theData = fgets($fh);
fclose($fh);
echo $theData;
?>

..but I need the second.

Any help would be greatly appreciated

 Answers

1
$myFile = "4-24-11.txt";
$lines = file($myFile);//file in to an array
echo $lines[1]; //line 2

file — Reads entire file into an array

Sunday, November 20, 2022
4

Well, you could do:

$f = fopen($file, 'r');
$line = fgets($f);
fclose($f);

It's not one line, but if you made it one line you'd either be screwed for error checking, or be leaving resources open longer than you need them, so I'd say keep the multiple lines

Edit

If you ABSOLUTELY know the file exists, you can use a one-liner:

$line = fgets(fopen($file, 'r'));

The reason is that PHP implements RAII for resources.

That means that when the file handle goes out of scope (which happens immediately after the call to fgets in this case), it will be closed.

Thursday, October 27, 2022
1
$var = "Bill";
$input = "Kate
Johnny
Bill
Kermit";

$output = str_replace($var ."rn", "", $input ."rn");
Tuesday, December 20, 2022
 
3

If you have control over the cron or command, have you considered passing a command-line argument, and reading it with $_SERVER['argv'][0]?

* * * * *   /usr/bin/php /path/to/script --cron

In the script:

<?php
if(isset($_SERVER['argv'][0]) and $_SERVER['argv'][0] == '--cron')
   $I_AM_CRON = true;
else
   $I_AM_CRON = false;
Sunday, October 30, 2022
 
a-dubb
 
4

What's in countRowsOfDb()? If it opens the file and counts the lines in it (and I don't know what else it could do), then it's not going to find a lot in the final loop, since the creation of the ostream with the same name will have emptied the file.

More generally, this is a very inefficient way of doing things (and could easily fail if there were an error in the format of the file). The best way to handle this is to use an std::vector<Student>, with:

studentVector.push_back( Student( id, name, grade, points, type ) );

in the input loop. In all later loops, studentVector.size() gives the number of entries, or you can use the iterators.

Even better would be to use std::getline on the input, then initialize an std::istringstream to parse each line. This will catch input format errors much more reliably. Something like:

std::string line;
int lineNumber = 0;
while ( std::getline( inDb, line ) ) {
    ++ lineNumber;
    std::istringstream data( line );
    if ( data >> id >> name >> grade >> points >> type ) {
        studentVector.push_back( Student( id, name, grade, points, type ) );
    } else {
        std::cerr << "Format error in lne " << lineNumber << std::endl;
    }
}

Also, it is generally a better idea to write to separate file, then rename it after having verified that the write worked, i.e.:

std::ofstream outDb( "files/students.dat.new" );
//  Do output...
outDb.close();
if ( outDb ) {
    remove( "files/students.dat" );
    rename( "files/students.dat.new", "files/students.dat" );
} else {
    std::cerr << "Write error on output" << std::endl;
}

And of course, any write error should result in a return of EXIT_FAILURE from main. (This is one case where a global variable or a singleton is justified—keeping track of the return code.)

Sunday, November 13, 2022
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :