Thread overview | |||||
---|---|---|---|---|---|
|
January 13, 2007 Delete chars at the end of a text file | ||||
---|---|---|---|---|
| ||||
Hi, Now i'm trying to delete the last n characters at the end of a text file making the file shorter in size and pushing the EOF n places back, i try replacing the last char with "" and also std.string.whitespace but both doesn't do the job. Any ideas? Thanks in advance. |
January 13, 2007 Re: Delete chars at the end of a text file | ||||
---|---|---|---|---|
| ||||
Posted in reply to Heinz | Heinz wrote: > Hi, > > Any ideas? > > Thanks in advance. Hi Heinz! The following did work under linux, don't know if it'll work on windows: ----[code]----------------------- void trimFile(char[] fileName, int count) { // open file auto file = cast(char[])std.file.read(fileName); // cut of count characters file = file[0 .. $-count]; // write file std.file.write(fileName, file); } --------------------------------- you should place some exception-handling around each line, though. Nicolai |
January 13, 2007 Re: Delete chars at the end of a text file | ||||
---|---|---|---|---|
| ||||
Posted in reply to rochus | rochus wrote:
<snip>
> void
> trimFile(char[] fileName, int count) {
> // open file
> auto file = cast(char[])std.file.read(fileName);
> // cut of count characters
> file = file[0 .. $-count];
> // write file
> std.file.write(fileName, file);
> }
<snip>
Should work, as long as:
- the file isn't too big to fit in memory and to store in a char[]
- the file contains no multi-byte characters
You can also check out:
- chsize in the C RTL (defined in io.h)
- SetEndOfFile in the Windows API
Stewart.
|
Copyright © 1999-2021 by the D Language Foundation