Connecting Tech Pros Worldwide Forums | Help | Site Map

fout.close()

Kev
Guest
 
Posts: n/a
#1: Aug 22 '05
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

Srini
Guest
 
Posts: n/a
#2: Aug 22 '05

re: fout.close()


> Rather unusual... unless Im missing something trivial[color=blue]
>
> 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[/color]

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

Kev
Guest
 
Posts: n/a
#3: Aug 22 '05

re: fout.close()


"Srini" <srinivasa.s@gmail.com> wrote in news:1124694172.825835.285460
@z14g2000cwz.googlegroups.com:
[color=blue]
> 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.[/color]

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.







Srini
Guest
 
Posts: n/a
#4: Aug 23 '05

re: fout.close()


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

Kev
Guest
 
Posts: n/a
#5: Aug 23 '05

re: fout.close()


"Srini" <srinivasa.s@gmail.com> wrote in news:1124770181.283903.219610
@g44g2000cwa.googlegroups.com:
[color=blue]
> 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)[/color]

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 :o)
Closed Thread


Similar C / C++ bytes