GIDForums  

Go Back   GIDForums > Computer Programming Forums > CPP / C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 17-May-2008, 02:10
pfanning pfanning is offline
Awaiting Email Confirmation
 
Join Date: Oct 2007
Posts: 39
pfanning is on a distinguished road

Problem opening a file


This 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:
template<class Entry>
void Binary_tree<Entry>::fill_tree()	
{
	datanode fileData;
	int fileKey;
	float fileGpa;

	ifstream infile;
	infile.open("c:\\temp\\LAB5.IN");
	assert (!infile.fail()); //stops program if file doesn't open

	while (infile>>fileKey)
	{
		infile>>fileKey;
		infile>>fileGpa;
		fileData.key = fileKey;
		fileData.gpa = fileGpa;
		insert(fileData);
	}
	infile.close(); // done with infile so we close it here
}
  #2  
Old 17-May-2008, 10:10
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,627
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Problem opening a file


Quote:
Originally Posted by pfanning
This one is killing me...
Since we can't look over your shoulder and see what is really going on with your system, I'll suggest a way for you to get a little farther along.

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:
#include <iostream>
#include <fstream>
//#include <cassert>

using namespace std;

int main()
{
    ifstream infile;
    cout << "Here's the directory listing for c:\\temp" << endl;
    system("dir c:\\temp");
    cout << endl << endl;

    infile.open("c:\\temp\\LAB5.TXT");
    //assert(!infile.fail());

    if (!infile) {
        cout << "Can't open c:\\temp\\LAB5.TXT for reading" << endl;
        cout << endl;
        infile.clear();
    }
    else {
        cout << "Opened c:\\temp\\LAB5.TXT for reading, now closing." 
             << endl << endl;
        infile.close();
    }

    infile.open("c:\\temp\\LAB5.IN");
    //assert(!infile.fail());
    
    if (!infile) {
        cout << "Can't open c:\\temp\\LAB5.IN  for reading" << endl << endl;
        infile.clear();
    }
    else {
        cout << "Opened c:\\temp\\LAB5.IN  for reading, now closing." 
             << endl << endl;
        infile.close();
    }
    return 0;
}

Here's a run when I had LAB5.TXT but not LAB5.IN:

Code:
Here's the directory listing for c:\temp Volume in drive C has no label. Volume Serial Number is xxxx-xxxx Directory of c:\temp 05/17/2008 08:43 AM <DIR> . 05/17/2008 08:43 AM <DIR> .. 03/17/2003 12:52 PM 15,296 EULA.txt 04/18/2007 07:48 AM 31 HELLO.BAT 05/17/2008 08:26 AM 0 LAB5.TXT 06/30/2007 06:41 AM 38 makefile.mak 05/02/2000 06:08 PM 4,890 readme.txt 03/13/2000 01:47 PM 384 simpleASX.asx 06/30/2007 06:42 AM 77 test.c 03/13/2000 01:47 PM 62,439 wmt.asf 8 File(s) 83,155 bytes 2 Dir(s) 9,867,735,040 bytes free Opened c:\temp\LAB5.TXT for reading, now closing. Can't open c:\temp\LAB5.IN for reading

Here's a run when I put in LAB5.IN:

Code:
Here's the directory listing for c:\temp Volume in drive C has no label. Volume Serial Number is xxxx-xxxx Directory of c:\temp 05/17/2008 08:47 AM <DIR> . 05/17/2008 08:47 AM <DIR> .. 03/17/2003 12:52 PM 15,296 EULA.txt 04/18/2007 07:48 AM 31 HELLO.BAT 05/17/2008 08:47 AM 0 LAB5.IN 05/17/2008 08:26 AM 0 LAB5.TXT 06/30/2007 06:41 AM 38 makefile.mak 05/02/2000 06:08 PM 4,890 readme.txt 03/13/2000 01:47 PM 384 simpleASX.asx 06/30/2007 06:42 AM 77 test.c 03/13/2000 01:47 PM 62,439 wmt.asf 9 File(s) 83,155 bytes 2 Dir(s) 9,867,730,944 bytes free Opened c:\temp\LAB5.TXT for reading, now closing. Opened c:\temp\LAB5.IN for reading, now closing.

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:
    system("dir c:/temp");
.
.
.
    infile.open("c:/temp/LAB5.TXT");
.
.
.
    infile.open("c:/temp/LAB5.IN");
.
.
.

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  
Old 17-May-2008, 17:56
pfanning pfanning is offline
Awaiting Email Confirmation
 
Join Date: Oct 2007
Posts: 39
pfanning is on a distinguished road

Re: Problem opening a file


Thanks 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 GIDBlog2nd Week of IA Training by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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

All times are GMT -6. The time now is 04:36.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.