Question

In: Computer Science

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

Solutions

Expert Solution

#include<string>
#include <iostream>
#include <fstream>
#include <cstdlib>

using namespace std;

bool fileOpen(string fileName, ofstream &out, bool mode){
   out.open(fileName, mode);
   if(out.is_open()){
       return true;
   }
   else{
       return false;
   }
}

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;
   if (fileOpen(file, myfile, ios::out))
   {
       cout << endl << "File successfully opened." << endl;
       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
   }
   else
   {
       cout << "Error opening file" << endl;
   }
}


Related Solutions

I need C++ program that Read an input file of text.txt one word at a time....
I need C++ program that Read an input file of text.txt one word at a time. The file should consist of about 500 words. The program should remove all punctuations,keep only words. Store the words in a built-in STL container, such as vector or map.Can someone help with any additional comments that I can understand the logic?thank you
****NEED CODED IN C++, READ THE INSTRUCTIONS CAREFULLY AND PAY ATTENTION TO THE INPUT FILE, IT...
****NEED CODED IN C++, READ THE INSTRUCTIONS CAREFULLY AND PAY ATTENTION TO THE INPUT FILE, IT IS REQUIRED FOR USE IN THE PROBLEM**** You are to generate a list of customers to serve based on the customer’s priority, i.e. create a priority queue/list for a local company. The company has been receiving request and the request are recorded in a file, in the order the request was made. The company processes each user based on their priority, the highest priority...
please check my answer if its right or need better answer. I need example of big...
please check my answer if its right or need better answer. I need example of big five personality traits. so please check my answer • Openness: the founder of x country he is open minded and every one can meet him. also he likes to travel new places and try new food • consciousnesses: like local astonaut called xxxx he is so immagination guy and organized because of his awarness and imagination he went to space to discover more and...
I need to update this java program to take input about each employee from a file...
I need to update this java program to take input about each employee from a file and write their information and salary/pay information to a file. use input file payroll.txt Kevin Yang 60 20 Trey Adams 30 15 Rick Johnson 45 10 Cynthia Wheeler 55 11.50 Sarah Davis 24 10 Muhammad Rabish 66 12 Dale Allen 11 18 Andrew Jimenez 80 15 import java.util.Scanner; public class SalaryCalcLoop { double Rpay = 0, Opay = 0; void calPay(double hours, double rate)...
Create a c++ program that: Create an input file using notepad ( .txt ). When testing...
Create a c++ program that: Create an input file using notepad ( .txt ). When testing your program using different input files, you must change the filename inside your program otherwise there will be syntax errors. There are a finite number of lines to be read from the data file. But we can’t assume to know how many before the program executes; so, the standard tactic is to keep reading until you find the “End of File” marker. Input date...
A C PROGRAM *Edit/Update I do not need the file loading script, but I am not...
A C PROGRAM *Edit/Update I do not need the file loading script, but I am not against it being included in the answer* I must read off of an excel file (comma separated) to input five different things for a book, all comma separated, there are 360 books in the file. The book title, author, ISBN number, number of pages, and finally the year it was published. Now some of the ISBN or Pg numbers may be missing and will...
Validating Input file, So for my C++ assignment, my professor says we need to validate the...
Validating Input file, So for my C++ assignment, my professor says we need to validate the Input File, Which means, if the line is missing a comma, one of the 3 information, or the string is a white line, it will ignore it. If the line has a white line, the program will correct it and will read in the line. I will store these into an Array, and display them in First name, Last name, and Number of Votes...
Write a C program, called reverse, using standard I/O functions, to take a file as input...
Write a C program, called reverse, using standard I/O functions, to take a file as input then copies it to another file in reverse order. That is, the last byte becomes the first, the byte just before the last one becomes the second, etc. The program call should look like: reverse fileIn fileOut
Program Language C++ How do I take lines of string from an input file, and implement...
Program Language C++ How do I take lines of string from an input file, and implement them into a stack using a double linked list? Example input, command.txt, and output file. Ex: Input.txt postfix: BAC-* prefix:+A*B/C-EF postfix:FE-C/B*A+ postfix:AB-C-D/ postfix:AB-CF*-D / E+ Command.txt printList printListBackwards Output.txt List: postfix:BAC-* prefix:+A*B/C-EF postfix:FE-C/B*A+ postfix:AB-C-D/ postfix:AB-CF*-D/E+ Reversed List: postfix:AB-CF*-D/E+ postfix:AB-C-D/ postfix:FE-C/B*A+ prefix:+A*B/C-EF postfix:BAC-*
C Programming: Write a program that accepts 2 arguments, an input file and an output file....
C Programming: Write a program that accepts 2 arguments, an input file and an output file. The program is to store in the output file the contents of the input file in reverse. If the input file looks like this: Hello World.\n This is Line 2\n This is the end.\n then the output file should look like this: \n .dne eht si sihT\n 2 eniL si sihT\n .dlroW olleH The main program should look like this: int main(int argc, char...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT