www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Delete chars at the end of a text file

reply Heinz <billgates microsoft.com> writes:
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
parent reply rochus <rochus rochus.net> writes:
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
parent Stewart Gordon <smjg_1998 yahoo.com> writes:
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