View Subcategory

Write a string to a file

ruby
File.new("a_file", "w") << "some text"
csharp
System.IO.File.WriteAllText("filename.txt", "Some text to write to the file");
clojure
(with-out-writer "output.txt" (println "Hello file!"))
fsharp
let stream = new StreamWriter("test.txt", false)
stream.WriteLine("This line overwrites file contents!")
cpp
IO::StreamWriter^ stream;

try
{
stream = gcnew IO::StreamWriter("test.txt", false);
stream->WriteLine("This line overwites file contents!");
}

Append to a file

ruby
file = File.new('/tmp/test.txt', 'a+') ; file.puts 'This line appended to file!!' ; file.close()
csharp
System.IO.File.AppendAllText("filename.txt", "Some text to append to the file");
clojure
(with-out-append-writer "output.txt" (println "This is appended to the file"))
fsharp
let stream = new StreamWriter("test.txt", true)
stream.WriteLine("This line appended to file!")
cpp
IO::StreamWriter^ stream;

try
{
stream = gcnew IO::StreamWriter("test.txt", true);
stream->WriteLine("This line appended to file!");
}