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
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.
Need to check if File is successfully opened? C++ It works when I input the right...
I have a BST database that i have to export to a .CSV file. i can get it to export the first record. how can i get it to loop until the entire database is exported. void ActorBST::ExportToCSV(NodeActor *node) { string fileName; cout << "File Name(include .csv): "; cin >>fileName; ofstream myfile; myfile.open (fileName); myfile << "Year , Award, Winner, name, film"<< endl; myfile << node->year << "," << node->award << "," << node->winner << "," << node->name << ","...
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...
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 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...
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...
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 debugging this C++ prgram. What Am i doing wrong? //******************************************************** // This program reads two input files whose lines are //ordered by a key data field. This program should merge //these two files, writing an output file that contains //all lines from both files ordered by the same key field. // //********************************************************* #include <iostream> #include<string> #include<fstream> //prototype void mergeTwoFiles (ifstream&,ifstream&, ofstream&); using namespace std; int main() {string inFile1,inFile2,outFile; // input and output files ifstream in1; ifstream in2;...
QUESTION 6 To determine whether a file was opened successfully, the program can use the fstream function: Hint: google fail() open() close() eof() 10 points QUESTION 7 Which of the following are correct ways to end a loop using a test for end-of-file? (Two correct answers) inStream.get(next); while(!inStream.eof( )) { cout << next; inStream.get(next); } while(inStream->next) { cout << next; } inStream.get(next) while(!eof(inStream)) { cout << next; inStream.get(next); } while(inStream >> next) cout << next; 10 points QUESTION...
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;
Having issues using binary search on a pointer array. 1. Write 1000 random ints to file 2. Binary Search to find if number exists in element from file. int main() { ArrayActions action; int count; int userInput; int num = 1000; int array[num]; ofstream myFile ("/Users/chan/Desktop/LANEY_CIS27/Assignemtn2_CIS27/Assignemtn2_CIS27/File.txt"); //Set srand with time to generate unique random numbers srand((unsigned)time(0)); //Format random num up to 999 for(count = 0; count < num; count++) { array[count] = rand() % 1000; } //Condition...