-
input/output
Hello everyone,
I'm trying to figure out how input/output files work. I got warnings, and it compiled, but when I tried to run the program, it gave me
input file failed
press any key to continue
I thought we are supposed to list the directory, and my input file is saved in the same folder, and the same directory, any suggestions??
Thanks a million!!
p.s. this is only the partial code.
Code:
int main()
{
string first_name; //declare string variable first name
string last_name; //declare string variable last name
string gender; //declare string variable gender
int age; //declare int variable age
string type_coverage; //declare string variable type of coverage
string club_member; //declare string variable health club
//member
string smoker; //declare string variable smoker
int zip_code; //declare int variable zip code
float basic_premium; //declare int variable basic premium
float discount_premium; //declare int variable discount premium
float premium; //declare int variable premium
ifstream inFile; //declare file streams
ofstream outFile;
inFile.open("C:\Windows\C++\proj4.in.txt"); //opens the input file
outFile.open("C:\Windows\C++\proj4.out.txt"); //opens the output file
if(!inFile)
{
cout << "Input file failed!" << endl;
return 1; //terminates the program if input fails
}
-
Code:
if (!inFile.is_open())
{
//terminate program
}
notice the .is_open() part
-
Hi, Waldo2k2,
Thanks for the suggestions.... however, I tried it and it gave me the same thing.... I think the program can't find the input file.....
I tried my last project in UNIX system without the "C:\\windows\C++" part and it works with the same structure.
Now, I'm trying to modify the same program but using the visual C++ so it is easier to debug, and I saved the input file in the same folder in the directory, but it would not work....... any more suggestions??
Thanks so much!!
-
oh wow, i missed the biggest mistake,
Quote:
Code:
inFile.open("C:\Windows\C++\proj4.in.txt"); //opens the input file
since you used unix before you used forward slashes for the path, in windows it's the backslashes, the problem is in c++ the backslash is used for character escape sequences. So, you need to change \ to \\. then it will work. I bet those are the warnings you're getting as well.
And by the way leave is_open() as it is, that is the correct way to do it. You can also save time by doing:
Code:
ifstream inFile("c:\\windows\\c++\\proj4.in.txt");
-
Hi Waldo2k2
Cool!!! It worked!!! and it made sense too... it is funny that you learned the mistakes that you made and you realized that
"duh! Didn't I know this before?!!" I guess that comes with experiences.....
Thanks so much again!!