Question

in c++ pleaseHW09: Read/Write File Ints Now that weve had a taste of what file I/O is all about, heres your chance to try it out on your own! Youre to write a program that will prompt the user if he/she would like to read ints from a file (and have them displayed to stdout) or write ints to a file for safekeeping. If the user wishes to save a set of numbers, then the file nums.txt is opened for writing and the user is prompted for integer values which are written to the output file. If the user decides to read the ints from nums.txt, then the file is opened and the integers it contains are extracted and displayed to stdout. If youd like to see some sample runs, take a look at ReadWritelntsSampleRuns.txt. For this implementation, youll have to write a couple of other functions in addition to the main function: . writents-this function is called from main if the user wants to save a set of integers to the nums.txt file: it accepts as an input argument an opened output file stream object thats ready for writing. It then prompts the user for five integer values, each of which are written to the output file before returning back to the caller Displaylnts this function is called from main if the user wants to display the contents of nums.txt to stdout; it accepts as an input argument an opened input file stream object thats ready for reading. It proceeds to extract the five integer values and display them to stdout. The main function will drive the whole program, itll be responsible for determining which function to call as well as the opening and closing of files. For more information, you may want to look in your book on p.308 for an introduction to file I/O, and a simple example on p.313 where the author demonstrates the.open and.close member functions you can call once youve declared an ifstream or ofstream variable. (However, dont forget to call the .fail function after you open the file so you can check if you were successful in opening the file; see p.316 for a brief discussion of that as well as a code sample!) Finally, on p.334 where the author points out that when you pass file stream variables as arguments to functions, they must be passed by reference

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<iostream>
#include<fstream>

using namespace std;

void WriteInts(ofstream &fout){
    int a;
    cout << "Enter five integers:";
    for (int i = 0; i<5; i++){
        cin >> a;
        fout << a << " ";
    }
   
}

void DisplayInts(ifstream &fin){
    int a;
    cout << "Integers are as follows:";
    for (int i = 0; i<5; i++){
        fin >> a;
        cout << a << " ";
    }
    cout << endl;
    fin.close();
}


int main(){

   ifstream fin, fin1;
   string name;
   string ch;
   int data[100];

  
   
   cout << "Enter a file to read:";
   cin >> name;
   fin.open(name.c_str());
   if (!fin){
      cout << "Error opening file\n";  
   }
   else {
     int count = 0;
     while(fin >> data[count]){
         count++;
     }
     cout << "File has been read and following is the data:\n";
     for (int i = 0; i<count; i++)
         cout << data[i] << " ";
     cout << "\n";
   }
   cout << "Do you want to write integers to nums.txt(y/n):";
   cin >> ch;
   if (ch[0] == 'y'){
        ofstream fout("nums.txt");
        WriteInts(fout);
        fout.close();
   }
   cout << "Do you want to display integers in nums.txt(y/n):";
   cin >> ch;
   if (ch[0] == 'y'){
        ifstream fin("nums.txt");
        DisplayInts(fin);
        fin.close();
   }
  
   return 0;
}

e Computer Science ques × G reading a binary file in C++ C++-Reading and writing t | + ← → 。 |HomeworkLib.com/homework-help/expertq

Add a comment
Know the answer?
Add Answer to:
in c++ please HW09: Read/Write File Ints Now that we've had a taste of what file...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write C++ Code in single source file (.cpp file) containing following user defined functions: 1. IsEven-...

    Write C++ Code in single source file (.cpp file) containing following user defined functions: 1. IsEven- takes an integer input (one argument of type int) and return true or false. 2. IsPositive- takes a float input (one argument of type double) and return a Boolean value. The function returns the true if its argument is positive and false if its argument is zero or negative. 3. IsPrime- takes an integer input and return true or false. 4. NumOfDigits- takes an...

  • The name of the C++ file must be search.cpp Write a program that will read data...

    The name of the C++ file must be search.cpp Write a program that will read data from a file. The program will allow the user to specify the filename. Use a loop that will check if the file is opened correctly, otherwise display an error message and allow the user to re-enter a filename until successful. Read the values from the file and store into an integer array. The program should then prompt the user for an integer which will...

  • For this c++ assignment, Overview write a program that will process two sets of numeric information....

    For this c++ assignment, Overview write a program that will process two sets of numeric information. The information will be needed for later processing, so it will be stored in two arrays that will be displayed, sorted, and displayed (again). One set of numeric information will be read from a file while the other will be randomly generated. The arrays that will be used in the assignment should be declared to hold a maximum of 50 double or float elements....

  • A. File I/O using C library functions File I/O in C is achieved using a file...

    A. File I/O using C library functions File I/O in C is achieved using a file pointer to access or modify files. Processing files in C is a four-step process: o Declare a file pointer. o Open the desired file using the pointer. o Read from or write to the file and finally, o Close the file. FILE is a structure defined in <stdio.h>. Files can be opened using the fopen() function. This function takes two arguments, the filename and...

  • Use c++ as programming language. The file needs to be created ourselves (ARRAYS) Write a program...

    Use c++ as programming language. The file needs to be created ourselves (ARRAYS) Write a program that contains the following functions: 1. A function to read integer values into a one-dimensional array of size N. 2. A function to sort a one-dimensional array of size N of integers in descending order. 3. A function to find and output the average of the values in a one dimensional array of size N of integers. 4. A function to output a one-dimensional...

  • Hi everyone, I have a C programming problem, answers are better with explanations. Background Materials: The...

    Hi everyone, I have a C programming problem, answers are better with explanations. Background Materials: The Task: The Answer: The completed C code. Below is the file charIO4C.c: #include <stdio.h> #include <ctype.h> #define QUIT_LETTER 'q' int main(void) { int c, line_length; // Each pass through the outer loop reads a line of input. while (1) { printf("\nEnter a line of text. (To quit, start the line with %c.)\n", QUIT_LETTER); c = fgetc(stdin); if (c == EOF || c == QUIT_LETTER)...

  • Please use python 3 programming language Write a function that gets a string representing a file...

    Please use python 3 programming language Write a function that gets a string representing a file name and a list. The function writes the content of the list to the file. Each item in the list is written on one line. Name the function WriteList. If all goes well the function returns true, otherwise it returns false. Write another function, RandomRange that takes an integer then it returns a list of length n, where n is an integer passed as...

  • //Done in C please! //Any help appreciated! Write two programs to write and read from file...

    //Done in C please! //Any help appreciated! Write two programs to write and read from file the age and first and last names of people. The programs should work as follows: 1. The first program reads strings containing first and last names and saves them in a text file (this should be done with the fprintf function). The program should take the name of the file as a command line argument. The loop ends when the user enters 0, the...

  • Program 7 File Processing and Arrays (100 points) Overview: For this assignment, write a program that...

    Program 7 File Processing and Arrays (100 points) Overview: For this assignment, write a program that will process monthly sales data for a small company. The data will be used to calculate total sales for each month in a year. The monthly sales totals will be needed for later processing, so it will be stored in an array. Basic Logic for main() Note: all of the functions mentioned in this logic are described below. Declare an array of 12 float/doubles...

  • c++ CSI Lab 6 This program is to be written to accomplish same objectives, as did...

    c++ CSI Lab 6 This program is to be written to accomplish same objectives, as did the program Except this time we modularize our program by writing functions, instead of writing the entire code in main. The program must have the following functions (names and purposes given below). Fot o tentoutefill datere nedefremfite You will have to decide as to what arguments must be passed to the functions and whether such passing must be by value or by reference or...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT