Truncate a file: Difference between revisions

Content added Content deleted
m (→‎{{header|Perl 6}}: alternate method)
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 89: Line 89:
f.close()
f.close()
}</lang>
}</lang>

=={{header|AWK}}==
=={{header|AWK}}==
<lang AWK>
<lang AWK>
Line 314: Line 315:


When specifying a new file size larger than current value, the file will be extended and padded with null bytes.
When specifying a new file size larger than current value, the file will be extended and padded with null bytes.

=={{header|C++}}==
<lang C++>#include <string>
#include <fstream>

using namespace std;

void truncateFile(string filename, int max_size) {
std::ifstream input( filename, std::ios::binary );
char buffer;
string outfile = filename + ".trunc";
ofstream appendFile(outfile, ios_base::out);
for(int i=0; i<max_size; i++) {
input.read( &buffer, sizeof(buffer) );
appendFile.write(&buffer,1);
}
appendFile.close(); }

int main () {
truncateFile("test.txt", 5);
return 0;
}
</lang>


=={{header|C sharp}}==
=={{header|C sharp}}==
Line 368: Line 346:
}
}
}</lang>
}</lang>

=={{header|C++}}==
<lang C++>#include <string>
#include <fstream>

using namespace std;

void truncateFile(string filename, int max_size) {
std::ifstream input( filename, std::ios::binary );
char buffer;
string outfile = filename + ".trunc";
ofstream appendFile(outfile, ios_base::out);
for(int i=0; i<max_size; i++) {
input.read( &buffer, sizeof(buffer) );
appendFile.write(&buffer,1);
}
appendFile.close(); }

int main () {
truncateFile("test.txt", 5);
return 0;
}
</lang>


=={{header|Clojure}}==
=={{header|Clojure}}==
Line 441: Line 442:
If you try to truncate a non-existent file using a stream you will get an EFOpenError exception with the message:
If you try to truncate a non-existent file using a stream you will get an EFOpenError exception with the message:
Cannot open file "<File name with full path>. The system cannot find the file specified.
Cannot open file "<File name with full path>. The system cannot find the file specified.

=={{header|Elena}}==
=={{header|Elena}}==
ELENA 4.x:
ELENA 4.x:
Line 502: Line 504:
truncateFile args.[0] (Int64.Parse(args.[1]))
truncateFile args.[0] (Int64.Parse(args.[1]))
0</lang>
0</lang>

=={{header|Go}}==
Go has the required function in the standard library. The implementation calls operating system specific functions and returns whatever errors the operating system reports.
<lang go>import (
"fmt"
"os"
)

if err := os.Truncate("filename", newSize); err != nil {
fmt.Println(err)
}</lang>
Package os also has a separate function that operates on an open file.


=={{header|Fortran}}==
=={{header|Fortran}}==
Line 575: Line 565:
CALL FILEHACK("foobar.txt",12)
CALL FILEHACK("foobar.txt",12)
END</lang>
END</lang>

=={{header|Go}}==
Go has the required function in the standard library. The implementation calls operating system specific functions and returns whatever errors the operating system reports.
<lang go>import (
"fmt"
"os"
)

if err := os.Truncate("filename", newSize); err != nil {
fmt.Println(err)
}</lang>
Package os also has a separate function that operates on an open file.


=={{header|Haskell}}==
=={{header|Haskell}}==
Line 880: Line 882:
# Truncate a file to 567 bytes.
# Truncate a file to 567 bytes.
truncate("file", 567);</lang>
truncate("file", 567);</lang>
=={{header|Perl 6}}==
{{Works with|rakudo|2016.07}}
<lang perl6>use NativeCall;

sub truncate(Str, int32 --> int32) is native {*}

sub MAIN (Str $file, Int $to) {
given $file.IO {
.e or die "$file doesn't exist";
.w or die "$file isn't writable";
.s >= $to or die "$file is not big enough to truncate";
}
truncate($file, $to) == 0 or die "Truncation was unsuccessful";
}</lang>

The external <code>truncate</code> routine could be replaced with the following line (in which case no need for <code>NativeCall</code>):
<lang perl6>spurt $file, slurp($file).substr($to);</lang>


=={{header|Phix}}==
=={{header|Phix}}==
Line 977: Line 962:
END IF
END IF
END SUB</lang>
END SUB</lang>

=={{header|Powershell}}==
<lang powershell>Function Truncate-File(fname) {
$null | Set-Content -Path "$fname"
}
</lang>


=={{header|PureBasic}}==
=={{header|PureBasic}}==
Line 993: Line 984:
ProcedureReturn #True
ProcedureReturn #True
EndProcedure</lang>
EndProcedure</lang>

=={{header|Powershell}}==
<lang powershell>Function Truncate-File(fname) {
$null | Set-Content -Path "$fname"
}
</lang>


=={{header|Python}}==
=={{header|Python}}==
Line 1,025: Line 1,010:
(λ(o) (file-truncate o size))))
(λ(o) (file-truncate o size))))
</lang>
</lang>

=={{header|Raku}}==
(formerly Perl 6)
{{Works with|rakudo|2016.07}}
<lang perl6>use NativeCall;

sub truncate(Str, int32 --> int32) is native {*}

sub MAIN (Str $file, Int $to) {
given $file.IO {
.e or die "$file doesn't exist";
.w or die "$file isn't writable";
.s >= $to or die "$file is not big enough to truncate";
}
truncate($file, $to) == 0 or die "Truncation was unsuccessful";
}</lang>

The external <code>truncate</code> routine could be replaced with the following line (in which case no need for <code>NativeCall</code>):
<lang perl6>spurt $file, slurp($file).substr($to);</lang>


=={{header|REXX}}==
=={{header|REXX}}==