writing data(adding new records) to the input file

This is a discussion on writing data(adding new records) to the input file within the C++ Programming forums, part of the General Programming Boards category; can someone PLEASE KINDLY help me on how to add new records to input fie( in this case its "datafile.dat"). ...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    11

    adding new records to the input file

    can someone PLEASE KINDLY help me on how to add new records to input fie( in this case its "datafile.dat"). i want to add new employee records to my "datafile.dat"
    Also, How can i delete an employee record from the datafile? i understand that it needs a matching name for delete purpose, but HOW do i insert that match funtion into my program?


    PLEASE CHECK IF I HAVE ANY OTHER MISTAKES IN MY CODE. I WUD B XTREMELY GRATEFUL
    THANX A LOT!!!!!!!!!


    #include <stdio.h>
    #include < iostream.h>
    #include < iomanip.h>
    #include < fstream.h>
    #include < string.h>
    #include "employee.h"



    void main()
    {

    EmployeeList *List = new EmployeeList;
    char NewName[50];
    char NewDepartment[50];
    int NewSalary;
    ifstream In;
    fstream datafile;
    Employee result;
    char get;

    // Open the data file for reading
    In.open("datafile.dat");

    //Get the first line to check if it is 'done'
    In >> NewName;

    // Add employees to the list until they enter 'done'
    while(strcmp(NewName, "done") != 0)
    {
    cout <<"Please choose one of the following menu selections:\n";
    cout <<"A. Add a record \n";
    cout <<"B. Delete a record \n";
    cout <<"C. Display all records \n";
    cout <<"D. Exit \n";
    cin>>get;
    switch(get)
    {
    case 'A' : result.AddEmployee(); break;
    case 'B' : result.DeleteEmployee(); break;
    case 'C' :result.DisplayEmployee;
    break;
    case 'D' : NewName == "done" ; break;
    break;
    }
    // Get the remaining data for the employee
    In >> NewDepartment >> NewSalary;

    // Create the employee and assign the values
    Employee *NewEmployee = new Employee(NewName, NewDepartment, NewSalary);
    List->Add(NewEmployee);

    // Get the next name or 'done'
    In >> NewName;
    }

    In.close();



    }




    "datafile.dat"

    Raj Computer 90000
    Ayesha Accounting 95000
    Jr.Raj Operational 140000
    Ali Business 50000
    Zainab Accountin 45000
    done x 0
    Attached Files Attached Files
    Last edited by ayesha; 11-22-2001 at 08:32 PM.

  2. #2
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    If you want to add a record to the end the you can open the file for appending (ios::app). To delete a record, you're going to have to read the whole file into a data structure, and then write back all but the record you need to delete. To 'match' character arrays you can use strcmp().
    zen

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 11:39 PM
  2. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 04:25 PM
  3. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 08:51 PM
  4. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 05:54 PM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM

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