I am trying to get this code to get the user to correctly enter the ifstream file by using the if(infile.fail()) statement. it catches the failure, but when I tried to loop it back to have the user try again it gets stuck in an infinite loop. here are a couple of my options I have tried. one I just exit the program, and have to manually restart it, but that is inefficient (it works though). The other catches the failulure but then I can't get it not to get caught in an infinite loop.
1 2 3 4 5 6 7 8 9
cout << "Enter the name of the file: \n";
cin >> filein;
infile.open (filein,ios::binary);
if (infile.fail())
do
{
cout << "Please enter a file to encrypt: ";
cin >> filein;
} while (infile.fail() );
1 2 3 4 5 6 7 8 9
cout << "Enter the name of the file: \n";
cin >> filein;
infile.open (filein,ios::binary);
if (infile.fail( ))
{
cout<<"Input file opening failed. \n";
system("PAUSE");
exit(1);
}
The second works but is not what I am trying to accomplish. I'm sure there is something better than a do while loop for this, but I am not sure which to use. Can I do something like getline(cin,infile)? or can I use some other kind of pointer? Any suggestions or help would be fantstic!
I am using the ios::binary to open the file in binary mode so I can encrypt it using XOR. If I don't open the text file in binary it doesn't convert back correctly in XOR because of the ASCII traits. Should I move the .open command to the outside? like so:
1 2 3 4 5 6 7 8 9 10 11
cout << "Enter the name of the file: \n";
cin >> filein;
if (infile.fail())
{
do
{
cout << "Please enter a file to encrypt: ";
cin >> filein;
} while (infile.fail() );
}
infile.open (filein,ios::binary);
Nope, that doesn't work. If I move the .open command the the (infile.fail()) has nothing to catch. I don't know why I didn't see that to start. Any other ideas on how to get back to the ciin line?
std::ifstream infile;
char* filein;
std::cout << "Enter the name of the file: \n";
std::cin >> filein;
infile.open (filein, std::ios::binary);
while (infile.fail( ))
{
std::cout << "Input file opening failed. Re-enter the name of the file: \n";
std::cin >> filein;
infile.open (filein, std::ios::binary);
}
std::cout << "Input file opening is a success. \n";
I put your code in, maybe I did something wrong but it isn't working for me. this is my entire code. it takes a text file and XORs it for encryption, yes this is my assignment. I have been working on this and had it working until i tried to get this portion to work without having to restart the program. I am new in C++ and this is kicking my butt.
- I can input a wrong name and it request for another file name until I provide a good one.
- Then it ask for an encryption key
- Next it ask for an output file name without problem even if I give a name of an existing file.
- And finally, I can quit the console without any error, and the output file if effectively encrypted.
I'm using Bloodshed as my compiler, I wonder if that is my problem? Unfortunately that is what I have to use. Thanks for all your help, I might just go back to quiting the program and take the points hit on the grade. This has been driving me nuts!
it is an infinite loop. If I put in the wrong filename it asks for the user to give the correct name over and over. even if I put in the right filename.
Humm... from what I see (It's 6:20 am here and I'm still working through, so I'm not fully awake ^^) there is no reason to have an infinite loop. (You could add a counter an exit after X fail, but this doesn't fix your problem)
Excuse this (bad) question but do you enter the file name with its dot extension ? ie : "myfile.h" and not "myfile" ? Sometime errors are just an oversight (sorry if the words are not accurate).