elstcb
asked on
Pointer to file
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_variab les)
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
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_variab
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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".
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".
ASKER
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
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
ASKER
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
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
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"
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"
... 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'.
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
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
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);
Therefore, you should prefix all your std objects with std::????.
So change code to the following:
void subfunction( std::ofstream& fout);
ASKER
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? ( https://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,s truct std::char_traits<char> > *' to 'class std::basic_ifstream<char,s truct 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
Axter, as you're here... :-p
Do you remember those arrays of ifstreams in one of my other questions? ( https://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
}
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,s
A reference that is not to 'const' cannot be bound to a non-lvalue
Any further help really appreciated.
Thanks,
Steve
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]));
GetNextLine(std::ifstream &my_fin);
The use the following syntax to use it with a pointer:
GetNextLine(*(fin[0]));
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]));
GetNextLine(std::ifstream *my_fin);
Then use the following syntax to use it with a pointer:
GetNextLine((fin[0]));
ASKER
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
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
ASKER
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:
https://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
As you have both helped me out here I've posted another question with points for Axter at:
https://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
You're very welcome ... glad that I could help you.
Have a nice day,
regards,
ZOPPO
Have a nice day,
regards,
ZOPPO
ASKER
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