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
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...
****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...
I made the command cp code in c language. But when I copy a file, file...
I made the command cp code in c language. But when I copy a file, file permissions are not copied equally. So I want to copy the file authority as well. What should I do? #include #include #include #include #include #include #include #include #include int main(int argc, char *argv[]) { struct stat st; char ch; int src, dst; if(argc != 3) { printf("argument error \n"); printf("usage: ./a.out src dest \n"); exit(0); } src = open(argv[1], O_RDONLY); if(src == -1){ perror("open...
I have a C problem. I need to read the data of a txt file to...
I have a C problem. I need to read the data of a txt file to array by struct, then use these data to sum or sort. The txt file and the struct like aa.txt 1 2 3 4 ***************************** struct aaa{ int num1,num2; }bbb[2]; num1 for 1,3, num2 for 2 4 I made a void readfile () function for read data. How can I pass the numbers to another function ( void sum () and void sort() ) for...
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)...
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 import java.util.Scanner; public class SalaryCalcLoop { double Rpay = 0, Opay = 0; void calPay(double hours, double rate) { if (hours <= 40) { Rpay = hours * rate; Opay = 0; } else { double Rhr, Ohr; Rhr = 40; Ohr = hours - Rhr; Rpay = Rhr *...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT