Question

Need to check if File is successfully opened? C++ It works when I input the right...

Need to check if File is successfully opened? C++

It works when I input the right txt file. But, it keeps stating, File successfully open." even I put in a non-existing file.

Here are the specifications:

Develop a functional flowchart and then write a C++ program to solve the following problem.

Create a text file named first.txt and write your first name in the file. You will be reading the name of the file from the keyboard as a string, using the string class. Your program will also read your first name from the keyboard. The process of the file creation (name of the file, mode for opening the file, and testing the open success is done in a user-defined function. The function communicates the status of file open to the calling function).

Here is what I have:

#include<string>

#include <iostream>

#include <fstream>

#include <cstdlib>

using namespace std;

int main() {

   string file; //This is a declaration for a file name

   string firstname;
   ofstream myfile;   

   cout << "Please enter the file name of your first name:";   
   cin >> file;   

   myfile.open(file);
  
   if (myfile.is_open())
   {
       cout << "\nFile successfully open.\n";
  
   }
   else
   {
       cout << "Error opening file";
   }

   cout << "Please enter your first name:"; //asks the user to input first name
   cin >> firstname; //user inputs first name

   myfile << firstname;
   myfile.close(); //closes the file

0 0
Add a comment Improve this question Transcribed image text
Request Professional Answer

Request Answer!

We need at least 10 more requests to produce the answer.

0 / 10 have requested this problem solution

The more requests, the faster the answer.

Request! (Login Required)


All students who have requested the answer will be notified once they are available.
Know the answer?
Add Answer to:
Need to check if File is successfully opened? C++ It works when I input the right...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Similar Homework Help Questions
  • The input file is already there. the file is too long, don't have to send it. using virtualbox ubuntu We want to cre...

    The input file is already there. the file is too long, don't have to send it. using virtualbox ubuntu We want to create a command line terminal where user can run the snap commands with three different options: -thanos, -ironman and -holk. The terminal must keep listening to user command. If the user type:  snap -thanos: all the text line in the text file “input.txt” that does not contain the word “thanos” must be randomly removed. The remaining text...

  • Can someone help me put the string removee the return to an outfile and fix?? prompt: CS 575 LabEx14: no e’s Let the user specify an input file and an output file (text files). Read the input file and...

    Can someone help me put the string removee the return to an outfile and fix?? prompt: CS 575 LabEx14: no e’s Let the user specify an input file and an output file (text files). Read the input file and write the output file; the output file should be the same as the input file, except that it has no e’s. For example, if the input file had the line. Streets meet in the deep. Then the output file would have...

  • I'm trying to get this program to give the user an option to enter a random...

    I'm trying to get this program to give the user an option to enter a random word and then have that word included at the end of each break in the story. I can get the option to ask for a word, but can't get the inputted word in the sentence to finish it. I believe my string story is wrong. Please help! //This program will ask a user to input three random words. //The words will be placed within...

  • Hello, I need to implement these small things in my C++ code. Thanks. 1- 10 characters...

    Hello, I need to implement these small things in my C++ code. Thanks. 1- 10 characters student first name, 10 characters middle name, 20 characters last name, 9 characters student ID, 3 characters age, in years (3 digits) 2- Open the input file. Check for successful open. If the open failed, display an error message and return with value 1. 3- Use a pointer array to manage all the created student variables.Assume that there will not be more than 99...

  • Hello I need a small fix in my program. I need to display the youngest student...

    Hello I need a small fix in my program. I need to display the youngest student and the average age of all of the students. It is not working Thanks. #include <iostream> #include <iomanip> #include <fstream> #include <vector> #include <algorithm> using namespace std; struct Student { string firstName; char middleName; string lastName; char collegeCode; int locCode; int seqCode; int age; }; struct sort_by_age { inline bool operator() (const Student& s1, const Student& s2) { return (s1.age < s2.age); // sort...

  • I need help on how to correctly read the file when it exists and show error...

    I need help on how to correctly read the file when it exists and show error when it doesn't read ifstream myfile("input.txt"); if (myfile.is_open()) while (getline » id_read >>x_read >>y_read) go till end of file original_list.push_back (new city(id_read, x_read, y_read, id_read, false)); ++added; myfile.close(); return added; else cout << "Unable to open file"; return e;

  • This assignment uses functions, files, and strings. Enough flexibility is provided for you to apply your...

    This assignment uses functions, files, and strings. Enough flexibility is provided for you to apply your knowledge of basic C++ programing to develop your solution. Develop a functional flowchart and then write a C++ program to solve the following problem. 1. Create a text file named file1.txt and write your brand of car (like Honda, Toyota, etc) in the file. You will be reading the name of the file from the keyboard as a string, using the string class. Your...

  • Write a simple telephone directory program in C++ that looks up phone numbers in a file...

    Write a simple telephone directory program in C++ that looks up phone numbers in a file containing a list of names and phone numbers. The user should be prompted to enter a first name and last name, and the program then outputs the corresponding number, or indicates that the name isn't in the directory. After each lookup, the program should ask the user whether they want to look up another number, and then either repeat the process or exit the...

  • Develop a functional flowchart and then write a C++ program to solve the following problem. 1....

    Develop a functional flowchart and then write a C++ program to solve the following problem. 1. Create a text file named c1.txt and write your brand of computer (like Dell, HP, etc) in the file. You will be reading the name of the file from the keyboard as a string, using the string class. Your program will also read the brand of your computer from the keyboard. The process of the file creation (name of the file, mode for opening...

  • IP requirements: Load an exam Take an exam Show exam results Quit Choice 1: No functionality...

    IP requirements: Load an exam Take an exam Show exam results Quit Choice 1: No functionality change. Load the exam based upon the user's prompt for an exam file. Choice 2: The program should display a single question at a time and prompt the user for an answer. Based upon the answer, it should track the score based upon a successful answer. Once a user answers the question, it should also display the correct answer with an appropriate message (e.g.,...

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