Question

In: Computer Science

1) How do you read the contents of a text file that contains whitespace characters as...

1) How do you read the contents of a text file that contains whitespace

characters

as part of its data?

2) What capability does the fstream data type provide that the ifstream

and ofstream

data types do not?

3) What header file do you need to include in a program that performs

file operations?

4) What data type do you use when you want to create a file stream object

that

can write data to a file?

5) What data type do you use when you want to create a file stream object

that

can read data from a file?

6) Why should a program close a file when it

s finished using it?

7) Write code that does the following:

(a) Opens an output file with the filename Numbers.txt ,

uses a loop to write the numbers 1 through 100 to the file,

and then closes the file.

(b) Opens the Numbers.txt file that was created in Part 7a,

reads all of the numbers from the file and

displays them in rows of 10 values, and then closes the file.

(c) Add all of the numbers read from the file and displays their

total.

Solutions

Expert Solution

1. By using getline() method one can read data from file including white spaces.

Example:

#include<iostream>
#include<fstream>
using namespace std;
//driver program
int main()
{
   string str;
   ifstream infile; //declare the file object to open the file
   //open the files in read mode
   infile.open("D:/test.txt");
   //if not open
   if(!infile)
   {
      cout<<endl<<"Unable to open the source file";
          exit(0);
   }
   //read the data from file
   while(! infile.eof())
        {
        getline(infile,str);//read a line including space
        cout<<endl<<str;//display the read data from file
       }
   }

OUTPUT

DATA OF TEST.TXT

2)

ifstream data type represents to input file stream and the object will have the capacity to read data from file.

ofstream data type represents to output file stream and the object will have the capacity to write data into file.

fstream data type represents to both input and output file stream and the object will have the capacity to read data from file and write data into a file.

3) fstream header file is used during the file operation in c++.

such as

#include<fstream>

4)

ofstream data type will be used to write data into file.

5)

ifstream data type will be used to read data from file.

6)

First of all it is a good practice of a programmer to close a file after performing any operation.

But logically

1. In some situations if you open a file in write mode, then the changes will not completely reflect with file unless you close the file.

2. Your program may allocate unwanted memory for this reason. So garbage collector will come into action to close the files and free the memory from unwanted memory blockage. It will increase the execution speed.

So it is a good practice to close a file after completion of operation with it.

7)

#include<iostream>
#include<fstream>
using namespace std;
//driver program
int main()
{
   int i,sum=0,n;
   ifstream in; //declare the file object to open the file in read mode
   ofstream out;//declare the file object to open the file in write mode
   //open the files in write mode
   out.open("D:/Numbers.txt");
   //if not open
   if(!out)
   {
      cout<<endl<<"Unable to open the file";
          exit(0);
   }
     
   for(i=1;i<=100;i++) //loop from 1 to 100
       out<<" "<<i; //write the numbers into file
   out.close(); //close the file
  
   //open the files in read mode
   in.open("D:/Numbers.txt");
   //if not open
   if(!in)
   {
      cout<<endl<<"Unable to open the file";
          exit(0);
   }
   i=0;
   //read the data from file
   while(!in.eof())
        {
        in>>n; //read the number from file and store it in n
    if(i%10==0) //condition for new line
        cout<<endl;
               i++;
        cout<<n<<" ";//print the number
        sum=sum+n;//compute the sum

       }
       in.close();//close the file
       //display the sum
       cout<<endl<<"Sum of numbers from 1 to 100 is : "<<sum;
   }

output


Related Solutions

C Programming How do you read in a text file line by line into a an...
C Programming How do you read in a text file line by line into a an array. example, i have the text file containing: line 1: "qwertyuiop" line 2: "asdfghjkl" line 3: "zxcvbnm" Then in the resulting array i get this: array:"qwertyuiopasdfghjklzxcvbnm"
In Java. Modify the attached GameDriver2 to read characters in from a text file rather than...
In Java. Modify the attached GameDriver2 to read characters in from a text file rather than the user. You should use appropriate exception handling when reading from the file. The text file that you will read is provided. -------------------- Modify GaveDriver2.java -------------------- -----GameDriver2.java ----- import java.io.File; import java.util.ArrayList; import java.util.Scanner; /** * Class: GameDriver * * This class provides the main method for Homework 2 which reads in a * series of Wizards, Soldier and Civilian information from the user...
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!
how to code the text with excess whitespace removed in C++. Whitespace includes spaces and tabs....
how to code the text with excess whitespace removed in C++. Whitespace includes spaces and tabs. Excess whitespace is: • All whitespace at the beginning of the text. • All whitespace at the end of the text. • Any point in the text where more than a single whitespace character is used. For instance, if the input text is: Welcome to C++ ! The output would be: Welcome to C++!
Computer Science - Java Programming How do you read a text file and store three different...
Computer Science - Java Programming How do you read a text file and store three different pieces of information in the database when the given text file contains this info.: 12345 Computer Science Bob Stone 23456 Art James G. Ocean? These are written in the format as ID Class Name. I was going to take the three different pieces of information by separating them by spaces, but the number of spaces is random and I don't know how to adjust...
An HTML file is a text file that contains text to be displayed in a browser...
An HTML file is a text file that contains text to be displayed in a browser and __________ to be interpreted (read) by the browser formatting and styling the document Both C++ and javascript are computer programing languages; what are their differences? What HTML tag can let you insert javascript in to document Which attributes of the <script> tag tells the brorwser what kind of scripting language is insterted? (give an example) in the javascript section of the HTML file,...
Using Node.js, show how you would read a file and and write out its contents to...
Using Node.js, show how you would read a file and and write out its contents to a new file in reverse order. As an example, if the source file had: Roses are red, Violets are blue. If the Trump administration continues to lose cabinet members, I will not be sad. Boo hoo hoo. The destination file would have: I will not be sad. Boo hoo hoo. If the Trump administration continues to lose cabinet members, Violets are blue. Roses are...
How to read and print all contents in a binary file using a Binary Search Tree...
How to read and print all contents in a binary file using a Binary Search Tree inorder traversal in C. Additionally, how to search using a Binary Search Tree to display the specific Athlete and his/her information. An example would be a sports record binary file that consist of name of athlete, height , weight, championships won. Athlete: Michael Jordan Height: 6’6” Weight : 205 lbs Championships won: 6 =================== Athlete: LeBron James Height: 6’8” Weight: 250 lbs Championships won:...
Write a program that will write the contents of a text file backwards one character at...
Write a program that will write the contents of a text file backwards one character at a time. Your program should first ask for the text file name to be processed – and after verifying that the file exists, it should read the file into a StringBuilder buffer. Save the new file using the old filename appended with “Ver2-“ at the front. So for the file “MyStuff.txt” the file would be saved as Ver2-MyStuff.txt”. You can use any text file...
In this lab, you will learn to read data from a sequential-access text file. To read...
In this lab, you will learn to read data from a sequential-access text file. To read data from a file, you need one FileStream object and one StreamReader object. The StreamReader object accepts the FileStream object as its argument. To read data from a sequential-access text file, here's what you need to do: From the desktop, open Visual Studio 2017. From the menu bar, navigate to File > New > Project. In the New Project window, select Windows Forms App...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT