D
Daniel Moree
I'm working on a program that must first establish if the file exists in the
program directory then it must open if for reading, read each line and set
the variables then the program goes on about it's buisness.
My problem is all the resources I have found aren't very clear on these
things. All of them open the file then check to see if the stream is open.
Well, the problem with using file.open("filename.dat", ios::in |
ios::binary) is that if the file doesn't exists, it creates it
automatically. I need to see if the file exists, if it doesn't show an error
message, then if the file does exists, open if, read from it, then set the
global program variables. Below is my code so far to try to get this work.
I've marked where I needed help with // Need help here and many question
marks
Hopefully you guys will get a good view of what i'm doing, just a basic
console app in MSVC++ that pulls information in a binary file then if that
is successful allow you to set the variables to something else. Then run the
program again and view the information then set it to something different.
===== MAIN.CPP =====
#include <iostream.h>
#include <fstream.h>
#include <iomanip.h>
// variables
int Box_X;
int Box_Y;
int Box_Z;
float input_X;
float input_Y;
float input_Z;
char* filename = {"main.dat"};
// Functions
bool pullconfig();
bool setconfig();
// main function
int main(){
cout << "Pulling configuration information...\n";
if(pullconfig() == false){
cout << "\nError opening \"main.dat\" file: Does Not Exist!\n";
return 0;
}
cout << "Configuration pulled successfully!\n";
cout << "\nBox_X = " << Box_X << "; Box_Y = " << Box_Y << "; Box_Z = "
<< Box_Z << "\n";
cout << "Set Box_X to: ";
cin >> input_X;
cout << "Set Box_Y to: ";
cin >> input_Y;
cout << "Set Box_Z to: ";
cin >> input_Z;
if(setconfig() == false){
cout << "\nError setting configuration information!\n";
return 0;
}
cout << "\nNew Configuration Set Successfully!";
return 0;
}
bool pullconfig(){
ifstream fin(filename, ios::in | ios::binary);
if(!fin.is_open()){
return(false);
}
// Need Help here for pulling info and setting it to Box_X, Box_Y, and
Box_Z ????????????????????????????????
return(true);
}
bool setconfig(){
ofstream fout(filename, ios:ut | ios:binary);
if(!fout.is_open()){
return(false);
}
fout << "Box_X = " << input_X << "\n";
fout << "Box_Y = " << input_Y << "\n";
fout << "Box_Z = " << input_Z << "\n";
fout.close();
return(true);
}
program directory then it must open if for reading, read each line and set
the variables then the program goes on about it's buisness.
My problem is all the resources I have found aren't very clear on these
things. All of them open the file then check to see if the stream is open.
Well, the problem with using file.open("filename.dat", ios::in |
ios::binary) is that if the file doesn't exists, it creates it
automatically. I need to see if the file exists, if it doesn't show an error
message, then if the file does exists, open if, read from it, then set the
global program variables. Below is my code so far to try to get this work.
I've marked where I needed help with // Need help here and many question
marks
Hopefully you guys will get a good view of what i'm doing, just a basic
console app in MSVC++ that pulls information in a binary file then if that
is successful allow you to set the variables to something else. Then run the
program again and view the information then set it to something different.
===== MAIN.CPP =====
#include <iostream.h>
#include <fstream.h>
#include <iomanip.h>
// variables
int Box_X;
int Box_Y;
int Box_Z;
float input_X;
float input_Y;
float input_Z;
char* filename = {"main.dat"};
// Functions
bool pullconfig();
bool setconfig();
// main function
int main(){
cout << "Pulling configuration information...\n";
if(pullconfig() == false){
cout << "\nError opening \"main.dat\" file: Does Not Exist!\n";
return 0;
}
cout << "Configuration pulled successfully!\n";
cout << "\nBox_X = " << Box_X << "; Box_Y = " << Box_Y << "; Box_Z = "
<< Box_Z << "\n";
cout << "Set Box_X to: ";
cin >> input_X;
cout << "Set Box_Y to: ";
cin >> input_Y;
cout << "Set Box_Z to: ";
cin >> input_Z;
if(setconfig() == false){
cout << "\nError setting configuration information!\n";
return 0;
}
cout << "\nNew Configuration Set Successfully!";
return 0;
}
bool pullconfig(){
ifstream fin(filename, ios::in | ios::binary);
if(!fin.is_open()){
return(false);
}
// Need Help here for pulling info and setting it to Box_X, Box_Y, and
Box_Z ????????????????????????????????
return(true);
}
bool setconfig(){
ofstream fout(filename, ios:ut | ios:binary);
if(!fout.is_open()){
return(false);
}
fout << "Box_X = " << input_X << "\n";
fout << "Box_Y = " << input_Y << "\n";
fout << "Box_Z = " << input_Z << "\n";
fout.close();
return(true);
}