473,804 Members | 1,971 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

can't open file in constructor

254 Contributor
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
4 2095
nickyeng
254 Contributor
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
horace1
1,510 Recognized Expert Top Contributor
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
nickyeng
254 Contributor
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
nickyeng
254 Contributor
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.tx t", 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

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

Similar topics

6
9745
by: moonriver | last post by:
I write a program accessing files in network drive o:. It is doable as a standalone application. However, if it is running under windows service, the following exception will appear: 13/07/2004 10:24:48 AM run() error: System.IO.IOException: The specified network password is not correct. at System.IO.__Error.WinIOError(Int32 errorCode, String str) at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare...
4
6803
by: Otik | last post by:
Hi, I'm trying to open a file with read access only, e.g. FileStream fs = new FileStream("C:\foo.txt", FileMode.Open, FileAccess.Read); The file is open by another process and the FileStream constructor throws an IOException. I thought that it was possible to open a file with read access even if it's being used by another process. Is that not true? Thanks, Otik
19
1499
by: Eric Lilja | last post by:
Hello, I have a class that I want to be able to output detailed information about what it's doing either to a file or to the screen. What I first tried was adding a std::ostream-reference member variable to the class and then adding a constructor that, apart from the "normal" arguments, also took an std::ostream&. I then intended to set a boolean member variable called logging to true and have the other member functions check that...
8
1791
by: Peter A. Schott | last post by:
Been reading the docs saying that file should replace open in our code, but this doesn't seem to work: # Open file for writing, write something, close file MyFile = file("MyFile.txt", "w") MyFile.write("This is a test.") MyFile.close() However, using: MyFile = open("MyFile.txt", "w")
18
4362
by: Jen | last post by:
I'm using Microsoft's own VB.NET FTP Example: http://support.microsoft.com/default.aspx?scid=kb;en-us;832679 I can get the program to create directories, change directories, etc., but I can't get it to upload a file to the FTP server. I just get a "Cannot connect to remote server" error after this TRY: s = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
3
2734
by: Chi | last post by:
what is the "unable to write data to the transport connection" I use the oreilly , programming c# using System; using System.Net.Sockets; using System.Text; using System.IO; // get a file name from the client // open the file and send the // contents from the server to the client
6
4906
by: scottyman | last post by:
I can't make this script work properly. I've gone as far as I can with it and the rest is out of my ability. I can do some html editing but I'm lost in the Java world. The script at the bottom of the html page controls the form fields that are required. It doesn't function like it's supposed to and I can leave all the fields blank and it still submits the form. Also I can't get it to transfer the file in the upload section. The file name...
10
4284
by: Paul Cheetham | last post by:
Hi, I am developing an application that needs to store some machine-specific settings. The application is going to be published on the network in order to keep the clients on the latest version. Because of this, I am unable to store these settings in the App.Config file, as this gets updated every time the application does, and there doesn't appear to be a way of preventing this. Most of my application settings are kept in the...
20
5625
by: Author | last post by:
a .net 1.1 app has a class whose constructor opens a db connection to sql svr two thousand. this class has more than a dozen of methods. most of them don't do db stuff. I am wondering if this design is going to be a problem, bcoz each time this class is instantiated, a db conn is open. The worst thing is that I haven't seen anywhere in the code the db conn is closed. I write about this bcoz I see that this app leaves more than one...
0
9711
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9593
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10595
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10335
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10088
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9169
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6862
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5529
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3831
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.