Input validation of opening a text file in C++ - c++

I am building an input validation function that takes the input of the user and tries to open that file. and repeats if user is not entering the correct format. the correct format is:
test1.txt
My function works if I write correct format in the first run, but after the second run it keeps printing the error message although I am writing the write format to be opened. I have tried to clear the input "cin" and "filename" after taking the input but it did not work. Any ideas ?
string getFileInput()
{
string filename;
fstream file;
cout << "Please enter the name of the file: ";
getline(cin, filename);
file.open(filename.c_str());
while(!file.is_open())
{
file.clear(); file.ignore();
cout << "File name is incorrect, please enter again: ";
cin.clear(); cin.ignore();
getline(cin, filename);
file.open(filename.c_str());
}
// Extra condition. Empty file
if (file.eof())
{
cout << filename << " is an empty file." << endl;
}
file.close();
return filename;
}

I could reproduce and fix.
The problem is caused by cin.ignore(). According to cppreference:
... the next available character c in the input sequence is delim
So ignore will read the next line, up to the newline, and leave that newline alone. And the following getline can only read an empty string!
By the way, using a non opened fstream (file) for ignore and clear is at least useless and could be harmlfull because those methods are expected to be called on an open stream. And using cin.clear() is useless too and can be harmfull: if for any reason you have a read error (because you reached an end of file for example), you will consistently clear the error condition and try to read again when you should abort.
Finally, the eof condition is only set after a read returned nothing because of the end of file. It is never set when opening an empty file, nor if you could successfully read up to the end of file.
So the function should boil down to:
string getFileInput()
{
string filename;
fstream file;
cout << "Please enter the name of the file: ";
getline(cin, filename);
if (! cin) {
// test the error immediately and before using filename!
cerr << "read error: aborting...\n";
return "";
}
file.open(filename.c_str());
while(!file.is_open())
{
cout << "File name is incorrect, please enter again: ";
getline(cin, filename);
if (! cin) {
// test the error immediately and before using filename!
cerr << "read error: aborting...\n";
return "";
}
file.open(filename.c_str());
}
file.close();
return filename;
}

Related

File stream with repeating input

I'm attempting to create a repeating menu that will allow a user to re-enter a file name if the program is unable to open the file.
Right now it works correctly if I enter the name of an existing file, but if the file doesn't exist it prints the "File not found" then executes the rest of the program. I'm new to file streams and most of the code here was found through references. I'm a bit lost on what exactly is going on and what the best way to handle the situation is. Any guidance would be appreciated.
typedef istream_iterator<char> istream_iterator;
string fileName;
ifstream file;
do {
cout << "Please enter the name of the input file:" << endl;
cin >> fileName;
ifstream file(fileName.c_str());
if (!file) {
cout << "File not found" << endl;
}
} while (!file);
std::copy(istream_iterator(file), istream_iterator(), back_inserter(codeInput));
After constructing the object file will always exist, so your loop condition always fails. Change the condition to whether the file didn't open properly.
do {
...
}
while (!file.is_open())
this code will work.
do {
std::cout << "Please enter the name of the input file:" << std::endl;
std::cin >> fileName;
file = std::ifstream(fileName.c_str());
if (!file) {
std::cout << "File not found" << std::endl;
}
} while (!file);
your error was that you have 2 definition of the file variable.
the variable in while (!file) that is used is the one defined outside the do-while loop, and it is valid state is set to true by default.
In addition to #acraig5075 answer:
Writing a type then a variable name (ifstream file) is to create a new variable. Obviously you know this, but if you use the same name again in, for example, a loop, it makes a new and distinct variable.
ifstream file; // a unique variable
...
do {
...
ifstream file(fileName.c_str()); // another unique variable
...so change the usage inside the loop to:
file.open(fileName.c_str());

ifstream is.open() acting like it's not reading the file

//Prompts user for a file name and stores it
string fileName;
cout << "Enter the file name: ";
cin >> fileName;
ifstream inFile (fileName);
inFile.open(fileName);
//Prompt the user until they give the name of a file that can be opened
bool validFileName = false;
while(validFileName == false)
{
if(inFile.is_open())
{
validFileName = true;
}
else
{
cout << "Please enter a valid file name: ";
cin >> fileName;
ifstream inFile;
inFile.open(fileName);
}
}
//this block prints to the terminal, so it's opening
if(inFile.is_open())
{ cout << "It works! \n"; }
I am trying to create a program that will work with a file, but there needs to be a section that checks to see if the file that the user types in is an actual file that the program can open. I've tried a few different ways to write the while loop, because it needs to keep asking until it receives a valid file. I have the valid file name "input.txt", but even when I type that into the terminal it continues to print the error message. I have tried to type the file name with and without quotes, so I'm not sure what it is caught up on. I know it is opening the file, because I added a second check afterward and it appears that it's opening, so I think it's an issue with how I have the error check statement written?
The problem is that you are using
ifstream inFile;
inFile.open(fileName);
in the loop. The variable in the loop hides the variable of the same name outside the loop. Remove the first of those lines.
FWIW, you can simplify your code to:
ifstream inFile (fileName);
while(!inFile)
{
// Prompt the user until they give the name of a file that can be opened
cout << "Please enter a valid file name: ";
cin >> fileName;
inFile.open(fileName);
}
if(inFile)
{
cout << "It works! \n";
}
In:
ifstream inFile (fileName);
inFile.open(fileName);
The file is opened on the first line.
Reopening it is redundant.
Also in the loop you declare a temporary variable inFile which goes out of stop at the end of the else statement. Make sure to declare it only once at the outmost scope you would like to use it.

Odd behavior in C++ program

I am writing a program that I allow the user to specify an input file to open and when I test with incorrect file names, the program is behaving very weird and it seems to have something to do with the input buffer, but I don't know where to begin other than using getline() instead of cin >> but I have already tried that.
here is the code that I think may be the problem:
bool openfile(ifstream&);
string userInput();
int main()
{
// ...
while (!openfile(inputFile))
openfile(inputFile);
string input = userInput();
// ...
}
bool openfile(ifstream &inputFile)
{
string filename;
cout << "Please enter the name of the file or type quit to exit the program: ";
cin >> filename;
cout << endl;
if (filename == "quit")
exit(4);
else
inputFile.open(filename);
if (!inputFile)
{
cout << "The file \"" << filename << "\" could not be opened or does not exist.\n";
return false;
}
return true;
}
string userInput()
{
string englishSentence;
cout << "Please enter a sentence or type quit to exit the program: \n";
getline(cin, englishSentence);
if (englishSentence == "quit")
exit(4);
return englishSentence;
}
Those are the two functions that read any input. openfile() is called first as you can see. Any help is greatly appreciated. Let me know if you suspect something else in my code and I will paste it.
while (!openfile(inputFile))
openfile(inputFile);
What this does is attempt to open the file twice each iteration, as long as the first attempt fails. Also, you need to make sure that inputFile is closed before attempting to open it again since it appears that you're reusing the same file object repeatedly.
Certainly first try something like:
while (!openfile(inputFile))
;
You can just do:
while (!openfile(inputFile));
Since the way you have it it would request an input filename twice if it fails the first time.
Basically to outline the problem:
Begin Loop
Request Filename
Invalid Filename
Request Replacement Filename
Return to start of the loop (Checks again)
Some problems I see from your code:
int main(); { ... } does not define the main function. You need to drop the semicolon, or it won't even compile.
while (!openfile(inputFile)) openfile(inputFile); repeats openfile(inputFile) unnecessarily. If the first one (in the condition) fails and the second one (in the body) succeeds, a third call will be made (in the condition) to check if the loop should continue. What you probably want is just while (!openfile(inputFile)) { }.
You open a file in openfile and never use it in the subsequent userInput.

remove stop words from a text file

I'm still a beginner to programming, I got an assignment asks me to make a code that read a text file and then remove the stop words. I made a code but it's not that good. what i want is to know how to remove a word from a line and apply case folding to the file after removing the stop words.
here is my code...
string line, deleteline;
ifstream stopword;
stopword.open("example.txt");
if (stopword.is_open())
{
while ( getline (stopword,line) )
{
cout << line << endl;
}
stopword.close();
}
else cout << "Unable to open file";
ofstream temp;
temp.open("temp.txt");
cout << "Please input the stop-word you want to delete..\n ";
cin >> deleteline;
while (getline(stopword,line))
{
if (line != deleteline)
{
temp << line << endl;
}
}
temp.close();
stopword.close();
remove("example.txt");
rename("temp.txt","example.txt");
cout <<endl<<endl<<endl;
system("pause");
return 0;
}
The code would be right, but you closed the file... and after closing, you're trying to read from it later. That won't quite work, unless you open it again.
Here you close the file (7th line in main):
stopword.close();
And then, in the 2nd while you try to getline from that stream:
while (getline(stopword,line))
In order for that to work, you have to open the file again. And clear the stream, because it'll probably have the eofbit error state flag set.

Segfault during file read

The program is supposed to prompt the user for their username. Upon receiving the username it concatenates it with '.history' to create username.history. Then it opens that file (username.history) and reads the input from it. I am running into a segfault here though. Whenever it opens the file, which is empty because the file doesn't exist, it reads multiple lines and then throws the segfault. I think the problem might stem from how I'm trying to open the file, but I'm not sure. Here is the portion that is causing problems:
// File input and output
ifstream f_in;
ofstream f_out;
// Prompt user for their username.
char username[80];
cout << "Please input your username: " << endl;
cin >> username;
cout << endl;
cout << "Loading history file if it exists." << endl;
// Create file naem and initialize the file line counter to 0.
strcat(username, ".history");
int fcount = 0;
// Open file and read in lines if there are any.
// Place read lines into the command string for use later.
char tmp[50];
f_in.open(username);
while(!f_in.eof()){
f_in >> tmp;
cmd[fcount] = tmp;
fcount++;
}
f_in.close();
Other pertinent info:
cmd is declared as a global variable (char cmd[200][50])
Any help will be greatly appreaciated.
Not sure if it is the only issue, but cmd[fcount] = tmp is wrong. You should use strcpy().
while(f_in.good())
{
f_in >> tmp;
cmd[fcount] = tmp;
fcount++;
}