473,320 Members | 2,004 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

writes to files passed as struct of ofstream objects fail

I wanted to hold information about open files in a struct and be able
pass it around, but the behavior is not what I would expect. Can
someone tell me why this doesn't work? (Even better, what is the
proper way to do this, eg, without the pointers and such). As is
probably obvious, I am not terribly knowledgable here.

The *fs.of<< ... statement in main() does nothing in gcc 3.2.2 and
gives a seg fault in gcc 3.2 (Mandrake linux 9.1 and 9.0).

thanks

m

---------------------------------------------------------------------------------------------
// -*- c++ -*-
//writes to file passed as struct of ofstream object fails
#include <iostream>
#include <fstream>

struct ofstruct{
std::ofstream * of;
};

void open(ofstruct & fs)
{
std::ofstream ofile("of.txt",std::ios::out);

if( ofile.fail()){
std::cerr<<"of.txt : file could not be opened";
exit(1);
}
fs.of = &ofile;
std::cout<<*fs.of<<" "<<fs.of<<" is the fobj in open"
<<std::endl; // gives addresses
ofile<<"some filler"
<<std::endl; // writes to file, of course
*fs.of<<"some more filler"
<<std::endl; // writes to file as well
}
int main()
{
ofstruct fs;

open(fs);
std::cout<<*fs.of<<" "<<fs.of<< " is the fobj in main"
<<std::endl; // same addresses
*fs.of<< "filler from main"
<< std::endl; // but doesn't write to file

return 0;
}

------------------------------------------------------------------------------------------
Jul 19 '05 #1
0 1986

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

Similar topics

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 {
4
by: webdev | last post by:
lo all, some of the questions i'll ask below have most certainly been discussed already, i just hope someone's kind enough to answer them again to help me out.. so i started a python 2.3...
4
by: knapak | last post by:
Hello I'm a self instructed amateur attempting to read a huge file from disk... so bear with me please... I just learned that reading a file in binary is faster than text. So I wrote the...
3
by: peter.xiau | last post by:
In my case, I'd to open several output files at one time, but the number of output files are determined at run-time, not compile-time. So I tried to open file with std::ofstream when I need, and...
5
by: shyam | last post by:
HI I have a logging application wherin I create a ofstream object say, ofstream logger; by doing this logger.open(/* some path*/,ios_base:app);
5
by: jwright | last post by:
I have decided to use a struct to collect my data. The input file is comma dilineated between almost all of the fields. Here is the code I have so far and a sample input and output file. ...
5
by: cybersangeeth | last post by:
Hi, I need to read 1KB each time from multiple files in a folder and pass it to a byte array in a struct to be sent through a socket. I'm a C++ newbie. I managed to read 1KB each time from one...
15
by: JoeC | last post by:
I am writing a program that I am trying to learn and save binary files. This is the page I found as a source: http://www.angelfire.com/country/aldev0/cpphowto/cpp_BinaryFileIO.html I have...
24
by: Grey Alien | last post by:
If I have the ff struct: struct A { unsigned int i; char s; } a, b; And use them in code like this:
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.