Question

In: Computer Science

HW_6a - Read a text file Create a new C++ project and name it as:   Numbers...

HW_6a - Read a text file

  1. Create a new C++ project and name it as:   Numbers
  2. Create a text file and

    save it as:   data.txt

  • Create and save the file

     in a C++ project

     in the Resource folder.

  1. Enter the following numbers:        3

                                                             4

                                                             5       Note:   After you enter the 5, don’t press the enter key.

  1. Save and close the file.

  1. Add another file and name it:   Source.cpp

  1. Write one statement that declares a file object and opens it for reading.

  1. Include code that checks to see if the file opened successfully.
  • If the file does not open, the program should display:   Error opening file!
    • The program should then close.

  • Use a while loop to read the text file and output the numbers to the screen.

   (See OUTPUT below)

  • The while loop should read the file until the endof-file marker is encountered.
  • One number is read and displayed each iteration.

/* OUTPUT

Here are the numbers in the file:

     3

     4

     5

Press any key to continue . . . */

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

Solutions

Expert Solution

Numbers.cpp

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

int main () {
    ofstream myfile ("data.txt");
    int a, b, c;
    if (myfile.is_open())
    {
        cout << "Enter the three numbers: \n";
        cin >> a;
        cin >> b;
        cin >> c;
        myfile << a << endl;
        myfile << b << endl;
        myfile << c << endl;
        myfile.close();
    }
    else cout << "Error opening file!";
    return 0;
}

Output:

Source.cpp

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

int main () {
    string line;
    ifstream myfile ("data.txt");
    if (myfile.is_open())
    {
        // OUTPUT
        cout << "Here are the numbers in file: \n";
        while ( getline (myfile,line) )
        {
            cout << line << '\n';
        }
        myfile.close();
    }

    else cout << "Error opening file!"; 

    return 0;
}

Output:

Thumbs Up Please !!!


Related Solutions

Write a simple phonebook using c++ that read text file name list.txt and has:  first name, last...
Write a simple phonebook using c++ that read text file name list.txt and has:  first name, last name, and phone number as the example: MIKEL BUETTNER 3545044 ENOCH BUGG 2856220 BRENDON LAVALLEY 433312 QUINTIN CREEK 5200413 JAMISON MILLETT 6243458 FLORENCIO PUMPHREY 3296862 DARRICK FREGOSO 6868442 TOBIAS GLASSMAN 6040564 and then ask the user to add a new contact first name, last name, and phone number and same the information into a file. Use array and pointer
in c++ (Sum, average and product of numbers in a file) Suppose that a text file...
in c++ (Sum, average and product of numbers in a file) Suppose that a text file Exercise13_3.txt contains six integers. Write a program that reads integers from the file and displays their sum, average and product. Integers are separated by blanks. Instead of displaying the results on the screen, send the results to an output named using your last name. Example:       Contents of Exercise13_3.txt: 100 95 88 97 71 67 80 81 82             Contents of YourLastName.txt: Your...
In linux , Using a simple text editor, create a text file with the following name...
In linux , Using a simple text editor, create a text file with the following name &quot;Test&quot; and content: GNU GENERAL PUBLIC LICENSE The GNU General Public License is a free, copy left license for the GNU General Public License is intended to guarantee your freedom to GNU General Public License for most of our software; it applies … 2-Write the command to create the text file. 3-Print the file permissions. 4-Create a directory named &quot;361&quot; 5-Copy file &quot;Test&quot; to...
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...
C++ Goals:Practicing arrays Create a program that will read whole numbers from a file called Labs4-7Mu.dat...
C++ Goals:Practicing arrays Create a program that will read whole numbers from a file called Labs4-7Mu.dat (posted on Canvas)and store it into an array. The number of values in the file is less than 300 and all the values are whole numbers. The actual number of values stored in the file should be determined. Your program should then prompt the user to enter another whole number between 2 and 20 (you may assume the user enters a valid value) and...
For c language. I want to read a text file called input.txt for example, the file...
For c language. I want to read a text file called input.txt for example, the file has the form. 4 hello goodbye hihi goodnight where the first number indicates the n number of words while other words are separated by newlines. I want to store these words into a 2D array so I can further work on these. and there are fewer words in the word file than specified by the number in the first line of the file, then...
Use Vi text editor or ATOM to create a bash script file. Use the file name...
Use Vi text editor or ATOM to create a bash script file. Use the file name ayaan.sh. The script when ran it will execute the following commands all at once as script. Test your script file make sure it works. Here is the list of actions the script will do: It will create the following directory structure data2/new2/mondaynews off your home directory. inside the mondaynews, create 4 files sports.txt, baseball.txt, file1.txt and file2.txt. Make sure file1.txt and file2.txt are hidden...
how to creat Create a text file that contains a list of integer numbers separated by...
how to creat Create a text file that contains a list of integer numbers separated by comma (numbers.txt). E.g. 3, 7, 5, 1, 11, 8,2, 6 2- how to Write a python program (minMax.py) to read the file numbers.txt and find the largest and smallest numbers in the file. (without using use min(), max()). calculate the average and output the results. 3-Use python file i/o, write a python program (fileCopy.py) that makes a copy of minMax.py (Read contents of minMax.cp...
HW_6d - Write a struct object to a binary file. Create a new C++ project and...
HW_6d - Write a struct object to a binary file. Create a new C++ project and name it as:   Cats Create a Source.cpp file. Declare a struct named Cat. (in the Source.cpp file, or in a separate header file) Each Cat object has a name and age. The name data member is a c_string. The age data member is an integer. Ask the user to enter 3 cats. Use a while loop to read the information about one cat entered...
How do I do this: Write a program that can read a text file of numbers...
How do I do this: Write a program that can read a text file of numbers and calculate the mean and standard deviation of those numbers. Print the result in another text file. Put the result on the computer screen. EACH LINE OF THE PROGRAM MUST BE COMMENTED!
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT