www.digitalmars.com         C & C++   DMDScript  

D - append line with stream on file

reply Manfred Hansen <manfred toppoint.de> writes:
Hello,

is it possible to append a line on a file with std.streams ?

import std.stream;
                                                                                
int main() {
        File file = new File;
        file.create("testfile.txt");
                                                                                
        file.writeLine("Line 1");
                                                                                
        file.close();
                                                                                
        return 0;
}


If i run the programm twice, i have only one line in my file.

In C looks like:

#include <stdio.h>
                                                                                
int main(void) {
                                                                                
        FILE* fp;
        fp= fopen("testfile.txt","a+");
        fputs("Line 1",fp);
        fclose(fp);
                                                                                
        return 0;
}


I missed an append function in stream's .

Manfred
Apr 20 2004
next sibling parent "Matthew" <matthew.hat stlsoft.dot.org> writes:
Shouldn't you use an open() method? (I don't know; just guessing)

"Manfred Hansen" <manfred toppoint.de> wrote in message
news:c63r3p$btu$1 digitaldaemon.com...
 Hello,

 is it possible to append a line on a file with std.streams ?

 import std.stream;

 int main() {
         File file = new File;
         file.create("testfile.txt");

         file.writeLine("Line 1");

         file.close();

         return 0;
 }


 If i run the programm twice, i have only one line in my file.

 In C looks like:

 #include <stdio.h>

 int main(void) {

         FILE* fp;
         fp= fopen("testfile.txt","a+");
         fputs("Line 1",fp);
         fclose(fp);

         return 0;
 }


 I missed an append function in stream's .

 Manfred

Apr 20 2004
prev sibling parent reply Andrew <Andrew_member pathlink.com> writes:
In article <c63r3p$btu$1 digitaldaemon.com>, Manfred Hansen says...
Hello,

is it possible to append a line on a file with std.streams ?

import std.stream;
                                                                                
int main() {
        File file = new File;
        file.create("testfile.txt");
                                                                               

Replaceing your first two lines with: File file = new File("testfile.txt",FileMode.Out); file.seekEnd(0); Should do the trick!
        file.writeLine("Line 1");
                                                                                
        file.close();
                                                                                
        return 0;
}


If i run the programm twice, i have only one line in my file.

In C looks like:

#include <stdio.h>
                                                                                
int main(void) {
                                                                                
        FILE* fp;
        fp= fopen("testfile.txt","a+");
        fputs("Line 1",fp);
        fclose(fp);
                                                                                
        return 0;
}


I missed an append function in stream's .

Manfred

Apr 20 2004
parent reply Andrew <Andrew_member pathlink.com> writes:
In article <c64039$kmq$1 digitaldaemon.com>, Andrew says...
In article <c63r3p$btu$1 digitaldaemon.com>, Manfred Hansen says...
Hello,

is it possible to append a line on a file with std.streams ?

import std.stream;
                                                                                
int main() {
        File file = new File;
        file.create("testfile.txt");
                                                                               

Replaceing your first two lines with: File file = new File("testfile.txt",FileMode.Out); file.seekEnd(0); Should do the trick!

Even Better: File file = new File; try { file.open("testfile.txt",FileMode.Out); } catch (OpenError e) { file.create("testfile.txt",FileMode.Out); } file.seekEnd(0);
Apr 20 2004
parent reply "Ben Hinkle" <bhinkle4 juno.com> writes:
 Even Better:

 File file = new File;
 try {
 file.open("testfile.txt",FileMode.Out);
 } catch (OpenError e) {
 file.create("testfile.txt",FileMode.Out);
 }
 file.seekEnd(0);

My std.stream "update" fixes that bug. File open will create if it isn't there. -Ben
Apr 20 2004
parent reply "Andrew Edwards" <edwardsac spamfreeusa.com> writes:
Must be in 0.82, I'm still stuck on 0.81 and tried it after I made my first
post. Needless to say: it didn't work. I'll download 0.82. Thanks...

"Ben Hinkle" <bhinkle4 juno.com> wrote in message
news:c643e8$q1c$1 digitaldaemon.com...
 Even Better:

 File file = new File;
 try {
 file.open("testfile.txt",FileMode.Out);
 } catch (OpenError e) {
 file.create("testfile.txt",FileMode.Out);
 }
 file.seekEnd(0);

My std.stream "update" fixes that bug. File open will create if it isn't there. -Ben

Apr 20 2004
parent "Kris" <someidiot earthlink.dot.dot.dot.net> writes:
Andrew,

Ben was talking about his *updated* version: it's not in the Phobos library
(yet). Ben provided a download link for it a week or so ago, and I think
this is probably the place: http://home.comcast.net/~benhinkle/stream.d

- Kris

"Andrew Edwards" <edwardsac spamfreeusa.com> wrote in message
news:c645op$u1q$1 digitaldaemon.com...
 Must be in 0.82, I'm still stuck on 0.81 and tried it after I made my

 post. Needless to say: it didn't work. I'll download 0.82. Thanks...

 "Ben Hinkle" <bhinkle4 juno.com> wrote in message
 news:c643e8$q1c$1 digitaldaemon.com...
 Even Better:

 File file = new File;
 try {
 file.open("testfile.txt",FileMode.Out);
 } catch (OpenError e) {
 file.create("testfile.txt",FileMode.Out);
 }
 file.seekEnd(0);

My std.stream "update" fixes that bug. File open will create if it isn't there. -Ben


Apr 20 2004