fout.close()

K

Kev

Rather unusual... unless Im missing something trivial

ofstream fout;
fout.open("test.txt");
fout << "My name is: " <<myname << endl;
if( fout.is_open() )
fout.close();

The result is an access violation on fout.close(). Always at 0x00000010. If
the if statement is removed so only fout.close() remains the error also
remains. If it matters its in a contructor, used to write some of the
parameters on creation.

cheers
 
S

Srini

Rather unusual... unless Im missing something trivial
ofstream fout;
fout.open("test.txt");
fout << "My name is: " <<myname << endl;
if( fout.is_open() )
fout.close();

The result is an access violation on fout.close(). Always at 0x00000010. If
the if statement is removed so only fout.close() remains the error also
remains. If it matters its in a contructor, used to write some of the
parameters on creation.

cheers

It worked fine for me. What compiler are you using? And a bit more
detail on your code and the error you are getting might help to point
out the problem.

Srini
 
K

Kev

It worked fine for me. What compiler are you using? And a bit more
detail on your code and the error you are getting might help to point
out the problem.

VC++6. I agree. Its as simple as it gets. I dont think there are any
typos below. No syntax or compile errors. Runtime error: 'The instruction
at etc etc etc. The memory could not be written.' iostream fstream and
conio.h are included.

This is the basic idea. Note that some things were cut for brevity. There
are three objects, all nested. Middle and Bottom are created in
contructors. At the moment there are only one Top and Middle, but more
could be added later.

using namespace std;
Main()
{
TopObj = new Top;
}

Top::Top()
{
MiddleObj1 = new Middle;
//MiddleObj2 = new Middle;
};


Middle::Middle()
{

BottomObj1 = new Bottom;
//BottomObj2 = new Bottom;
//BottomObj3 = new Bottom;
//BottomObj4 = new Bottom;

//fout does not access the above... test only.
//Intended to put creation variables in a log.

string myname = "George";
ofstream fout;
fout.open("test.txt");
fout << "My name is: " <<myname << endl;
if( fout.is_open() )
fout.close();
}

Bottom::Bottom()
{
x=1;
y=2;
z=3;
}

In actual fact "My name is: George" is written to the file. So I do not
understand what is not being written.
 
S

Srini

I tried this second sample too... I checked it under g++ and aCC. I
don't have a VC++ compiler. This one worked fine too. Please check the
same under a different compiler. I could not find any problem with the
code. (though it can be a bit better)

Srini
 
K

Kev

I tried this second sample too... I checked it under g++ and aCC. I
don't have a VC++ compiler. This one worked fine too. Please check the
same under a different compiler. I could not find any problem with the
code. (though it can be a bit better)

lol doesnt surprise me.

I ran it from a simple test program... on the off chance that I really was
doing something silly. And yes it worked here too. So... there is something
in the program that is causing the problem and preventing it from working
anywhere. Put it in main at the very top and it didnt work there either.
Obviously there is a conflict somewhere. Maybe with the stderr and stdout
logs.

Hunting season has opened. Thanks :eek:)
 

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

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top