I doing assignment about file processing.
It quite hard.
1.Sometimes can open the file sometimes cannot
2.How to overwrite y content in the file?
3. How to add Data into the file?
Brief: You know I create the file then when I close the program. After that open again. It overwrite the begin file even though I need that. Actually I want add more data into it!!
4. How to access the file?

Overall is how the program run as? How it search data from the file created?

So......................................URGENT
This week have to hand in the assignment
Help me :eek:

//CourseData.h

#ifndef COURSEDATA_H
#define COURSEDATA_H
using namespace std;

#include <string>

class CourseData
{
private:
  int courseNumber;
  char courseTitle[20];
  int courseyear;
  char courseLeader[20];
  char internalTelCour[20];
  int roomNumber;
  char departmentName[20];
public:
  //default CourseData constructor
  CourseData(int =0, string ="",int =0,string ="",string ="",int =0,string ="");

  //accessor functions for courseNumber
  void setCourseNumber( int );
  int getCourseNumber() const;

  //accessor functions for courseTitle
  void setCourseTitle (string);
  string getCourseTitle() const;

  //accessor functions for courseyear
  void setCourseYear( int );
  int getCourseYear () const;

  //accessor functions for courseLeader
  void setCourseLeader(string);
  string getCourseLeader() const;

  //accessor functions for internalTelCour
  void setInternalTelCour (string);
  string getInternalTelCour() const;

  //accessor functions for roomNumber
  void setRoomNumber (int);
  int getRoomNumber () const;

  //accessor functions for departmentName
  void setDepartmentName (string);
  string getDepartmentName () const;
};

#endif
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
//CourseData.cpp

#include <string>

#include "CourseData.h"

//default CourseData constructor
CourseData::CourseData(int courseNumberValue, string courseTitleValue
                       ,int couseyearValue, string courseLeaderValue, string internalTelCourValue
                       ,int roomNumberValue, string departmentNameValue)
{
  setCourseNumber( courseNumberValue );
  setCourseTitle (courseTitleValue);
  setCourseYear( couseyearValue );
  setCourseLeader(courseLeaderValue);
  setInternalTelCour (internalTelCourValue);
  setRoomNumber (roomNumberValue);
  setDepartmentName (departmentNameValue);
}//end CourseData constructor

//get courseNumber value
int CourseData::getCourseNumber() const
{
  return courseNumber;
}//end function getCourseNumber

//set courseNumber value
void CourseData::setCourseNumber( int courseNumberValue)
{
  courseNumber=courseNumberValue;
}//end function setCourseNumber

//get courseTitle value
string CourseData::getCourseTitle() const
{
  return courseTitle;
}//end function getCourseTitle

//set CourseTitle value
void CourseData::setCourseTitle (string courseTitleString )
{
  //copy at most 20 characters from string to courseTitle
  const char *courseTitleValue=courseTitleString.data();
  int length=courseTitleString.size();
  length=( length < 20 ? length:19);
  strncpy(courseTitle, courseTitleValue, length);
  courseTitle[length]='\0';//append null character to courseTitle
}//end function setCourseTitle

//get courseyear
int CourseData::getCourseYear () const
{
  return courseyear;
}//end function getCourseYear

//set couseyear value
void CourseData::setCourseYear( int couseyearValue)
{
  courseyear=couseyearValue;
}//end function setCourseYear

//get CourseLeader value
string CourseData::getCourseLeader() const
{
  return courseLeader;
}//end function getCourseLeader

//set CourseLeader value
void CourseData::setCourseLeader (string courseLeaderString )
{
  //copy at most 20 characters from string to courseLeader
  const char *courseLeaderValue=courseLeaderString.data();
  int length=courseLeaderString.size();
  length=( length < 20 ? length:19);
  strncpy(courseLeader, courseLeaderValue, length);
  courseLeader[length]='\0';//append null character to courseLeader
}//end function setCourseLeader

//get InternalTelCour value
string CourseData::getInternalTelCour() const
{
  return internalTelCour;
}//end function getInternalTelCour

//set InternalTelCour value
void CourseData::setInternalTelCour (string internalTelCourString )
{
  //copy at most 12 characters from string to internalTelCour
  const char *internalTelCourValue=internalTelCourString.data();
  int length=internalTelCourString.size();
  length=( length < 12 ? length:11);
  strncpy(internalTelCour, internalTelCourValue, length);
  internalTelCour[length]='\0';//append null character to internalTelCour
}//end function setInternalTelCour 

//get roomNumber
int CourseData::getRoomNumber () const
{
  return roomNumber;
}//end function getRoomNumber 

//set RoomNumber value
void CourseData::setRoomNumber ( int roomNumberValue)
{
  roomNumber=roomNumberValue;
}//end function setRoomNumber 

//get departmentName value
string CourseData::getDepartmentName () const
{
  return departmentName;
}//end function getDepartmentName 

//set DepartmentName value
void CourseData::setDepartmentName (string departmentNameString )
{
  //copy at most 20 characters from string to departmentName
  const char *departmentNameValue=departmentNameString.data();
  int length=departmentNameString.size();
  length=( length < 20 ? length:19);
  strncpy(departmentName, departmentNameValue, length);
  departmentName[length]='\0';//append null character to departmentName
}//end function setDepartmentName 

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
//main

#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstdlib>
#include "CourseData.h"

int enterChoice();
void createTextFile( fstream&);
void updateRecord( fstream&);
void newRecord( fstream&);
void deleteRecord( fstream&);
void outputLine( ostream&, const CourseData &);
int getCourse( const char * const);

enum Choice { PRINT=1, UPDATE, NEW, DELETE, END};

int main()
{
  //open file for reading and writing
  fstream inOutCourse("course.txt",ios::in|ios::out| ios::binary);

  //exit program if fstream cannot open file
  if(!inOutCourse)
  {
    cerr<<"File could not be opened."<<endl;
    exit(1);
  }//end if

  int choice;//store user choice

  while( (choice=enterChoice() ) !=END)
  {
    switch(choice)
    {
    case PRINT://create text file from record file
      createTextFile(inOutCourse);
      break;
    case UPDATE://update record
      updateRecord(inOutCourse);
      break;
    case NEW://create record
      newRecord (inOutCourse);
      break;
    case DELETE://delete existing record
      deleteRecord (inOutCourse);
      break;
    default://display error if user does not select valid choice
      cerr<<"Incorrect choice"<<endl;
      break;
    }//switch end

    inOutCourse.clear();//reset end of file indicator
  }//end while
  return 0;
}//end main

//enable user to input menu choice
int enterChoice()
{
  //display available option
  cout<<"\nEnter your choice"<<endl
    <<"1-store a formatted text file of courses"<<endl
    <<" called\"print.txt\" for printing"<<endl
    <<"2-update an account"<<endl
    <<"3-add a new course"<<endl
    <<"4-delete a course"<<endl
    <<"5-end program\n? ";

  int menuChoice;
  cin>>menuChoice; //input menu selection from user
  return menuChoice;
}//end function enterChoice

//crete formatted text file for printing
void createTextFile(fstream &readFromFile)
{
  //create text File
  ofstream outPrintFile("print.txt",ios::out);

  //exit program if ofstream cannot create file
  if(!outPrintFile)
  {
    cerr<<"File could not be created."<<endl;
    exit(1);
  }//end if

  outPrintFile<<left<<setw(20)<<"Number"<<setw(20)
    <<"Title"<<setw(20)<<"Year"<<setw(20)<<"Leader"<<setw(20)
    <<"Internal Tel"<<setw(20)<<"Room No."<<setw(20)<<"Department"<<endl;

  //set file-position pointer to beginning of readFromFile
  readFromFile.seekg(0);

  //read first record from record file
  CourseData course;
  readFromFile.read(reinterpret_cast<char *>( &course),
    sizeof(CourseData));

  //copy all records from record file into text file
  while( !readFromFile.eof() )
  {
    //write single record to text file
    if(course.getCourseNumber()!=0)//skip empty records
      outputLine(outPrintFile,course);

    //read next record fromrecord file
    readFromFile.read(reinterpret_cast<char *>(&course),
      sizeof(CourseData));
  }//end while
}//end function createTextFile

//update balance in record
void updateRecord(fstream &updateFile)
{
  //obtain number of course to update
  int account=getCourse( "Enter course to update");

  //move file position pointer to correct record in file
  updateFile.seekg((account-1)*sizeof(CourseData));

  //read first record from file
  CourseData course;
  cout<<left<<course.getCourseNumber()<<' '
    <<course.getCourseTitle()<<' '
    <<course.getCourseYear()<<' '
    <<course.getCourseLeader()<<' '
    <<course.getInternalTelCour()<<' '
    <<course.getRoomNumber()<<' '
    <<course.getDepartmentName();
  cout<<"Muak\n";
  updateFile.read(reinterpret_cast< char *>(&course),
    sizeof(CourseData));

  //update record
  if(course.getCourseNumber() !=0)
  {
    outputLine(cout,course); //display the record

    int updatechoice;
    //request user to specify transaction
    cout<<"\nChoice\n"
      <<"1.Number\n"
      <<"2.Title\n"
      <<"3.Year\n"
      <<"4.Leader\n"
      <<"5.Internal Tel\n"
      <<"6.Room No.\n"
      <<"7.Department\n";
    cin>>updatechoice;

    if(updatechoice==1)
    {
      cout<<left<<setw(5)<<course.getCourseNumber()<<setfill(' ')
        <<setw(20)<<course.getCourseTitle()
        <<setw(20)<<course.getCourseYear()
        <<setw(20)<<course.getCourseLeader()
        <<setw(20)<<course.getInternalTelCour()
        <<setw(20)<<course.getRoomNumber()
        <<setw(20)<<course.getDepartmentName();

      cout<<"\nEnter Course Number:\n";
      int temp;//Number
      cin>>temp;

      //update record Data
      int oldData=course.getCourseNumber();
      course.setCourseNumber(oldData + temp);
      outputLine(cout,course);//display record
    }
    else if(updatechoice==2)
    {
      string transaction;//charge or payment
      cin>>transaction;

      //update record Data
      string oldData=course.getCourseTitle();
      course.setCourseTitle (oldData + transaction);
      outputLine(cout,course);
    }


    updateFile.seekp((account-1)*sizeof(CourseData));

    updateFile.write(reinterpret_cast<const char *>(&course), sizeof(CourseData));

  }
  else
    cerr<<"Course #"<<account
    <<"has no information."<<endl;
}


void newRecord(fstream &insertInFile)
{
  //obtain number of account to create
  int account=getCourse("Enter new course number");

  //move file-position pointer to correct record in file
  insertInFile.seekg( ( account-1)* sizeof(CourseData));

  //read record from file
  CourseData course;
  insertInFile.read(reinterpret_cast< char *>( &course),
    sizeof(CourseData));

  //create record, if record does not previously exist
  if(course.getCourseNumber()==0)
  {
    char courseTitle[20];
    int courseyear;
    char courseLeader[20];
    char internalTelCour[20];
    int roomNumber;
    char departmentName[20];

    //user enters courseTitle,courseyear,courseLeader,internalTelCour,roomNumber,departmentName
    cout<<"Enter Title, Year, Leader, Internal Tel,Room No., Department\n? ";
    cin>>setw(20)>>courseTitle;
    cin>>setw(20)>>courseyear;
    cin>>setw(20)>>courseLeader;
    cin>>setw(20)>>internalTelCour;
    cin>>setw(20)>>roomNumber;
    cin>>setw(20)>>departmentName;

    course.setCourseTitle (courseTitle);
    course.setCourseYear(courseyear);
    course.setCourseLeader(courseLeader);
    course.setInternalTelCour (internalTelCour);
    course.setRoomNumber (roomNumber);
    course.setDepartmentName (departmentName);

    cout<<"course.setRoomNumber"<<course.getRoomNumber()<<endl;

    //move file position pointer to correct recorsd file
    insertInFile.seekp((account-1) * sizeof(CourseData));

    //insert record in file
    insertInFile.write(reinterpret_cast<const char *>(&course),sizeof(CourseData));
  }//end if
  else//display error if account already exists
    cerr<<"Course #"<<account
    <<" already contains information."<<endl;
}//end function newRecord

//delete an existing record
void deleteRecord(fstream &deleteFromFile)
{
  //obtain number of course to delete
  int account=getCourse("Enter account to delete");

  //move file-position pointer to correct record in file
  deleteFromFile.seekg((account-1)*sizeof(CourseData));

  //read record from file
  CourseData course;
  deleteFromFile.read(reinterpret_cast<char *>(&course),
    sizeof(CourseData));

  //delete record, if record exists in file
  if(course.getCourseNumber() !=0)

  {
    CourseData blankCourse;//create blank record

    //move file-position pointer to correct record in file
    deleteFromFile.seekp((account-1)*sizeof(CourseData));

    //replace existing record with blank record
    deleteFromFile.write(
      reinterpret_cast< const char *>(&blankCourse),
      sizeof(CourseData));

    cout<<"Course #"<<account<<" deleted.\n";
  }//end if
  else//display error if record does not exist
    cerr<<"Course #"<<account<<" is empty.\n";
}//end deleteRecord

//display single record
void outputLine(ostream &output, const CourseData &record)
{
  cout<<"Hey"<<endl;
  output<<left<<setw(20)<<record.getCourseNumber()
    <<setw(20)<<record.getCourseTitle()
    <<setw(20)<<record.getCourseYear()
    <<setw(20)<<record.getCourseLeader()
    <<setw(20)<<record.getInternalTelCour()
    <<setw(20)<<record.getRoomNumber()
    <<setw(20)<<record.getDepartmentName();
}//end function outputLine


//obtain course number value from user
int getCourse(const char * const prompt)
{
  int courseNumber;

  //obtain course number value
  do
  {
    cout<<prompt<<" (1-100):";
    cin>>courseNumber;
  }while(courseNumber<1||courseNumber>100);

  return courseNumber;
}//end function getCourse

This is the problem code
VERY VERY SUPER URGENT :sad:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.