digitalmars.D.learn - Delete chars at the end of a text file
- Heinz (7/7) Jan 12 2007 Hi,
- rochus (16/21) Jan 13 2007 Hi Heinz!
-
Stewart Gordon
(10/19)
Jan 13 2007
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.
Jan 12 2007
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
Jan 13 2007
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.
Jan 13 2007