stuck on loop for (infile.fail())

Mar 30, 2011 at 2:50am
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!
Last edited on Mar 30, 2011 at 3:22am
Mar 30, 2011 at 2:54am
I'm not very familiar with ifstream, but the
infile.open (filein,ios::binary);
shouldn't be called INTO the loop to avoid infinite loop ?
Mar 30, 2011 at 3:03am
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);
Mar 30, 2011 at 3:05am
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?
Mar 30, 2011 at 3:26am
This does work for me :

1
2
3
4
5
6
7
8
9
10
11
12
13

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";
Mar 30, 2011 at 3:39am
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.

!removed code!
Last edited on Mar 30, 2011 at 5:29pm
Mar 30, 2011 at 3:51am
Well your code does compile without error here.

- 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.
Last edited on Mar 30, 2011 at 3:52am
Mar 30, 2011 at 3:55am
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!
Mar 30, 2011 at 4:02am
I use GCC, and as I do not know Bloodshed I don't know it's particularities.

Do you have any special error report ? or this is still an infinite loop error ?
Mar 30, 2011 at 4:09am
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.
Mar 30, 2011 at 4:24am
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).
Last edited on Mar 30, 2011 at 4:25am
Mar 30, 2011 at 4:40am
yeah, I have been using mydocument.txt. Don't worry, it is 11:40pm here and I have been staring at this most of the day and I'm tired.
Topic archived. No new replies allowed.