Question

Wrtie a C++ program to read and display the test data entered from keyboard and stored...

Wrtie a C++ program to read and display the test data entered from keyboard and stored in the infor.dat file.

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

Code:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(){
    
   fstream newfile;
   newfile.open("infor.dat",ios::out);  // open a file to perform write operation using file object
   
   if(newfile.is_open()) //checking whether the file is open
   {
      string x;
      
      cout<<"Enter test data: ";
      getline(cin,x); // taking input from keyboard
      
      newfile<<x<<"\n";   //inserting text
      newfile.close();    //close the file object
   }
   
   cout<<"\nNow reading from the file: \n\n";
   
   newfile.open("infor.dat",ios::in); //open a file to perform read operation using file object
   
   if (newfile.is_open()){   //checking whether the file is open
      string tp;
      while(getline(newfile, tp)){ //read data from file object and put it into string.
         cout << tp << "\n"; //print the data of the string
      }
      newfile.close(); //close the file object.
   }
}

Output:

infor.dat at the beginning of execution of the program.

Run Debug Stop Share A Save {} Beautify Language C++ main.cpp infor.dat < input Writing to the file Enter test data: Is • GDB

program execution:

23 24 newfile.open(infor.dat, ios::in); //open a file to perform read input Enter test data: This is test data hello 123 No

infor.dat at the end of execution of the program.

main.cpp infor.dat 1 This is test data hello 123 2 input Enter test data: This is test data hello 123 Now reading from the fi

1594947114697_image.png

Add a comment
Know the answer?
Add Answer to:
Wrtie a C++ program to read and display the test data entered from keyboard and stored...
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 a C program to read in a string from the keyboard and display the whole...

    write a C program to read in a string from the keyboard and display the whole string backward

  • In C, write a program to read 5 student scores from keyboard the average of test...

    In C, write a program to read 5 student scores from keyboard the average of test score. Use for loop. Instead of entering the 5 scores each time you run the program, you can use i/o redirect. Create an input file inputfor.txt, then add 5 scores in it. Run the command like this: scorefor < inputfor.txt

  • C language 2. Write a program to read in a string from the keyboard and only...

    C language 2. Write a program to read in a string from the keyboard and only display the vowels and their positions index) in a table format. Enter a string: This is a test Here are the vowels and their positions:

  • Write a program in C++ that will read a sentence from the keyboard, terminated with a...

    Write a program in C++ that will read a sentence from the keyboard, terminated with a period '.' and write the sentence to an output file with all white spaces replaced by the symbol '*'. You may use the “isspace()” function of the “cctype” library to carry out the determination of whether the character read is 'white space'. Show any #include files you may need. Make sure to handle conditions where the file may be missing so your program does...

  • Create a program (Lab9_Act1_Write.py) that will read in data from the keyboard and store it in...

    Create a program (Lab9_Act1_Write.py) that will read in data from the keyboard and store it in a file. Your program should prompt the user for the name of the file to use and then request values be entered until the user is done. This data should be written to two files: a text file, user_file_name.txt and a comma separated value file, user_file_name.csv. To help you with decision making, we suggest that you ask the user to input data representing students’...

  • Program Description: A C# app is to be created to produce Morse code. The Morse code...

    Program Description: A C# app is to be created to produce Morse code. The Morse code assigns a series of dots and dashes to each letter of the alphabet, each digit, and a few special characters (such as period, comma, colon, and semicolon). In sound-oriented systems, the dot represents a short sound and the dash represents a long sound. Separation between words is indicated by a space, or, quite simply, the absence of a dot or dash. In a sound-oriented...

  • This Java program reads an integer from a keyboard and prints it out with other comments....

    This Java program reads an integer from a keyboard and prints it out with other comments. Modify the comments at the top of the program to reflect your personal information. Submit Assignment1.java for Assignment #1 using Gradescope->Assignemnt1 on canvas.asu.edu site. You will see that the program has a problem with our submission. Your program is tested with 4 testcases (4 sets of input and output files). In order to pass all test cases, your program needs to produce the same...

  • Write a program to read through the data stored in a file and when you find...

    Write a program to read through the data stored in a file and when you find line that starts with “From”, split the line into words using the split function. We are interested in who sent the message, which is the second word on the From line. IN PYTHON

  • Concepts: Pointers and pointer arrays Write a program to read in a phrase from the keyboard,...

    Concepts: Pointers and pointer arrays Write a program to read in a phrase from the keyboard, re-display the text, and display a message on the following line giving the text's status as a palindrome. Do not use any arrays or subscripting in this program, except for arrays of pointers. A palindrome is a word or phrase which is the same backwards as forwards; for example "civic", "toot", and "radar" are palindrome words; each word is spelled the same left to...

  • Program Description: A Java program is to be created to produce Morse code. The Morse code...

    Program Description: A Java program is to be created to produce Morse code. The Morse code assigns a series of dots and dashes to each letter of the alphabet, each digit, and a few special characters (such as period, comma, colon, and semicolon). In sound-oriented systems, the dot represents a short sound and the dash represents a long sound. Separation between words is indicated by a space, or, quite simply, the absence of a dot or dash. In a sound-oriented...

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