Truncate a file: Difference between revisions

Added C# solution.
(ReSort alphabetically, {{header|Pascal}}: add example)
(Added C# solution.)
Line 97:
return dotruncate(fn, fp);
}</lang>
 
===POSIX===
<lang c>#include <unistd.h>
Line 109 ⟶ 110:
 
When specifying a new file size larger than current value, the file will be extended and padded with null bytes.
 
=={{header|C sharp}}==
 
<lang c sharp>using System;
using System.IO;
 
namespace TruncateFile
{
internal class Program
{
private static void Main(string[] args)
{
TruncateFile(args[0], long.Parse(args[1]));
}
 
private static void TruncateFile(string path, long length)
{
if (!File.Exists(path))
throw new ArgumentException("No file found at specified path.", "path");
 
using (var fileStream = new FileStream(path, FileMode.Open, FileAccess.Write))
{
if (fileStream.Length < length)
throw new ArgumentOutOfRangeException("length",
"The specified length is greater than that of the file.");
 
fileStream.SetLength(length);
}
}
}
}</lang>
 
=={{header|Haskell}}==