PDA

Click to See Complete Forum and Search --> : C++ File appending


Cahlel
Jan 3rd, 2001, 11:27 AM
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.

parksie
Jan 3rd, 2001, 12:11 PM
This should work:

out_stream.open("Mail.dat", ios::append);

Cahlel
Jan 3rd, 2001, 03:34 PM
It doesn't work....! is there a different incllude file I need to use? says 'append' is undefined....help....?

parksie
Jan 3rd, 2001, 03:38 PM
Try including iomanip.h and ios.h

Cahlel
Jan 3rd, 2001, 03:46 PM
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....

parksie
Jan 3rd, 2001, 03:49 PM
Sorry about that...the code I had in front of me was for a different set of headers :angry:

Try ios_base::app

Cahlel
Jan 3rd, 2001, 03:53 PM
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
}

parksie
Jan 3rd, 2001, 03:59 PM
GGgggnnnckkk. I know what it is :) I was referring to the official headers... ;)

#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);
}

Cahlel
Jan 3rd, 2001, 04:03 PM
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...

parksie
Jan 3rd, 2001, 04:05 PM
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <conio.h>
#include <ctype.h>
#include <iomanip>
#include <io.h>

Cahlel
Jan 3rd, 2001, 04:07 PM
Error: ai.cpp(7,2):Unable to open include file 'CSTDLIB.h'

Do i need to download it?

parksie
Jan 3rd, 2001, 04:07 PM
It doesn't have a .h extension!!!

Cahlel
Jan 3rd, 2001, 04:08 PM
It automatically added one, I'm using borland? I dunno why...

parksie
Jan 3rd, 2001, 04:11 PM
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)

Cahlel
Jan 3rd, 2001, 04:13 PM
Dang, that sucks, owell, thanks for your help and I guess I'll try to find the problem myself

Jan 3rd, 2001, 04:32 PM
turbo C++ does not support the standard C++ library, but BCC 5.5 does...

and parksie, I thought you did this:


//.......
out_file.open("filename.txt", ios::app);


that works for me wheater using iostream or iostream.h

parksie
Jan 4th, 2001, 12:02 PM
I was reading the wrong source code - that had ios::append, rather than ios_base::app.

Jan 4th, 2001, 02:26 PM
I meant:

I thought you were supposed to do this:

<code was here>

parksie
Jan 4th, 2001, 03:27 PM
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*