3. Create a text file using Notepad or any other application that saves as a tex
ID: 3696387 • Letter: 3
Question
3. Create a text file using Notepad or any other application that saves as a text file format. Save this file to the root folder of your NetBeans project as employees.txt. In this file, enter five employee records. Each record should consist of a first name in positions 1 through 10, followed by the employee's years of service in positions 11 and 12. This is a fixed width file, so unused spaces for the name would hold a space. The years of service must use numerics for both positions. For example, one record would be for "Mike", who has been employed at the company for four years. His record would consist of positions 1 through 10 as "Mike " (no quotes) and positions 11 and 12 as "04" (no quotes).
4. Create a method named readEmployees that reads the employees.txt file to the command line. Each first name should be preceded with "First name: ". Each year of service should be preceded with "Year(s) of service: ". Be sure to format each year of service so that any leading zeroes are not displayed. Use a try…catch block read and output the text file's records. Handle two potential errors via the catch block. The first should take care of a file that can't be found. The second should be of an Exception type. Each raised exception should include a suitable custom message for that exception type along with the result of its .getMessage() string.
5. Call each of the methods as necessary above from the main method. The pseudocode for the main method will be as given below.
For exercises 3 and 4: Create the employees.txt file (this is done using your text editor). Open and read the employees.txt file and output its contents on a record-by-record basis.
Explanation / Answer
#include<iostream>
using namespace std;
int main()
{
String content;
ifstream abc("emp.txt");
try{
while(!abc.eof)
{
abc>> content;
cout<< content<<endl;
}
}catch(...)
{
cout<<" file not found";
}
catch(FileNotFoundException e)
{
cout<<" Exception opening file"
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.