Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Create a simple program to demonstrated basic file IO capabilities in C++. Your program must do the following: Create a file Prompt the user for
Create a simple program to demonstrated basic file IO capabilities in C++.Your program must do the following:
Create a file
Prompt the user for a file path and name (getlne(cin, yourFile)) If the file already exists or cannot be opened, ask the user to try again and repeat the process. Extra Credit: If the file exists, ask the user if they want to overwrite the file. If they do, then erase the consents of the file and start at the beginning. If not reject the file and start again.
Add lines of user input text to that file.
The user inputs one line of text at a time (getlne(cin, yourString)). That is then written to the file.
Determine the length of the file.
Use the type streampos. Use seek and tell to get the start. Use seek and tell to get the end.
Add one more line of user input text to the file.
Determine the new length of the file.
Output the file line by line.
Sample output:
Enter a new file name to create: /Desktop/example.txt The file: "/Desktop/example.txt" already exists! Would you like to overwrite the file named: "/Desktop/example.txt" (Y/N): y Attempting to delete file: "/Desktop/example.txt". The file: "/Desktop/example.txt" was successfully created! Enter lines of text to add to the file. Hit <return> on a blank line to end. Enter a text for line 1: This my text for the first line! Enter a text for line 2: And this is more for the second line. Enter a text for line 3: The file was written. The file: "/Desktop/example.txt" is 71 bytes long. Enter the final line of text to add to the file: At last, something for the final line. Writing "At last, something for the final line." to the file. The file: "/Desktop/example.txt" is now 109 bytes long. The file: "/Desktop/example.txt" contains the following lines: Line 1: This my text for the first line! Line 2: And this is more for the second line. Line 3: At last, something for the final line. Program ended with exit code: 0
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started