Results 1 to 19 of 19

Thread: C++ File appending

  1. #1

    Thread Starter
    Member Cahlel's Avatar
    Join Date
    Aug 2000
    Location
    Earth (I think)
    Posts
    63

    Question

    Hey, how do you use a piece of code like this to APPEND a file...:?

    ofstream out_stream;
    out_stream.open("Mail.dat");


    How do you open it for appending instead of wiping it clean each time.
    Wisdom is supreme, therefore get wisdom,
    though it costs all you have, get understanding.
    Proverbs 4:7

  2. #2
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    This should work:
    Code:
    out_stream.open("Mail.dat", ios::append);
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  3. #3

    Thread Starter
    Member Cahlel's Avatar
    Join Date
    Aug 2000
    Location
    Earth (I think)
    Posts
    63

    Unhappy Ha, no working

    It doesn't work....! is there a different incllude file I need to use? says 'append' is undefined....help....?
    Wisdom is supreme, therefore get wisdom,
    though it costs all you have, get understanding.
    Proverbs 4:7

  4. #4
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Try including iomanip.h and ios.h
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  5. #5

    Thread Starter
    Member Cahlel's Avatar
    Join Date
    Aug 2000
    Location
    Earth (I think)
    Posts
    63

    Include

    I tried including the files, iomanip.h and io.h NOT ioS.h (because ioS.h doesn't exist) and it still can't find it....
    Wisdom is supreme, therefore get wisdom,
    though it costs all you have, get understanding.
    Proverbs 4:7

  6. #6
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Sorry about that...the code I had in front of me was for a different set of headers :angry:

    Try ios_base::app
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  7. #7

    Thread Starter
    Member Cahlel's Avatar
    Join Date
    Aug 2000
    Location
    Earth (I think)
    Posts
    63

    Still no dice...

    Ok, here's the errors::

    Error: ai.cpp(62,52):Qualifier 'ios_base' is not a class or namespace name
    Error: ai.cpp(62,54):Function call missing )


    and here's my code::

    #include <iostream.h>
    #include <fstream.h>
    #include <stdlib.h>
    #include <conio.h>
    #include <ctype.h>
    #include <iomanip.h>
    #include <io.h>

    blah blah blah

    out_stream.open("mail.dat", ios_base::app);
    if (out_stream.fail())
    {textcolor(RED);
    cprintf("Error opening Mail output file, exiting program...");
    exit(1);


    can't figure out what's happening
    }
    Wisdom is supreme, therefore get wisdom,
    though it costs all you have, get understanding.
    Proverbs 4:7

  8. #8
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    GGgggnnnckkk. I know what it is I was referring to the official headers...
    Code:
    #include <iostream> 
    #include <fstream> 
    #include <cstdlib> 
    #include <conio.h> 
    #include <ctype.h> 
    #include <iomanip> 
    
    using namespace std;
    
    out_stream.open("mail.dat", ios_base::app); 
    
    if(out_stream.fail()) {
        textcolor(RED); 
        cprintf("Error opening Mail output file, exiting program..."); 
        exit(1); 
    }
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  9. #9

    Thread Starter
    Member Cahlel's Avatar
    Join Date
    Aug 2000
    Location
    Earth (I think)
    Posts
    63

    One more time...........

    Ok, one more time, this is all of my code, still won't work:


    /////////////////////////////////////////////////////
    // AI Program By Matthew Hinman Period 2 1-02-01 //
    // ---~~:Bertha:~~--- //
    /////////////////////////////////////////////////////
    #include <iostream.h>
    #include <fstream.h>
    #include <stdlib.h>
    #include <conio.h>
    #include <ctype.h>
    #include <iomanip.h>
    #include <io.h>

    using namespace std;


    void description();

    int main()
    {
    char command;
    char outfile[21];
    char infile[21];
    char mailchoice;
    description();
    textcolor(BLUE);
    cprintf(" Welcome to 'Bertha', an AI program\r\n");
    cprintf(" Just poke around, do what you like.\r\n");
    cprintf(" Commands are limited to 50 characters.\r\n");
    cprintf(" ----~~~::TURN CAPS-LOCK ON::~~~----\r\n");
    cout << "$ ";
    cin >> command;
    if (command=='H')
    {
    cprintf("Help Options\r\n");
    }
    if (command=='O')
    {
    cprintf("Change output file\r\n");
    cout << "New output file name? ";
    cin >> outfile;
    cout << "Outfile changed\n";
    }
    if (command=='I')
    {
    cprintf("Change input file\r\n");
    cout << "New input file name? ";
    cin >> infile;
    cout << "Infile changed\n";
    }
    if (command=='M')
    {
    ofstream out_stream;
    mail:
    cprintf("Mail Options: \r\n");
    cout << "S: send mail\n";
    cout << " R: Read mail\n";
    cout << " X: Exit\n";
    cin >> mailchoice;
    switch (mailchoice)
    {
    case 'S':
    char user[11];
    char mestext;
    char subject[31];
    out_stream.open("mail.dat", ios_base::app);
    if (out_stream.fail())
    {
    textcolor(RED);
    cprintf("Error opening Mail output file, exiting program...");
    exit(1);
    }
    cprintf("Send Mail\r\n");
    cout << "Who do you want to send mail to? ";
    cin >> user;
    out_stream << "NAME: " << user << endl;
    cout << "Subject of the message? (0-30 characters long ONE WORD) ";
    cin >> subject;
    out_stream << "SUBJECT: " << subject << endl;
    cout << "Message text: (~ to finish)\n";
    out_stream << "MESSAGE: ";
    do
    {
    mestext=getch();
    cout << mestext;
    out_stream << int(mestext);
    } while (mestext!='~');
    out_stream << endl << endl;

    cout << "Message saved, exiting to menu\n";
    goto mail;
    case 'R':
    cout << "Mail reading feature not yet supported";
    goto mail;
    case 'X':
    cprintf("Exiting mail program...\r\n");
    break;
    default:
    textcolor(RED);
    cprintf("Illegal Command, re-enter\r\n");
    textcolor(BLUE);
    goto mail;
    }
    }
    else cprintf("Command non-existent or not installed\r\n");
    }

    void description()
    {
    textcolor(RED);
    cprintf("This Program was written by Matthew Hinman and is\r\ncopyright with no unathorized reproductions\r\n");
    }



    If this doesn't work, i'm going to shoot someone...
    Wisdom is supreme, therefore get wisdom,
    though it costs all you have, get understanding.
    Proverbs 4:7

  10. #10
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Code:
    #include <iostream> 
    #include <fstream> 
    #include <cstdlib> 
    #include <conio.h> 
    #include <ctype.h> 
    #include <iomanip> 
    #include <io.h>
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  11. #11

    Thread Starter
    Member Cahlel's Avatar
    Join Date
    Aug 2000
    Location
    Earth (I think)
    Posts
    63

    Errrr

    Error: ai.cpp(7,2):Unable to open include file 'CSTDLIB.h'

    Do i need to download it?
    Wisdom is supreme, therefore get wisdom,
    though it costs all you have, get understanding.
    Proverbs 4:7

  12. #12
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    It doesn't have a .h extension!!!
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  13. #13

    Thread Starter
    Member Cahlel's Avatar
    Join Date
    Aug 2000
    Location
    Earth (I think)
    Posts
    63

    Errrr

    It automatically added one, I'm using borland? I dunno why...
    Wisdom is supreme, therefore get wisdom,
    though it costs all you have, get understanding.
    Proverbs 4:7

  14. #14
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Oh...I don't know then - I don't have borland. I'm sure it supports the standard c++ library (where the headers with no extension are from)
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  15. #15

    Thread Starter
    Member Cahlel's Avatar
    Join Date
    Aug 2000
    Location
    Earth (I think)
    Posts
    63

    Dang

    Dang, that sucks, owell, thanks for your help and I guess I'll try to find the problem myself
    Wisdom is supreme, therefore get wisdom,
    though it costs all you have, get understanding.
    Proverbs 4:7

  16. #16
    Guest
    turbo C++ does not support the standard C++ library, but BCC 5.5 does...

    and parksie, I thought you did this:

    Code:
    //.......
    out_file.open("filename.txt", ios::app);
    that works for me wheater using iostream or iostream.h

  17. #17
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    I was reading the wrong source code - that had ios::append, rather than ios_base::app.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  18. #18
    Guest
    I meant:

    I thought you were supposed to do this:

    <code was here>


  19. #19
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    That's what I interpreted from your reply .

    Compiling a quick test program resulted in my second answer being the right one - as in the one you gave

    I was reading the wrong bit of source code *cries*
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width