473,466 Members | 1,451 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Can't use ofstream??

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::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
Jul 22 '05 #1
2 1862

"BCC" <a@b.c> wrote in message
news:r0*****************@newssvr13.news.prodigy.co m...
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


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
Jul 22 '05 #2
BCC wrote:
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.


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
Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: james545 | last post by:
I am having a problem with ofstream creating files. Sometimes when I run the program, ofstream creates a new file for writing and writes to it. No problem. But sometimes it fails to create the...
1
by: red floyd | last post by:
Is there any way to retrieve the filename given to a std::ofstream (passed in constructor or in ofstream::open())? Or, should I derive from ofstream (should probably be a template to handle...
3
by: Chase Bradford | last post by:
Hey all I have a class Foo, and I'm trying to overload the << operator for both the ostream and ofstream for it. This way I should have two seperate formats for output, one for files and another...
2
by: Steven C | last post by:
ofstream blah; IS: if (blah == NULL) { } THE SAME AS:
8
by: Joe | last post by:
Hi, I want to use the C library function mkstemp. It returns a unix file descriptor (int) to an open file. How do I convert the file descriptor to a valid ofstream object? Thanks, Joe
15
by: keweiming | last post by:
I have a project which needs to open hundreds to thousands of files for writing. The following is a simplified test program I wrote to see if I can use a map<string, ofstream> object to keep the...
5
by: Squid Seven | last post by:
I'm trying to use a pointer to an ofstream object and having problems: ofstream *sessionFile = NULL; if( directory == "" ) sessionFile = new ofstream( fileName.c_str(), ios::out ); else {
5
by: wobudui | last post by:
Hi everyboday, I have some trouble in dealing with the file stream. My souce code Listed hear: int main() { char buffer={0}; ofstream ofile.open("mydata.in",ios::app); ofile.seekp(10);...
5
by: Joe Hesse | last post by:
Hi, I have a C++ function that writes to an ofstream object. I would like to sometimes use it to write to cout. I realize that cout is of type ostream which is not ofstream. Since cout is "kind...
5
by: ednaswap | last post by:
Hello, C++ experts, I am writing a Log class something like below. ================= c++ source code ======================== class Log { public: Log(); Log(string _filename);
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.