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

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.
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....
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"
Develop a C++ program that looks for a given value in a text file full of...
Develop a C++ program that looks for a given value in a text file full of integer values Prompt the user for a value to search for in the file No input validation is required Open the accompanying text file named numbers.txt Search the contents of the text file You may not "hard-code" the quantity of values found in the file... Use a loop of some sort until the end of the text file is reached Maintain how many many...
Write a C++ program that reads a string from a text file and determines if the...
Write a C++ program that reads a string from a text file and determines if the string is a palindrome or not using stacks and queue
Write a C program to find out the number of words in an input text file...
Write a C program to find out the number of words in an input text file (in.txt). Also, make a copy of the input file. Solve in C programming.
Could you write a c- program that reads a text file into a linked list of...
Could you write a c- program that reads a text file into a linked list of characters and then manipulate the linked list by making the following replacements 1. In paragraph 1 Replace all “c” with “s” if followed by the characters “e”, “i” or “y”; otherwise 2. In pragraph 2 Replace "We" with v"i" This is the text to be manipulated: Paragraph1 She told us to take the trash out. Why did she do that? I wish she would...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT