![]() |
| ||
| Doesn't open for file input successfully.... why? **I know the code looks big, but it's a very small isolated section at the bottom (starting at line 164) that's giving me the problem.** When this program is run, when I try to open the file I just appended some text to, it doesn't open... can anyone spot why? Thanks.... -Matt p.s. Yes this is a program I made after following a tutorial, incase you're wondering. //#include <iostream.h> <--No need for this because fstream includes it |
| ||
| Re: Doesn't open for file input successfully.... why? Your problem is that opening and closing files doesn't change the stream state. When you read fin to end-of-file, closing the stream doesn't change the eofbit. You need to clear the stream state as well as close the file if you plan on using fin to read from a file again: fin.clear();You can also avoid mysterious errors like this by using is_open instead of the boolean conversion to check the stream after opening it: if(!fin.is_open()) //if unable to open for file inputThis guarantees that you're testing if the file is open, not if it's in a good state altogether. |
| ||
| Re: Doesn't open for file input successfully.... why? Matt, don't use deprecated headers and system("pause");. They call for bad programming. |
| ||
| Re: Doesn't open for file input successfully.... why? Thanks a lot, that helped and my program works now, although I don't understand what I should be using instead of the iostream.h i tried just iostream and it said cout was undefined... |
| ||
| Re: Doesn't open for file input successfully.... why? You might have to add the line using namespace std;or add a std:: before every cout and cin (etc). What are you using for a compiler? Sounds like the old Borland thingy! |
| ||
| Re: Doesn't open for file input successfully.... why? I tried that and it still said the same thing. But is any header with the .h a deprecated one? or just the iostream one? Because i tried it with the fstream one just now. And I'm using the latest version of "Bloodshed Dev-C++" |
| ||
| Re: Doesn't open for file input successfully.... why? > i tried just iostream and it said cout was undefined... Well you also need to learn about namespaces as well. A crude "fix" is to convert #include <iostream.h>to #include <iostream>Better use would be only list the things which interest you. #include <iostream>And even better, avoid "using" altogether and be totally explicit in what type of cout you mean. #include <iostream>The more precise you can be, the fewer problems you'll have when it comes to using code which has several namespaces. > But is any header with the .h a deprecated one? or just the iostream one? All the ones declared by ANSI have lost the .h suffix, and now make use of the "std" namespace. The latest dev-c++ should be fine. |
| All times are GMT -4. The time now is 4:43 pm. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC