append file?

This is a discussion on append file? within the C++ Programming forums, part of the General Programming Boards category; I am very new to programming and would like to append information to a file (not write over existing text) ...

  1. #1
    Unregistered
    Guest

    Question append file?

    I am very new to programming and would like to append information to a file (not write over existing text)

    I am using Microsoft visual c++ and here is my write to file code
    (writes all the information over existing text)


    #include<iostream.h>
    #include<fstream.h>

    void main()
    {

    //declare variables

    char firstname[10];//allow upto 10 characters long ?
    char surname[10];
    char age[10];
    char gender[10];

    //get input from user

    cout<<"enter your christian name : ";
    cin>>firstname;//set firstname to name entered by user

    cout<<"enter your surname : ";
    cin>>surname;//set surname to name entered by user

    cout<<"enter your age : ";
    cin>>age;//set age to input entered from user

    cout<<"male or femele ? ";
    cin>>gender;//set gender to inputfrom user

    //setup new output stream (give it a name)

    ofstream outfile;

    //target it to a text file
    //then open it ready for data transfer

    outfile.open("C:\\outtest.txt");

    //send data

    outfile<<surname<<endl;
    outfile<<firstname<<endl;
    outfile<<age<<endl;
    outfile<<gender<<endl;
    //this just appended the text one after the other
    //so how do I append all the text to the end of a file?

    cout<<endl<<"file written\n";


    }

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    2
    When you create the file open it to append

    ofstream file(file_name,ios::app);
    file<<surname<<endl;
    .;
    .;
    This will automactically append anything you output to the end of the file you have opened

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-22-2008, 12:19 AM
  2. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 11:12 PM
  3. Hmm....help me take a look at this: File Encryptor
    By heljy in forum C Programming
    Replies: 3
    Last Post: 03-23-2002, 10:57 AM
  4. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21