• Some users have recently had their accounts hijacked. It seems that the now defunct EVGA forums might have compromised your password there and seems many are using the same PW here. We would suggest you UPDATE YOUR PASSWORD and TURN ON 2FA for your account here to further secure it. None of the compromised accounts had 2FA turned on.
    Once you have enabled 2FA, your account will be updated soon to show a badge, letting other members know that you use 2FA to protect your account. This should be beneficial for everyone that uses FSFT.

php > writing array to a text file

scoob8000

2[H]4U
Joined
May 4, 2002
Messages
2,834
Okay, I'm trying to clean up some of my code. I had been storing values in their own individual text files, but that's too cluttered.

So I'm thinking an array is the way to go, I can easily use file(); to read the data in as an array, but my problem is writing it back to the file when I'm done manipulating it.

The best I could figure out is
Code:
$fp = fopen($datafile,"w");
foreach ($lines as $line_num => $line) {
$blah = $fwrite($fp,$line);
}
fclose($fp);

But the resulting "data file" seems to get mangled up because of no carriage returns. Am I approaching this right, or is there a better/easier way?
 
btw in your code above you have $fwrite(); instead of fwrite();


Also, "\n" is for new line, "\r" is for pushing it back to the start of the line (character return). So add "\r\n" in with your line..
Code:
$line_file = $line . "\r\n";
fwrite($fp, $line_file);

Thats just assuming the rest of the stuff is already working for you :)
 
btw in your code above you have $fwrite(); instead of fwrite();


Also, "\n" is for new line, "\r" is for pushing it back to the start of the line (character return). So add "\r\n" in with your line..
Code:
$line_file = $line . "\r\n";
fwrite($fp, $line_file);

Thats just assuming the rest of the stuff is already working for you :)

Yea, try tacking on \r\n

Code:
$fp = fopen($datafile,"w");
foreach ($lines as $line_num => $line) {
$line.="\r\n";
$blah = fwrite($fp,$line);
}
fclose($fp);
 
while jmackay puts it in a good way to remember it (which may translate more literally to an old fashioned typewriter), it's not necessarily accurate for a computer
http://us2.php.net/fopen said:
Note: Different operating system families have different line-ending conventions. When you write a text file and want to insert a line break, you need to use the correct line-ending character(s) for your operating system. Unix based systems use \n as the line ending character, Windows based systems use \r\n as the line ending characters and Macintosh based systems use \r as the line ending character.
If you use the wrong line ending characters when writing your files, you might find that other applications that open those files will "look funny".
Windows offers a text-mode translation flag ('t') which will transparently translate \n to \r\n when working with the file. In contrast, you can also use 'b' to force binary mode, which will not translate your data. To use these flags, specify either 'b' or 't' as the last character of the mode parameter.
The default translation mode depends on the SAPI and version of PHP that you are using, so you are encouraged to always specify the appropriate flag for portability reasons. You should use the 't' mode if you are working with plain-text files and you use \n to delimit your line endings in your script, but expect your files to be readable with applications such as notepad. You should use the 'b' in all other cases.
If you do not specify the 'b' flag when working with binary files, you may experience strange problems with your data, including broken image files and strange problems with \r\n characters.

a simpler way might be
Code:
file_put_contents($datafile, implode("\r\n", $lines));

//
//
//
// line to make the code block high enough
// because on my browser 1 line code blocks
// are hard to read....
this will only work on php 5+ because that's when the file_put_contents() function was added
 
I sort of figured I would just need to include a carriage return like that.
But at the same time I wasn't sure if the foreach was the proper way to do what I was trying to do.

Is there a better way so store small values then one per line? Comma seperated, etc?

All the values I'm storing are either voltages, temperatures, or dates. I know a mysql database would be ideal but it's not an option for me.
 
But at the same time I wasn't sure if the foreach was the proper way to do what I was trying to do.

Well, if using php5, definitely go tim_m's way, as it is the easiest. Otherwise, don't have the fwrite in your foreach loop as there is no reason to write each line separately.

Code:
$fp = fopen($datafile,"w");
$output = "";
foreach($lines AS $line) {
    $output .= $line."\r\n";
}
$blah = fwrite($fp, $output);
fclose($fp);

And an even easier way is to use implode() and leave out the foreach loop (as per tim_m's example).

Code:
$fp = fopen($datafile,"w");
$output = implode("\r\n", $lines);
$blah = fwrite($fp, $output);
fclose($fp);
 
it's probably worth noting that if your array is exceedingly large (i don't know what that really means, but let's say tens of thousands of items) it probably would be better to use for/foreach and fwrite each item individually. otherwise you would have the huge array itself in memory plus implode would create a gigantic string in memory so you're using 2x+ the memory you really need. just keep in mind for small arrays this is hardly worth considering.
 
Back
Top