Question

In: Computer Science

Add an item to a Text File / C++ the program will prompt for a filename...

  • Add an item to a Text File / C++

  • the program will prompt for a filename AND then prompt for a text file item. text file items always contain an unknown amount of spaces, (see getline).

  • Allow the user to put both the filename and the extensions, like groceries.txt or retirementToDo.clist

  • Don't add any directory information to the filenames, use them as typed.

  • If the file does exist, the item will be APPENDED to the end of the file.

  • If that file does not exist, it will be created. (This will happen anyway with append)

The user may put in a checklist item with any number of spaces. (use getline(), not >>)

Solutions

Expert Solution

Attached the code. It has comments for explanation, you should go through them. If you get any queries, feel free to talk about it.

Program Screenshot for Indentation Reference:

Sample Output:

./fileitem
Enter filename: checklist.txt
Enter item to add: Finish web project by tomorrow
./fileitem
Enter filename: checklist.txt
Enter item to add: Get membership for gym
cat checklist.txt
Finish web project by tomorrow
Get membership for gym

Program code to copy:

#include <iostream>
#include <fstream>

using namespace std;


int main() {
    // ask for filename
    string filename;

    cout << "Enter filename: ";
    cin >> filename;

    // open file stream is append mode
    fstream file(filename, ios::in | ios::out | ios::app); // ios::app is for append
    // check file opened or created
    if (!file) {
        cout << "Unable to access file" << endl;
        return -1;
    }
    // get item to add
    string item;
    cout << "Enter item to add: ";
    // ignore newlines in input buffer
    cin.ignore(1, '\n');
    getline(cin, item); // use getline
    // add to the file
    file << item << endl;
    // close file
    file.close();
    return 0;
}


Related Solutions

(previous program) Prompt the user for the filename. Create a new file object sensehat_data_file which opens...
(previous program) Prompt the user for the filename. Create a new file object sensehat_data_file which opens the file in write mode. Write a loop which will read in 20 values for temperature and humidity from the SenseHat. Sleep 0.5 seconds between each reading taken from the SenseHat Write out the temperature and humidity to the file sensehat_data_file, separated by a comma. Close the file.) ONLY ANWSER problem below Then Open the file in read only mode. Use a loop to...
Write a python program that does the following: Prompt for a file name of text words....
Write a python program that does the following: Prompt for a file name of text words. Words can be on many lines with multiple words per line. Read the file and convert the words to a list. Call a function you created called list_to_once_words(), that takes a list as an argument and returns a list that contains only words that occurred once in the file. Print the results of the function with an appropriate description. Think about everything you must...
Write a C++ program to create a text file. Your file should contain the following text:...
Write a C++ program to create a text file. Your file should contain the following text: Batch files are text files created by programmer. The file is written in notepad. Creating a text file and writing to it by using fstream: to write to a file, you need to open thew file as write mode. To do so, include a header filr to your program. Create an object of type fsrteam. Open the file as write mode. Reading from a...
Write a program that will ask for the user to input a filename of a text...
Write a program that will ask for the user to input a filename of a text file that contains an unknown number of integers. And also an output filename to display results. You will read all of the integers from the input file, and store them in an array. (You may need to read all the values in the file once just to get the total count) Using this array you will find the max number, min number, average value,...
Write a C program that Reads a text file(any file)  and writes it to a binary file....
Write a C program that Reads a text file(any file)  and writes it to a binary file. Reads the binary file and converts it to a text file.
C++ code a program should prompt the user for the name of the output file. The...
C++ code a program should prompt the user for the name of the output file. The file should be opened for write operations. Values calculated by the program will be written to this file. The program must verify that the file opened correctly. If the file did not open, an error message should be printed and the user should be prompted again. This should continue until the user supplies a valid filename.
In C++, write a program that reads data from a text file. Include in this program...
In C++, write a program that reads data from a text file. Include in this program functions that calculate the mean and the standard deviation. Make sure that the only global variables are the actual data points, the mean, the standard deviation, and the number of data entered. All other variables must be local to the function. At the top of the program make sure you use functional prototypes instead of writing each function before the main function... ALL LINES...
Using C++, write a code that this program always stores text file output into a text...
Using C++, write a code that this program always stores text file output into a text file named "clean.txt". -The program should read one character at a time from "someNumbers.txt", and do the following. -If it is a letter, print that letter to the screen, AND also store it in the text file. All letters should be converted to lowercase beforehand. -If it is a number, print that number to screen, but do NOT store it in the text file....
Create this C++ program using classes 1. Create a file text file with a string on...
Create this C++ program using classes 1. Create a file text file with a string on it 2. Check the frecuency of every letter, number and symbol (including caps) 3. Use heapsort to sort the frecuencys found 4. Use huffman code on the letters, symbols or numbers that have frecuencys I created the file, and the frecuency part but i'm having trouble with the huffman and heapsort implementation.
1. Write a program that prompts the user for a filename, then reads that file in...
1. Write a program that prompts the user for a filename, then reads that file in and displays the contents backwards, line by line, and character-by character on each line. You can do this with scalars, but an array is much easier to work with. If the original file is: abcdef ghijkl the output will be: lkjihg fedcba Need Help with this be done in only PERL. Please use "reverse"
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT