Connecting Tech Pros Worldwide Forums | Help | Site Map

Can't use ofstream??

BCC
Guest
 
Posts: n/a
#1: Jul 22 '05
Hi I have an application/project called HKDemo. In HKDemo.cpp, in the usual
init stuff for the main class HKDemoApp, I can use the following code with
no problems:
ofstream out_file("test.txt", ios::out);
try {
out_file << "some stuff" << endl;
}
catch (...) {

}
out_file.close();

But this same code does not work in MainFrm.cpp! I must be missing
something really simple. Everything compiles just fine, and runs just fine
(i.e. in debug mode I can step over the code and it looks to be dumping data
to a file), but no file is created, and nothing is written.

Anyone have any suggestions as to what I am missing? Maybe its related to
the whole MVC concept?

Thanks,
Bryan



John Harrison
Guest
 
Posts: n/a
#2: Jul 22 '05

re: Can't use ofstream??



"BCC" <a@b.c> wrote in message
news:r0Zkd.7933$zx1.4466@newssvr13.news.prodigy.co m...[color=blue]
> Hi I have an application/project called HKDemo. In HKDemo.cpp, in the
> usual init stuff for the main class HKDemoApp, I can use the following
> code with no problems:
> ofstream out_file("test.txt", ios::out);
> try {
> out_file << "some stuff" << endl;
> }
> catch (...) {
>
> }
> out_file.close();
>
> But this same code does not work in MainFrm.cpp! I must be missing
> something really simple. Everything compiles just fine, and runs just
> fine (i.e. in debug mode I can step over the code and it looks to be
> dumping data to a file), but no file is created, and nothing is written.
>
> Anyone have any suggestions as to what I am missing? Maybe its related to
> the whole MVC concept?
>
> Thanks,
> Bryan[/color]

Instead of stepping over the code I would use your debugger to step into the
ofstream code, then you can find out what is really happening.

There are all sorts of reasons that file I/O doesn't work (although it has
nothing to do with MVC). For instance it could be that the file is created
but not where you expect it to be (that would be my first guess), try
replacing "test.txt" with "C:\\test.txt" for instance. Or it could be that
you don't have write permission in the place where you are trying to create
the file.

John


Martin Magnusson
Guest
 
Posts: n/a
#3: Jul 22 '05

re: Can't use ofstream??


BCC wrote:[color=blue]
> Hi I have an application/project called HKDemo. In HKDemo.cpp, in the usual
> init stuff for the main class HKDemoApp, I can use the following code with
> no problems:
> ofstream out_file("test.txt", ios::out);
> try {
> out_file << "some stuff" << endl;
> }
> catch (...) {
>
> }
> out_file.close();
>
> But this same code does not work in MainFrm.cpp! I must be missing
> something really simple. Everything compiles just fine, and runs just fine
> (i.e. in debug mode I can step over the code and it looks to be dumping data
> to a file), but no file is created, and nothing is written.[/color]

I think you should check that the stream is actually opened, after you
initialize it. Something like:

ofstream out_file("test.txt", ios::out);
if (not out_file.is_open())
{
cerr << "Could not open file for writing.\n" ;
}

/ martin
Closed Thread