Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Pointer to file

Posted on 2002-07-30
15
Medium Priority
?
221 Views
Last Modified: 2010-04-01
Hi all.

Hopefully an extremely easy question here, but I can't work it out at all.

I have several functions called (within a single class), all returning values to send out to a file. This is getting quite messy now and I'd like to be able to write the values out from the various functions rather than passing the values back.

I'm currently using ofstream (doing something like):
    ofstream fout(file_name);
    data = subfunction(several_variables)
    fout << data;
    fout.close();

I figure I should be able to create a pointer to this and pass that to the functions. Sadly all my attempts at doing this have failed...!

Any ideas?

Steve
0
Comment
Question by:elstcb
  • 6
  • 5
  • 4
15 Comments
 
LVL 31

Accepted Solution

by:
Zoppo earned 200 total points
ID: 7187507
Why not simply pass the ofstream as reference to the functions? I.e.:

void subfunction( ofstream& fout, ... )
{
 ...
 fout << ...;
}

...
ofstream fout(..);
subfunction( ofstream, ... );
fout.close();

ZOPPO
0
 
LVL 2

Author Comment

by:elstcb
ID: 7187737
Zoppo.

Thanks for the comment. Can you go into a bit more detail? I tried the above but ran into a few problems as follows:

My function prototype in my .h was:
  void OutputData(ofstream&);
This gave an error:
  Syntax Error : identifier 'ofstream'

my call to the function was:
  OutputData(fout);
This gave an error:
  Function does not take 1 parameter (presumably because the prototype failed)

My function declaration itself was:
  void OutputData(ofstream& fout){
This gave an error:
  Class ofstream & not found in filename.cpp

Thanks in advance,

Steve
0
 
LVL 31

Expert Comment

by:Zoppo
ID: 7187743
I think it's a namespace problem ... ofstream is declared in
namespace std, so you need either a
using namespace std;
before the use of ofstream or you use std::ofstream instead.

Of course you have to #include <fstream> and <iostream> before you use ofstream. You can include them before you include "my.h".

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 2

Author Comment

by:elstcb
ID: 7187783
Zoppo.

Ok I tried that...

using namespace std;
gives :- 'std' : does not exist or is not a namespace

std::ofstream
gives :- 'std' is not a class or namespace

Not sure if this suggests it's the problem or not. ofstream works fine inside my original function, it's just the subfunction (and passing the stream) that's causing problems.

Both fstream and iostream were already included.

Thanks again,

Steve
0
 
LVL 2

Author Comment

by:elstcb
ID: 7187944
Getting somewhere now... got the namespace working at last :-)

The above seems like it would work if the function is stand alone, however I can't get the function prototype to declare properly in my Class Dialog.h which is where the problems were arising.

I need to access some of the member variables within the class (plus other class functions from within this one). I tried including iostream and fstream in my .h as well as doing the namespace but that brought up lots of errors in fstream...

Any hints to get it going within my class?

Thanks,

Steve
0
 
LVL 31

Expert Comment

by:Zoppo
ID: 7188158
Hmm ... include <iostream> and <fstream> wherever you
inlcude "my.h" before(!) "my.h" ... then you should be
able to use std::ofstream within "my.h"

or you include <iostream> and <fstream> within "my.h"
0
 
LVL 31

Expert Comment

by:Zoppo
ID: 7188162
... maybe then you need to use 'std::ofstream' instead of 'using namespace std', because it maybe that 'using namespace std' may lead to problems if you i.e. have a variable with name 'list'.
0
 
LVL 30

Expert Comment

by:Axter
ID: 7188177
elstcb,
It sounds like you're using the *.h headers, or mixing the extension headers with extensionless headers.

Make sure that all your code is using the extensionless headers for the std objects.
Example
#include <iostream>
#include <fstream>

Don't use the following:
#include <iostream.h> //Bad headers
#include <fstream.h>  //Bad headers

0
 
LVL 30

Expert Comment

by:Axter
ID: 7188180
Also, I don't recommend that you put "using namespace std" in your header file.
Therefore, you should prefix all your std objects with std::????.

So change code to the following:
void subfunction( std::ofstream& fout);
0
 
LVL 2

Author Comment

by:elstcb
ID: 7188369
Yep that would be the one!

Axter, as you're here... :-p
Do you remember those arrays of ifstreams in one of my other questions? ( http://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=cplusprog&qid=20327833 ) How would I do similar with these?

I tried converting:

     ifstream *fin[6];
     for (int i=0; i<6; i++)
     {
          fin[i] = new ifstream(m_file_paths[i]);
     }

     ...code now in function...


to:
     std::ifstream *fin[6];
     for (int i=0; i<6; i++)
     {
          fin[i] = new std::ifstream(m_file_paths[i]);
     }

     aircraft[0] = GetNextLine(fin[0]);


but when it called the function it fell over saying:
error C2664: 'GetNextLine' : cannot convert parameter 1 from 'class std::basic_ifstream<char,struct std::char_traits<char> > *' to 'class std::basic_ifstream<char,struct std::char_traits<char> > &'
A reference that is not to 'const' cannot be bound to a non-lvalue

Any further help really appreciated.

Thanks,

Steve
0
 
LVL 30

Expert Comment

by:Axter
ID: 7188394
If GetNextLine function is the following type:
GetNextLine(std::ifstream &my_fin);

The use the following syntax to use it with a pointer:
GetNextLine(*(fin[0]));

0
 
LVL 30

Expert Comment

by:Axter
ID: 7188398
If GetNextLine function is the following type:
GetNextLine(std::ifstream *my_fin);


Then use the following syntax to use it with a pointer:
GetNextLine((fin[0]));

0
 
LVL 2

Author Comment

by:elstcb
ID: 7190380
Yep that works a treat, thanks! Ok so you both have points on the way to you, one last thing before I close the question though...

Having rebuilt everything this morning I'm now getting warnings in lots of the VC++ include files. I'm *fairly* certain that they weren't there before I changed the file streams yesterday... I's post them but they're massive and I don't have a disc to hand to transfer them. It could be arranged though :-)

Is this maybe because I now have a mix of #include <---> and #include "---.h" ?

Thanks,

Steve
0
 
LVL 2

Author Comment

by:elstcb
ID: 7190404
Ignore that last comment - I just found that someone had unticked the precompiled header option, should have checked that first really! Doh!

As you have both helped me out here I've posted another question with points for Axter at:
http://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=cplusprog&qid=20330486

I can't say thank you enough for both helping me out here, it is very much appreciated! I'd forgotten how much this place rocked, I'll no doubt be back in the near future! :-)

Steve
0
 
LVL 31

Expert Comment

by:Zoppo
ID: 7190438
You're very welcome ... glad that I could help you.

Have a nice day,

regards,

ZOPPO
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

972 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question