- Joined
- Jun 20, 2001
- Location
- Vancouver, WA
I'm trying to build into one of my programs the ability to save output to file. However, I'm having some problems making it work.
The code below is supposed to check to see if a file with the name entered exists, and then ask if the user would like to append the output (if not, the user must specify a new file). It detects if a file exists correctly, but does not loop through when the user specifies to not append.
Further checking shows that the problem (probably) lies with temp not being equal to the input key. If "n" or "N" is entered at the prompt, (temp=="n") and (temp=="N") both evaluate to FALSE. COUTing temp displays the key though, so I'm really confused... Any ideas?
JigPu
The code below is supposed to check to see if a file with the name entered exists, and then ask if the user would like to append the output (if not, the user must specify a new file). It detects if a file exists correctly, but does not loop through when the user specifies to not append.
Further checking shows that the problem (probably) lies with temp not being equal to the input key. If "n" or "N" is entered at the prompt, (temp=="n") and (temp=="N") both evaluate to FALSE. COUTing temp displays the key though, so I'm really confused... Any ideas?
Code:
//If File IO is needed, let's do some checking....
if ((PROGRAM.output==2) || (PROGRAM.output==3)) {
char *temp; //Temporary variable (used here to hold Y/N)
ifstream fout; //Assign a name for the stream
do {
cout << "Output File Name: "; //Ask for file name
cin >> PROGRAM.file; //And store into PROGRAM.file
fout.open(PROGRAM.file, ios::in); //And try opening the file
if (fout) { //Test for existance (and ask to append if exists)
cout << "Warning, output file exists! Append (y/n)? ";
cin >> temp;
}
} while ((fout) && ((temp=="n") || (temp=="N")));
JigPu