Форум » C/C++ для начинающих (C/C++ for beginners) » Binary and random access file » Ответить

Binary and random access file

BasicNewbie1: [pre2]The program should store the employee’s records in a random access file and supports a variety of operations such as add a new employee record, modify an employee record, search and display employee records, etc. & You are to use a binary file to keep the records of all employees. Identify and define a suitable structure to keep your employee record. [/pre2] as this mean isnt [pre2]const int RecordSize = ... void readRecord(Employee *theEmployee , int index){ // Note that you have an index instead of size ifstream inFile; inFile.open( "employee.txt" ); if( inFile.is_open() ){ if(inFile.seekg(index * RecordSize)){ // index * RecordSize is the offset of your record getline( inFile , theEmployee->EmployeeID ); // Note that you do not have an array anymore getline( inFile , theEmployee->EmployeeName ); getline( inFile , theEmployee->EmployeeDepartment ); getline( inFile , theEmployee->EmployeeDOB ); getline( inFile , theEmployee->EmployeeAddress ); inFile >> theEmployee->EmployeeWorkHours; inFile >> theEmployee->PayRate; inFile >> theEmployee->totalSalary; } inFile.close(); } } }[/pre2] isnt the coding correct ? as we use the binary file to keep the record isnt mean when ofstream . using [pre2]ios :: binary[/pre2] thats all?

Ответов - 5

Сыроежка: You should correctly set the file open mode specifying that it is a binary file and is opened for read/write (I/O) operations.

BasicNewbie1: mean infile.open ("employee.txt" , ios:: binary )? i get an fatal error that stated 1 unresolve external

Сыроежка: It means that you are using a name that is undefined. Usually such error message conttains a reference to a name that is undefined.


BasicNewbie1: i solved it . but as coder777 said in forum. i cant read any data in case i use random access file. [pre2]void readRecord(Employee *theEmployee , int &index){ // Note that you have an index instead of size ifstream inFile; string filename; cout << "Enter filename to be read : "; getline( cin, filename ); inFile.open( filename.c_str() , ios :: binary ); if( inFile.is_open() ){ if(inFile.seekg(index * RecordSize)){ // index * RecordSize is the offset of your record getline( inFile , theEmployee->EmployeeID ); // Note that you do not have an array anymore getline( inFile , theEmployee->EmployeeFirstName ); getline( inFile , theEmployee->EmployeeLastName ); getline( inFile , theEmployee->EmployeeDepartment ); getline( inFile , theEmployee->EmployeePosition ); inFile >> theEmployee->EmployeeWorkHours; inFile >> theEmployee->PayRate; inFile >> theEmployee->totalSalary; } inFile.close(); } }[/pre2] erm , now what my problem ? i cannot display the data inside my file .

Сыроежка: I already showed you what functions you should use with a file opened in the binary mode. So I do not understand what you are doing. There is such book by Herb Schildt "Herb Shildt's C++ Programming Cookbook". I think it will be useful if you will read this book.



полная версия страницы