Can't use ofstream??

B

BCC

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::eek:ut);
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
 
J

John Harrison

BCC said:
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::eek:ut);
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

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
 
M

Martin Magnusson

BCC said:
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::eek:ut);
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.

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

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

/ martin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
474,183
Messages
2,570,970
Members
47,526
Latest member
RoslynDavi

Latest Threads

Top