I was wondering if anyone knows how to print out the contents of an array into a text file(i.e myfile.txt)

What i have is an array full of strings and my goal is to print each of them onto a seperate line in a text file.

I kno how to write single lines in a text file but not contents of an array.

Any help is greatly appreciated!!!

Dani AI

Generated

Brief answer for PHP (one string per line), plus some practical tips that complement the C# examples posted by and the discussion about verbatim strings raised by .

A simple, safe overwrite (recommended for small-to-medium arrays):

$lines = array_map('strval', $array);               // ensure values are strings
file_put_contents($filename, implode(PHP_EOL, $lines) . PHP_EOL, LOCK_EX);

This joins entries with the OS newline (PHP_EOL), adds a final line break, and uses LOCK_EX to reduce race conditions. To append instead of overwrite use FILE_APPEND | LOCK_EX.

If the array can be very large, stream to disk to avoid high memory use:

$fp = fopen($filename, 'w');   // or 'a' to append
if ($fp === false) { /* handle error */ }
foreach ($array as $line) {
    fwrite($fp, rtrim((string)$line, "\r\n") . PHP_EOL);
}
fclose($fp);

Practical notes and troubleshooting

  • Check that PHP can write to the target folder (is_writable(dirname($filename))) and prefer fixing ownership/permissions over using 0777.
  • Sanitize any user-supplied filenames (use a whitelist, basename, or store in a fixed directory) to avoid path traversal.
  • If the file must never be partially written, write to a temp file (e.g. tempnam()), then rename() it into place for atomicity.
  • For cross-platform newline needs, explicitly use "\r\n" for Windows consumers; otherwise PHP_EOL is usually fine.
  • Watch encoding: convert to UTF-8 when required (mb_convert_encoding) and be aware of BOM if editors mis-handle files.
  • Verify success: check file_put_contents return value (bytes written) or use fwrite return values.

These PHP patterns cover the common cases; adapt them for CSV (use fputcsv) or binary output as needed.

Recommended Answers

All 3 Replies

// given array and filename
string[] array = { "apple", "banana", "cherry" };
string filename = @"C:\Temp\arrayfile.txt";

// short version
using (StreamWriter writer = new StreamWriter(filename))
{
    foreach (string item in array)
        writer.WriteLine(item);
}

// short, short version
File.WriteAllLines(filename, array);

It's a bit off-topic but could you please explain what the "@" is for in:
string filename = @"C:\Temp\arrayfile.txt";

I believe I've done similar things without the @ and it works as it should but I've also seen many people using it pretty often, what exactly does it do?
Thanks.

It's a bit off-topic but could you please explain what the "@" is for in:
string filename = @"C:\Temp\arrayfile.txt";

I believe I've done similar things without the @ and it works as it should but I've also seen many people using it pretty often, what exactly does it do?
Thanks.

If i am right im pretty sure it means
"take anything after this and between the quotes as a literal string"
meaning:

anything between the quotes will be taking and used AS IS

@"C:\documents\test.txt"-----means the computer will store it as (C:\documents\test.txt)

If you use this you dont have to use the \\ anymore

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.