file() will load the text file into an array.
$text = file("directory/filename.txt");
Keeping in mind that arrays start counting at 0, if you want line 40, you'd access it using $text['39']
If you want to add x to the end of the line:
$text['39'] = $text['39'] . "x";
Cgraz