![]() |
|
#1
|
|||
|
|||
Problem opening a fileThis one is killing me. Seems so easy. My assignment includes this otherwise small detail of opening a text file called LAB5.IN . I am using #include <fstream>. The file opens fine with a .TXT extension but change the extension to .IN and the assert statement shuts down the program since it is not opening. I'd just as soon change the file to .TXT extension and be over with it but since he specifically calls for LAB5.IN, I don't want to deviate. Can you tell me where I am going wrong?
The section of code in question follows: CPP / C++ / C Code:
|
|
#2
|
|||
|
|||
Re: Problem opening a fileQuote:
Make a little test program that just tries to open the files. Make the test program show you what is in that directory. Don't use that ugly "assert" stuff, since it doesn't allow any kind of recovery or other civilized behavior. If your instructor requires assert in your project, then by all means, don't argue just do it. However: see Footnote. CPP / C++ / C Code:
Here's a run when I had LAB5.TXT but not LAB5.IN: Code:
Here's a run when I put in LAB5.IN: Code:
Notes: 1. Use of the system("dir ...") function for debugging is more convenient than writing all of the implementation-dependent stuff for determining whether a file with a given name is in a given directory. If there are any knee-jerk anti-system() wonks out there: You are entitled to your opinion, and I respectfully suggest that if you don't want to use system(), then, by all means don't use system(). If you want to tell me not to use system(), then you are perfectly free to express your opinion, but you should realize that I am perfectly free to ignore it. If you do decide to object, and If I decide to respond to your objection(s), I'll try not to be rude. 2. The system() function is part of the C Standard Library and can be used with any standard C compiler. However... what happens when you call system() depends on what is in the parentheses. You are obviously using a Windows operating system, so the command "dir" is present. However, for my cygwin GNU compiler, system("dir c:\\temp"); doesn't work. If I change it to system("c:/temp") it gives a "short form" directory listing (like using "ls" on a Linux system). 3. Even with Borland and Microsoft compilers it isn't really necessary to use th e backslash as a path separator. The following lines work just fine on all Borland and Microsoft compilers that I have. CPP / C++ / C Code:
Note that things like directories and paths are not in any way specified by the C or C++ language standards. Such things are implementation-dependent. (Different operating systems and different compiler vendors can do it any way that they darn will please.) By the way, use of '/' path separator in programs compiled by Microsoft compilers is documented on msdn. You can look it up. If your instructor requires the "\\" stuff, then by all means, don't argue; just do it. I just thought I would throw it out there for your consideration. Regards, Dave Footnote about using "assert" in response to invalid user input conditions: In a "real" program (or even a development program), I think that having a program bomb out with no possible recovery or other civilized behavior (like, maybe, describing exactly what is the problem) is inexcusable. I mean, really, if a program requires a certain file to be present, what do you think paying customers would do if they ran the program without the required file and were presented with a dialog box that says something like "ThisVeryExpensiveProgram.exe has encountered a problem and needs to close. We are sorry for the inconvenience." and then goes on to say things like "Please tell Microsoft about this problem..." and gives a button to click so that you can send an error report to Microsoft? What would you do? Would you be happy with the program (for which you paid your good money) for suggesting that you notify Microsoft? (And, by the way, that's probably why Microsoft ignores all of those error reports anyhow. I mean, after all: what the heck is Microsoft supposed to do with such things.) At the very least I would print a (polite) message telling the user the exact nature of the problem and exit() the program. But maybe that's just me. I'm funny that way. |
|
#3
|
|||
|
|||
Re: Problem opening a fileThanks Dave! With your test program, I figured out that I was not saving the text file correctly (it was ending up as .IN.txt). Although I feel a little embarrassed to have not figured out the problem myself, that lesson will probably save me a great deal of time some day in the future.
Now, onto the meat of this baby - the binary search tree. Thanks again, Paul |
Recent GIDBlog
2nd Week of IA Training by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Airport Log program using 3D linked List : problem reading from file | batrsau | C Programming Language | 11 | 29-Feb-2008 07:44 |
| File opening problem | Tomb332 | CPP / C++ Forum | 6 | 30-Jul-2006 13:53 |
| After execution - Error cannot locate /Skin File? | WSCH | CPP / C++ Forum | 1 | 05-Mar-2005 20:03 |
| Yet another CD burner problem: Lite-On LSC-24082K | Erwin | Computer Hardware Forum | 1 | 22-May-2004 11:28 |
| CD Buring Failed | skanth2000 | Computer Hardware Forum | 1 | 15-Nov-2003 03:52 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The