Question

You are writing a program in C++. Give the necessary statements to open a file and...

You are writing a program in C++. Give the necessary statements to open a file and to confirm that the file has been successfully opened for writing.

Homework Answers

Answer #1

Code:
#include <iostream>
#include <stdio.h>
#include <fstream>

using namespace std;

int main()
{   //creating object of class ofstream
   ofstream writeFile;
   //opening the file named "file.txt" with object's function open()
   writeFile.open("file.txt");
  
   //checking whether file is opened or not using is_open() function
   //if file is opened, it returns 1 else 0
   //if file is opened, displaying a message
   if(writeFile.is_open())
   {
       cout<<"File has been successfully opened for writing\n";
   }  
   //closing the file
   writeFile.close();
  
   return 0;
}

Code Photo:
Output:

Know the answer?
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
Similar Questions
Writing a program in Python that reads a text file and organizes the words in the...
Writing a program in Python that reads a text file and organizes the words in the file into a list without repeating words and in all lowercase. Here is what I have #This program takes a user input file name and returns each word in a list #and how many different words are in the program. while True:   #While loop to loop program     words = 0     #list1 = ['Programmers','add','an','operating','system','and','set','of','applications','to','the','hardware',          # 'we','end','up','with','a','Personal','Digital','Assistant','that','is','quite','helpful','capable',           #'helping','us','do','many','different','things']        try:        ...
C++. This program can open a text file and print its contents to the screen. For...
C++. This program can open a text file and print its contents to the screen. For example, it could load the output.txt file generated in the previous program and display on the screen the text contained in the file.
C++ Xcode (mac) This program can open a text file and print its contents to the...
C++ Xcode (mac) This program can open a text file and print its contents to the screen.For example, it could load the output.txt file generated in the previous program and display on the screen the textcontained in the file.
C# Reading from Files Write a program to open a text file containing information about buildings....
C# Reading from Files Write a program to open a text file containing information about buildings. The program must display all the buildings in the file and then find the lowest cost building based on the cost per square foot. Format for one building: <building name> <size> sqft $<cost> Example: Allgood Hall 35000 sqft $8,250,099.75 Create the text file and add three or more of your favorite buildings.
Create a program that filters the data in a CSV file of product data based on...
Create a program that filters the data in a CSV file of product data based on some search word and prints the resulting output to a new file. Additionally, the program will print the number of items filtered to stdout. • Your program should take three arguments: an input file to process, an output file to save the results, and a search word. • If the output file already exists or cannot be opened, warn the user by printing "CANNOT...
Code a C file, following these instructions: This lab is about getting the input from a...
Code a C file, following these instructions: This lab is about getting the input from a file. Let’s assume the words are provided in one file, passed as an argument to your program. The names of the files are provided as arguments to your program (i.e., they are copied by the shell in the argv variable), and each file is a text file with lots of words. Open the manual page for open() system call and add to your code...
You will write a program that loops until the user selects 0 to exit. In the...
You will write a program that loops until the user selects 0 to exit. In the loop the user interactively selects a menu choice to compress or decompress a file. There are three menu options: Option 0: allows the user to exit the program. Option 1: allows the user to compress the specified input file and store the result in an output file. Option 2: allows the user to decompress the specified input file and store the result in an...
C++ create a program that: in main: -opens the file provided for input (this file is...
C++ create a program that: in main: -opens the file provided for input (this file is titled 'Lab_HW10_Input.txt' and simply has 1-10, with each number on a new line for 10 lines total) -calls a function to determine how many lines are in the file -creates an array of the proper size -calls a function to read the file and populate the array -calls a function to write out the contents of the array in reverse order *output file should...
The following code to run as the described program on python. The extra test file isn't...
The following code to run as the described program on python. The extra test file isn't included here, assume it is a text file named "TXT" with a series of numbers for this purpose. In this lab you will need to take a test file Your program must include: Write a python program to open the test file provided. You will read in the numbers contained in the file and provide a total for the numbers as well as the...
Lab 6    -   Program #2   -   Write one number to a text file. Use the write()...
Lab 6    -   Program #2   -   Write one number to a text file. Use the write() and read() functions with binary                                                        data, where the data is not char type.              (Typecasting is required) Fill in the blanks, then enter the code and run the program. Note:   The data is int type, so typecasting is            required in the write() and read() functions. #include <iostream> #include <fstream> using namespace std; int main() {    const int SIZE = 10;   ...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT