Thread: writing data(adding new records) to the input file

  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
    Last edited by ayesha; 11-22-2001 at 08:32 PM.

  2. #2
    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, 10:39 PM
  2. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03: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, 07:51 PM
  4. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM