By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,592 Members | 1,977 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,592 IT Pros & Developers. It's quick & easy.

can't open file in constructor

100+
P: 254
i have main program, which user key in the filename in command line argument.
i pass the argument to another class constructor to handle it.
I have the filename correct, but it still give me "can't open terrain file" message.
here is my code:
Expand|Select|Wrap|Line Numbers
  1.                         Constructor(char* filename =""){
  2.                 ifstream terrainfile;
  3.  
  4.                 terrainfile.open(filename, ios::in);
  5.  
  6.                 if(terrainfile.is_open()){
  7.                     if(!terrainfile.eof()){
  8.  
  9.                         // bla bla bla 
  10.                     }
  11.                 }else{
  12.                     cout << "Can't open terrain file." << endl;
  13.                     exit(1);
  14.                     terrainfile.close();
  15.                 }
  16.                        }
  17.  
Dec 16 '06 #1
Share this Question
Share on Google+
4 Replies


100+
P: 254
i called the class like this and pass the argument:

Expand|Select|Wrap|Line Numbers
  1. Constructor co(argv[1]);      // argv[1] is command line argument 
  2.  
Dec 16 '06 #2

Expert 100+
P: 1,510
i called the class like this and pass the argument:

Expand|Select|Wrap|Line Numbers
  1. Constructor co(argv[1]);      // argv[1] is command line argument 
  2.  
code looks OK - print value of filename in constructor to check it is OK
are you sure you got the absolute or relative path to the file correct
Dec 16 '06 #3

100+
P: 254
code looks OK - print value of filename in constructor to check it is OK
are you sure you got the absolute or relative path to the file correct
the path to the file is correct.

If i move away the ELSE part, it will print the content of the file.
But i have to put the else part because i want when user type the wrong filename it will give a error message.

Once i put the else part, it will cout the error message,
Once i move away else part, it cout the content of that file well.

so weird...

Thanks for helping.
Nick
Dec 16 '06 #4

100+
P: 254
Now i try it this way:
i coded the command line argument comparison first in main program, if it command line argument is equal to "terrain.txt", then error message, else it will continue the next coding line and then go to class constructor by taking command line argument. In post 1, it shown.
here is the code:
Expand|Select|Wrap|Line Numbers
  1. int main(int argc, char* argv[]){
  2.  
  3.         switch(argc){
  4.             case 1:
  5.                 cout << "Usage: game terrain file" << endl;
  6.                 exit(1);
  7.                 break;
  8.             case 2:
  9.                 if(argv[1] != "terrain.txt"){
  10.                     cout << "Can't open terrain file" << endl;
  11.                     exit(1);
  12.                 }
  13.                 break;
  14.         }
  15.                   //.....
  16.                  //....Constructor cons(argv[1]);
  17.                  //....
  18. }
  19.  
Dec 16 '06 #5

Post your reply

Sign in to post your reply or Sign up for a free account.