Question

In: Computer Science

HW_6c - Append data to existing data in a file. Include the following in the main.cpp...

HW_6c - Append data to existing data in a file.

  1. Include the following in the main.cpp file.

  1. Prompt the user to enter 3 more numbers, then read the numbers and write them to

            the results.txt file.

  • Include code so that when the data is written to file, it is appended (added on to the

    end of the existing data).

  1. Open the results.txt file and verify that the file was written successfully.

/* OUTPUT

Here are the numbers in the file:

3

4

5

Enter 3 more numbers:

6

7

8

The numbers have been written (appended) to results.txt.

Press any key to continue . . . */

Please make the code based on C++, and make a description for each code.

Solutions

Expert Solution

#include<iostream>
#include<fstream>
using namespace std;

/*main function*/
int main()
{  
   ofstream fout; // for writing to a file
   ifstream fin; // for reading from a file
   fin.open("results.txt"); // opening file
   // used append mode which is used to append data at the end of file
   fout.open("results.txt",ios::app);
   if(!fout && !fin){ // check if file not open
       cout<<"Error File not found!"<<endl;
       return 0;
   }
   int num; // to read number in variable
   cout<<"Here are the numbers in the file: \n";
   while(fin>>num){
       cout<<num<<endl; // print content of file
   }  
   cout<<"Enter 3 more numbers: "<<endl; // take input
   for(int i = 0; i < 3; i++){
       cin >> num; // taking input
       fout<<"\n"<<num; // writing to a file
   }
   cout<<"The numbers have been written (appended) to results.txt."<<endl;
   system("pause");
   return 0;
}

/* Screenshot of a code for indentation */

/* OUTPUT */

/* results.txt */


Related Solutions

HW_6b - Read and write a text file. Include the following in the main.cpp file. Add...
HW_6b - Read and write a text file. Include the following in the main.cpp file. Add code so that the numbers that are read from the data.txt file are written to an output           file named:   results.txt After the numbers are written to the file, the following message should be displayed:    The data has been written to the file. Open the results.txt file and verify that the file was written successfully. Please make the code based on C++,...
Create a CodeBlocks project with a main.cpp file. Submit the main.cpp file in Canvas. C++ Language...
Create a CodeBlocks project with a main.cpp file. Submit the main.cpp file in Canvas. C++ Language A Game store sells many types of gaming consoles. The console brands are Xbox, Nintendo, PlayStation. A console can have either 16 or 8 gigabytes of memory. Use can choose the shipping method as either Regular (Cost it $5) or Expedite (Cost is $10) The price list is given as follows: Memory size/Brand Xbox Nintendo PlayStation 16 gigabytes 499.99 469.99 409.99 8 gigabytes 419.99...
Consider the following header file for the intArray object and the following main.cpp and answer the...
Consider the following header file for the intArray object and the following main.cpp and answer the following questions: intArray.h main.cpp #include <iostream> using namespace std; class intArray { friend ostream& operator<<(ostream& outStream, intArray& rhs); public:     intArray();     intArray(int _size);     intArray(int _size, int array[]);     intArray(const intArray&);     ~intArray();     void Print();     void PrintSize();     int Size() const;     int operator[](int) const;     intArray operator=(const intArray ); private:     int size;     int *data; }; 7 #include <iostream>...
Write a java program that can create, read, and append a file. Assume that all data...
Write a java program that can create, read, and append a file. Assume that all data written to the file are string type. One driver class and a class that contains the methods. The program should have three methods as follows: CreateFile - only creating a file. Once it create a file successfully, it notifies to the user that it creates a file successfully. ReadingFile - It reads a contents of the file. This method only allow to read a...
Data Structure using C++ Complete and test the Sudoku problem main.cpp #include <iostream> #include <cmath> #include...
Data Structure using C++ Complete and test the Sudoku problem main.cpp #include <iostream> #include <cmath> #include "sudoku.h" using namespace std; int main() { cout << "See Programming Exercise 18." << endl; } sudoku.cpp #include <iostream> #include "sudoku.h" using namespace std; sudoku::sudoku() { cout << "Write the definition. ." << endl; } sudoku::sudoku(int g[][9]) { cout << "Write the definition." << endl; } void sudoku::initializeSudokuGrid() { cout << "Write the definition." << endl; } void sudoku::initializeSudokuGrid(int g[][9]) { cout << "Write...
For Linux: Write a command to append the top 20 lines of the file seq.fasta to...
For Linux: Write a command to append the top 20 lines of the file seq.fasta to a second file named combined.seq.
C++ Download the attached program and complete the functions. (Refer to comments) main.cpp ~ #include #include...
C++ Download the attached program and complete the functions. (Refer to comments) main.cpp ~ #include #include #define END_OF_LIST -999 using namespace std; /* * */ int exercise_1() { int x = 100; int *ptr; // Assign the pointer variable, ptr, to the address of x. Then print out // the 'value' of x and the 'address' of x. (See Program 10-2) return 0; } int exercise_2() { int x = 100; int *ptr; // Assign ptr to the address of...
Please comments this C++ code and show screenshots of the outputs main.cpp #include<iostream> #include<vector> #include<string> #include"BST.h"...
Please comments this C++ code and show screenshots of the outputs main.cpp #include<iostream> #include<vector> #include<string> #include"BST.h" #include"BST.cpp" using namespace std; std::vector<std::string> tokenize(char line[]) {    std::vector<std::string> tok;        std::string word = "";        for (int i = 0; i < strlen(line); i++)        {            if (i == strlen(line) - 1)            {                word += line[i];                tok.push_back(word);                return tok;       ...
URGENT: USING C++: You are given a main.cpp and a Schedule.h file. You are to write...
URGENT: USING C++: You are given a main.cpp and a Schedule.h file. You are to write a Schedule.cpp file that implements the missing pieces from Schedule.h. // ************************************ // ************* main.cpp ************ // ************************************ #include "Schedule.h" void main() { Schedule s1(10); s1.addEntry(1,20,"Feed Cat"); s1.addEntry(2,40,"Feed Dog"); s1.addEntry(2,50,"Walk Dog"); s1.addEntry(4,0, "Fix Dinner"); s1.addEntry(5,15,"Eat Dinner"); s1.printSchedule(); Schedule s2(s1); // Note this uses the copy constructor cout << endl << "Output from s2 " << endl; s2.printSchedule(); } // ********************************************** // ************* Schedule.h ******************...
Write a program to implement linked list data structure that will have following functions: a. Append...
Write a program to implement linked list data structure that will have following functions: a. Append a node in the list b. Insert a node in the list c. Delete a node from the list d. Display list e. Find maximum value in the list f. Find how many times a value exists in the list. g. Search Portion of the code is give below. You have to write code for the items (e, f, g) Program: #include<stdlib.h> #include<stdio.h> #include<iostream>...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT