File Processing C++

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jan 2006
Posts: 6
Reputation: fsloke is an unknown quantity at this point 
Solved Threads: 0
fsloke fsloke is offline Offline
Newbie Poster

File Processing C++

 
0
  #1
Mar 5th, 2006
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:
  1. //CourseData.h
  2.  
  3. #ifndef COURSEDATA_H
  4. #define COURSEDATA_H
  5. using namespace std;
  6.  
  7. #include <string>
  8.  
  9. class CourseData
  10. {
  11. private:
  12. int courseNumber;
  13. char courseTitle[20];
  14. int courseyear;
  15. char courseLeader[20];
  16. char internalTelCour[20];
  17. int roomNumber;
  18. char departmentName[20];
  19. public:
  20. //default CourseData constructor
  21. CourseData(int =0, string ="",int =0,string ="",string ="",int =0,string ="");
  22.  
  23. //accessor functions for courseNumber
  24. void setCourseNumber( int );
  25. int getCourseNumber() const;
  26.  
  27. //accessor functions for courseTitle
  28. void setCourseTitle (string);
  29. string getCourseTitle() const;
  30.  
  31. //accessor functions for courseyear
  32. void setCourseYear( int );
  33. int getCourseYear () const;
  34.  
  35. //accessor functions for courseLeader
  36. void setCourseLeader(string);
  37. string getCourseLeader() const;
  38.  
  39. //accessor functions for internalTelCour
  40. void setInternalTelCour (string);
  41. string getInternalTelCour() const;
  42.  
  43. //accessor functions for roomNumber
  44. void setRoomNumber (int);
  45. int getRoomNumber () const;
  46.  
  47. //accessor functions for departmentName
  48. void setDepartmentName (string);
  49. string getDepartmentName () const;
  50. };
  51.  
  52. #endif
  53. >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
  54. //CourseData.cpp
  55.  
  56. #include <string>
  57.  
  58. #include "CourseData.h"
  59.  
  60. //default CourseData constructor
  61. CourseData::CourseData(int courseNumberValue, string courseTitleValue
  62. ,int couseyearValue, string courseLeaderValue, string internalTelCourValue
  63. ,int roomNumberValue, string departmentNameValue)
  64. {
  65. setCourseNumber( courseNumberValue );
  66. setCourseTitle (courseTitleValue);
  67. setCourseYear( couseyearValue );
  68. setCourseLeader(courseLeaderValue);
  69. setInternalTelCour (internalTelCourValue);
  70. setRoomNumber (roomNumberValue);
  71. setDepartmentName (departmentNameValue);
  72. }//end CourseData constructor
  73.  
  74. //get courseNumber value
  75. int CourseData::getCourseNumber() const
  76. {
  77. return courseNumber;
  78. }//end function getCourseNumber
  79.  
  80. //set courseNumber value
  81. void CourseData::setCourseNumber( int courseNumberValue)
  82. {
  83. courseNumber=courseNumberValue;
  84. }//end function setCourseNumber
  85.  
  86. //get courseTitle value
  87. string CourseData::getCourseTitle() const
  88. {
  89. return courseTitle;
  90. }//end function getCourseTitle
  91.  
  92. //set CourseTitle value
  93. void CourseData::setCourseTitle (string courseTitleString )
  94. {
  95. //copy at most 20 characters from string to courseTitle
  96. const char *courseTitleValue=courseTitleString.data();
  97. int length=courseTitleString.size();
  98. length=( length < 20 ? length:19);
  99. strncpy(courseTitle, courseTitleValue, length);
  100. courseTitle[length]='\0';//append null character to courseTitle
  101. }//end function setCourseTitle
  102.  
  103. //get courseyear
  104. int CourseData::getCourseYear () const
  105. {
  106. return courseyear;
  107. }//end function getCourseYear
  108.  
  109. //set couseyear value
  110. void CourseData::setCourseYear( int couseyearValue)
  111. {
  112. courseyear=couseyearValue;
  113. }//end function setCourseYear
  114.  
  115. //get CourseLeader value
  116. string CourseData::getCourseLeader() const
  117. {
  118. return courseLeader;
  119. }//end function getCourseLeader
  120.  
  121. //set CourseLeader value
  122. void CourseData::setCourseLeader (string courseLeaderString )
  123. {
  124. //copy at most 20 characters from string to courseLeader
  125. const char *courseLeaderValue=courseLeaderString.data();
  126. int length=courseLeaderString.size();
  127. length=( length < 20 ? length:19);
  128. strncpy(courseLeader, courseLeaderValue, length);
  129. courseLeader[length]='\0';//append null character to courseLeader
  130. }//end function setCourseLeader
  131.  
  132. //get InternalTelCour value
  133. string CourseData::getInternalTelCour() const
  134. {
  135. return internalTelCour;
  136. }//end function getInternalTelCour
  137.  
  138. //set InternalTelCour value
  139. void CourseData::setInternalTelCour (string internalTelCourString )
  140. {
  141. //copy at most 12 characters from string to internalTelCour
  142. const char *internalTelCourValue=internalTelCourString.data();
  143. int length=internalTelCourString.size();
  144. length=( length < 12 ? length:11);
  145. strncpy(internalTelCour, internalTelCourValue, length);
  146. internalTelCour[length]='\0';//append null character to internalTelCour
  147. }//end function setInternalTelCour
  148.  
  149. //get roomNumber
  150. int CourseData::getRoomNumber () const
  151. {
  152. return roomNumber;
  153. }//end function getRoomNumber
  154.  
  155. //set RoomNumber value
  156. void CourseData::setRoomNumber ( int roomNumberValue)
  157. {
  158. roomNumber=roomNumberValue;
  159. }//end function setRoomNumber
  160.  
  161. //get departmentName value
  162. string CourseData::getDepartmentName () const
  163. {
  164. return departmentName;
  165. }//end function getDepartmentName
  166.  
  167. //set DepartmentName value
  168. void CourseData::setDepartmentName (string departmentNameString )
  169. {
  170. //copy at most 20 characters from string to departmentName
  171. const char *departmentNameValue=departmentNameString.data();
  172. int length=departmentNameString.size();
  173. length=( length < 20 ? length:19);
  174. strncpy(departmentName, departmentNameValue, length);
  175. departmentName[length]='\0';//append null character to departmentName
  176. }//end function setDepartmentName
  177.  
  178. >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
  179. //main
  180.  
  181. #include <iostream>
  182. #include <fstream>
  183. #include <iomanip>
  184. #include <cstdlib>
  185. #include "CourseData.h"
  186.  
  187. int enterChoice();
  188. void createTextFile( fstream&);
  189. void updateRecord( fstream&);
  190. void newRecord( fstream&);
  191. void deleteRecord( fstream&);
  192. void outputLine( ostream&, const CourseData &);
  193. int getCourse( const char * const);
  194.  
  195. enum Choice { PRINT=1, UPDATE, NEW, DELETE, END};
  196.  
  197. int main()
  198. {
  199. //open file for reading and writing
  200. fstream inOutCourse("course.txt",ios::in|ios::out| ios::binary);
  201.  
  202. //exit program if fstream cannot open file
  203. if(!inOutCourse)
  204. {
  205. cerr<<"File could not be opened."<<endl;
  206. exit(1);
  207. }//end if
  208.  
  209. int choice;//store user choice
  210.  
  211. while( (choice=enterChoice() ) !=END)
  212. {
  213. switch(choice)
  214. {
  215. case PRINT://create text file from record file
  216. createTextFile(inOutCourse);
  217. break;
  218. case UPDATE://update record
  219. updateRecord(inOutCourse);
  220. break;
  221. case NEW://create record
  222. newRecord (inOutCourse);
  223. break;
  224. case DELETE://delete existing record
  225. deleteRecord (inOutCourse);
  226. break;
  227. default://display error if user does not select valid choice
  228. cerr<<"Incorrect choice"<<endl;
  229. break;
  230. }//switch end
  231.  
  232. inOutCourse.clear();//reset end of file indicator
  233. }//end while
  234. return 0;
  235. }//end main
  236.  
  237. //enable user to input menu choice
  238. int enterChoice()
  239. {
  240. //display available option
  241. cout<<"\nEnter your choice"<<endl
  242. <<"1-store a formatted text file of courses"<<endl
  243. <<" called\"print.txt\" for printing"<<endl
  244. <<"2-update an account"<<endl
  245. <<"3-add a new course"<<endl
  246. <<"4-delete a course"<<endl
  247. <<"5-end program\n? ";
  248.  
  249. int menuChoice;
  250. cin>>menuChoice; //input menu selection from user
  251. return menuChoice;
  252. }//end function enterChoice
  253.  
  254. //crete formatted text file for printing
  255. void createTextFile(fstream &readFromFile)
  256. {
  257. //create text File
  258. ofstream outPrintFile("print.txt",ios::out);
  259.  
  260. //exit program if ofstream cannot create file
  261. if(!outPrintFile)
  262. {
  263. cerr<<"File could not be created."<<endl;
  264. exit(1);
  265. }//end if
  266.  
  267. outPrintFile<<left<<setw(20)<<"Number"<<setw(20)
  268. <<"Title"<<setw(20)<<"Year"<<setw(20)<<"Leader"<<setw(20)
  269. <<"Internal Tel"<<setw(20)<<"Room No."<<setw(20)<<"Department"<<endl;
  270.  
  271. //set file-position pointer to beginning of readFromFile
  272. readFromFile.seekg(0);
  273.  
  274. //read first record from record file
  275. CourseData course;
  276. readFromFile.read(reinterpret_cast<char *>( &course),
  277. sizeof(CourseData));
  278.  
  279. //copy all records from record file into text file
  280. while( !readFromFile.eof() )
  281. {
  282. //write single record to text file
  283. if(course.getCourseNumber()!=0)//skip empty records
  284. outputLine(outPrintFile,course);
  285.  
  286. //read next record fromrecord file
  287. readFromFile.read(reinterpret_cast<char *>(&course),
  288. sizeof(CourseData));
  289. }//end while
  290. }//end function createTextFile
  291.  
  292. //update balance in record
  293. void updateRecord(fstream &updateFile)
  294. {
  295. //obtain number of course to update
  296. int account=getCourse( "Enter course to update");
  297.  
  298. //move file position pointer to correct record in file
  299. updateFile.seekg((account-1)*sizeof(CourseData));
  300.  
  301. //read first record from file
  302. CourseData course;
  303. cout<<left<<course.getCourseNumber()<<' '
  304. <<course.getCourseTitle()<<' '
  305. <<course.getCourseYear()<<' '
  306. <<course.getCourseLeader()<<' '
  307. <<course.getInternalTelCour()<<' '
  308. <<course.getRoomNumber()<<' '
  309. <<course.getDepartmentName();
  310. cout<<"Muak\n";
  311. updateFile.read(reinterpret_cast< char *>(&course),
  312. sizeof(CourseData));
  313.  
  314. //update record
  315. if(course.getCourseNumber() !=0)
  316. {
  317. outputLine(cout,course); //display the record
  318.  
  319. int updatechoice;
  320. //request user to specify transaction
  321. cout<<"\nChoice\n"
  322. <<"1.Number\n"
  323. <<"2.Title\n"
  324. <<"3.Year\n"
  325. <<"4.Leader\n"
  326. <<"5.Internal Tel\n"
  327. <<"6.Room No.\n"
  328. <<"7.Department\n";
  329. cin>>updatechoice;
  330.  
  331. if(updatechoice==1)
  332. {
  333. cout<<left<<setw(5)<<course.getCourseNumber()<<setfill(' ')
  334. <<setw(20)<<course.getCourseTitle()
  335. <<setw(20)<<course.getCourseYear()
  336. <<setw(20)<<course.getCourseLeader()
  337. <<setw(20)<<course.getInternalTelCour()
  338. <<setw(20)<<course.getRoomNumber()
  339. <<setw(20)<<course.getDepartmentName();
  340.  
  341. cout<<"\nEnter Course Number:\n";
  342. int temp;//Number
  343. cin>>temp;
  344.  
  345. //update record Data
  346. int oldData=course.getCourseNumber();
  347. course.setCourseNumber(oldData + temp);
  348. outputLine(cout,course);//display record
  349. }
  350. else if(updatechoice==2)
  351. {
  352. string transaction;//charge or payment
  353. cin>>transaction;
  354.  
  355. //update record Data
  356. string oldData=course.getCourseTitle();
  357. course.setCourseTitle (oldData + transaction);
  358. outputLine(cout,course);
  359. }
  360.  
  361.  
  362. updateFile.seekp((account-1)*sizeof(CourseData));
  363.  
  364. updateFile.write(reinterpret_cast<const char *>(&course), sizeof(CourseData));
  365.  
  366. }
  367. else
  368. cerr<<"Course #"<<account
  369. <<"has no information."<<endl;
  370. }
  371.  
  372.  
  373. void newRecord(fstream &insertInFile)
  374. {
  375. //obtain number of account to create
  376. int account=getCourse("Enter new course number");
  377.  
  378. //move file-position pointer to correct record in file
  379. insertInFile.seekg( ( account-1)* sizeof(CourseData));
  380.  
  381. //read record from file
  382. CourseData course;
  383. insertInFile.read(reinterpret_cast< char *>( &course),
  384. sizeof(CourseData));
  385.  
  386. //create record, if record does not previously exist
  387. if(course.getCourseNumber()==0)
  388. {
  389. char courseTitle[20];
  390. int courseyear;
  391. char courseLeader[20];
  392. char internalTelCour[20];
  393. int roomNumber;
  394. char departmentName[20];
  395.  
  396. //user enters courseTitle,courseyear,courseLeader,internalTelCour,roomNumber,departmentName
  397. cout<<"Enter Title, Year, Leader, Internal Tel,Room No., Department\n? ";
  398. cin>>setw(20)>>courseTitle;
  399. cin>>setw(20)>>courseyear;
  400. cin>>setw(20)>>courseLeader;
  401. cin>>setw(20)>>internalTelCour;
  402. cin>>setw(20)>>roomNumber;
  403. cin>>setw(20)>>departmentName;
  404.  
  405. course.setCourseTitle (courseTitle);
  406. course.setCourseYear(courseyear);
  407. course.setCourseLeader(courseLeader);
  408. course.setInternalTelCour (internalTelCour);
  409. course.setRoomNumber (roomNumber);
  410. course.setDepartmentName (departmentName);
  411.  
  412. cout<<"course.setRoomNumber"<<course.getRoomNumber()<<endl;
  413.  
  414. //move file position pointer to correct recorsd file
  415. insertInFile.seekp((account-1) * sizeof(CourseData));
  416.  
  417. //insert record in file
  418. insertInFile.write(reinterpret_cast<const char *>(&course),sizeof(CourseData));
  419. }//end if
  420. else//display error if account already exists
  421. cerr<<"Course #"<<account
  422. <<" already contains information."<<endl;
  423. }//end function newRecord
  424.  
  425. //delete an existing record
  426. void deleteRecord(fstream &deleteFromFile)
  427. {
  428. //obtain number of course to delete
  429. int account=getCourse("Enter account to delete");
  430.  
  431. //move file-position pointer to correct record in file
  432. deleteFromFile.seekg((account-1)*sizeof(CourseData));
  433.  
  434. //read record from file
  435. CourseData course;
  436. deleteFromFile.read(reinterpret_cast<char *>(&course),
  437. sizeof(CourseData));
  438.  
  439. //delete record, if record exists in file
  440. if(course.getCourseNumber() !=0)
  441.  
  442. {
  443. CourseData blankCourse;//create blank record
  444.  
  445. //move file-position pointer to correct record in file
  446. deleteFromFile.seekp((account-1)*sizeof(CourseData));
  447.  
  448. //replace existing record with blank record
  449. deleteFromFile.write(
  450. reinterpret_cast< const char *>(&blankCourse),
  451. sizeof(CourseData));
  452.  
  453. cout<<"Course #"<<account<<" deleted.\n";
  454. }//end if
  455. else//display error if record does not exist
  456. cerr<<"Course #"<<account<<" is empty.\n";
  457. }//end deleteRecord
  458.  
  459. //display single record
  460. void outputLine(ostream &output, const CourseData &record)
  461. {
  462. cout<<"Hey"<<endl;
  463. output<<left<<setw(20)<<record.getCourseNumber()
  464. <<setw(20)<<record.getCourseTitle()
  465. <<setw(20)<<record.getCourseYear()
  466. <<setw(20)<<record.getCourseLeader()
  467. <<setw(20)<<record.getInternalTelCour()
  468. <<setw(20)<<record.getRoomNumber()
  469. <<setw(20)<<record.getDepartmentName();
  470. }//end function outputLine
  471.  
  472.  
  473. //obtain course number value from user
  474. int getCourse(const char * const prompt)
  475. {
  476. int courseNumber;
  477.  
  478. //obtain course number value
  479. do
  480. {
  481. cout<<prompt<<" (1-100):";
  482. cin>>courseNumber;
  483. }while(courseNumber<1||courseNumber>100);
  484.  
  485. return courseNumber;
  486. }//end function getCourse
This is the problem code
VERY VERY SUPER URGENT
Last edited by Narue; Mar 5th, 2006 at 11:48 am.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: File Processing C++

 
0
  #2
Mar 5th, 2006
No way anyone is going to wade through that unless you edit it to include code tags
http://www.daniweb.com/techtalkforum...cement8-3.html

> VERY VERY SUPER URGENT
http://www.catb.org/~esr/faqs/smart-...ns.html#urgent
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC