Examples for Standard C++ Libraries |
This example provides a simple procedure for insertion and retrieval of employee records using IOStreams. This example prompts the user either, add or, retrieve the employee record. The program creates a separate text file for each employee added to the database and updates the record. To retrieve the record of an employee , the program accepts the employee name and opens the file created for the employee (if it exists) and displays the records. In addition, to this the program checks for duplication of employee name entry. If no match is found for the employee details queried it prints "No match found".
The MMP file for IOstream is as shown below:
TARGET Opencppiostreamex.exe TARGETTYPE exe UID 0 0xE91BAF92 USERINCLUDE ..\inc SOURCEPATH ..\data START RESOURCE Opencppiostreamex_reg.rss #ifdef WINSCW TARGETPATH \private\10003a3f\apps #else TARGETPATH \private\10003a3f\import\apps #endif END //RESOURCE SOURCEPATH ..\src SOURCE Opencppiostreamex.cpp SYSTEMINCLUDE \epoc32\include SYSTEMINCLUDE \epoc32\include\stdapis SYSTEMINCLUDE \epoc32\include\stdapis\sys SYSTEMINCLUDE \epoc32\include\stdapis\stlport SYSTEMINCLUDE \epoc32\include\stdapis\stlport\stl STATICLIBRARY libcrt0.lib LIBRARY libstdcpp.lib LIBRARY libc.lib LIBRARY libpthread.lib LIBRARY euser.lib OPTION CW -wchar_t on MACRO _WCHAR_T_DECLARED
The code snippet shows IOStreams usage with the help of a simple employee information portal.
#include<fstream> #include<iostream> #include<string> // This is a GCCE toolchain workaround needed when compiling with GCCE // and using main() entry point #ifdef __GCCE__ #include<staticlibinit_gcce.h> #endif using namespace std; int entering() { char FileName[20]; char EmployeeName[40], Address[50], City[20], State[32], ZIPCode[10]; cout << "Enter the Following pieces of information\n"; cout << "Empl Name: "; cin >> ws; cin.getline(EmployeeName, 40); cout << "Address: "; cin >> ws; cin.getline(Address, 50); cout << "City: "; cin >> ws; cin.getline(City, 20); cout << "State: "; cin >> ws; cin.getline(State, 32); cout << "ZIP Code: "; cin >> ws; cin.getline(ZIPCode, 10); cout << "\nEnter the name of the file you want to create: "; cin >> FileName; ifstream EmplRecords(FileName); if(EmplRecords.is_open()) { cout<<"file name already exits\n"; } else { ofstream EmplRecords(FileName, ios::out); EmplRecords << EmployeeName << "\n" << Address << "\n" << City << "\n" << State << "\n" << ZIPCode; } EmplRecords.close(); return 0; } int reading() { char FileName[20]; char EmployeeName[40], Address[50], City[20], State[32], ZIPCode[10]; cout << "Enter the name of the file you want to open: "; cin >> FileName; ifstream EmplRecords(FileName); if(EmplRecords.is_open()) { EmplRecords.getline(EmployeeName, 40, '\n'); EmplRecords.getline(Address, 50); EmplRecords.getline(City, 20); EmplRecords.getline(State, 32); EmplRecords.getline(ZIPCode, 10); cout << "\n -=- Employee Information -=-"; cout << "\nEmpl Name: " << EmployeeName; cout << "\nAddress: " << Address; cout << "\nCity: " << City; cout << "\nState: " << State; cout << "\nZIP Code: " << ZIPCode; } else { cout<<"No match found\n"; } EmplRecords.close(); return 0; } int main() { char select; char opt; while(1) { cout<<"---=----WELCOME TO NOKIA----=---\n "; cout<<"================================\n"; cout<<"1. To create the profile of new Employees\n"; cout<<"2. To see the details of Employee\n"; cout<<"3. To exit\n"; cout<<"select an option\n"; cin>>select; switch(select) { case '1' : entering(); break; case '2' : reading(); break; case '3' : exit(0); default : cout<<"wrong option\n"; } cout<<"\nWant to continue "<<"press y"; cout<<"\nOR Press any other key to exit\n"; cin>>opt; if( opt=='Y' || opt=='y') continue; else break; } return 0; }