473,696 Members | 1,951 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

what's the difference betwwen these two ways to use std::ofstream

//case 1, works fine
function(..)
{
std::ofstream out;
out.open(file);
out << "error info" <<endl;
out.close();
}

//case 2, declare it in a class
class Logger
{
public:
Logger(std::wst ring file, std::ios_base:: openmode mode /*undefined
too, why?*/){}
private:
std::ofstream out; //error c2079, uses undefined class
'std::basic_ofs tream<_Elem, _Traits>'
}

btw, how to use ios_base:openmo de? is it declared in std namespace?

Aug 24 '06 #1
2 5661

"thinktwice " <me*********@gm ail.comskrev i meddelandet
news:11******** ************@m7 3g2000cwd.googl egroups.com...
//case 1, works fine
function(..)
{
std::ofstream out;
out.open(file);
out << "error info" <<endl;
out.close();
}
Just a guess: The function is in a cpp file that includes the
<fstreamheade r?

//case 2, declare it in a class
class Logger
{
public:
Logger(std::wst ring file, std::ios_base:: openmode mode
/*undefined
too, why?*/){}
private:
std::ofstream out; //error c2079, uses undefined class
'std::basic_ofs tream<_Elem, _Traits>'
}
A class declaration if often in a .h file. If so, it must also include
the necessary headers for names it uses.
>
btw, how to use ios_base:openmo de? is it declared in std namespace?
It is. There is a header <iosthat contains a lot of types and
constants used by the stream classes.
Bo Persson
Aug 24 '06 #2
thanks Persson , you are right,
i do include <fstreamin cpp file.
and the function implementation and the class definination is in the
header file.

Aug 25 '06 #3

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

Similar topics

0
2009
by: m vaughn | last post by:
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...
1
11429
by: red floyd | last post by:
Is there any way to retrieve the filename given to a std::ofstream (passed in constructor or in ofstream::open())? Or, should I derive from ofstream (should probably be a template to handle ifstream and fstream as well, but I'm typing on the fly) yes, I know the syntax may be off... class onamedfstream : public ofstream {
3
7824
by: Chase Bradford | last post by:
Hey all I have a class Foo, and I'm trying to overload the << operator for both the ostream and ofstream for it. This way I should have two seperate formats for output, one for files and another for the screen. Right now the two declarations are: std::ofstream& operator<<( std::ofstream &fos, const Foo &theClass); std::ostream& operator<<( std::ostream &fos, const Foo &theClass);
6
3214
by: ma740988 | last post by:
There's no way to use the STL algorithm copy to print an outfile (essentially an ofstream)? So now: int main() { std::ifstream InFile( "exercise15.txt"); std::ofstream ToFile( "NewFile.txt" ); //////////////////////////// // Alternative 1
11
3822
by: Gurikar | last post by:
Hello, ofstream ofs; fun(char* str) { ofs<<str<<endl; } void main()
8
14318
by: davihigh | last post by:
My Friends: I am using std::ofstream (as well as ifstream), I hope that when i wrote in some std::string(...) with locale, ofstream can convert to UTF-8 encoding and save file to disk. So does ifstream. Something I found shows that, I need to have a proper codecvt to set it. I need more information, maybe a small piece of code sample. Thank you!
3
9936
by: magix | last post by:
Hi, I got following to savefile into Output.txt, but how can I specify it to be in specific folder/directory (not at the same directory as the executables? Currently, it will generate the output.txt under the same directory as the executables. std::stringstream stream; stream << "Output.txt" << std::flush; std::ofstream SaveFile(stream.str().c_str());
15
6007
by: aaragon | last post by:
Hello, does anyone have a clue about this error? and how to solve it? It seems to be trivial to me, but not for the compiler. I'm using g++ 4.2 on an Ubuntu Linux system: // main() .... std::ofstream fout; fout.open("hello.out"); fout<<setfill('-')<<setw(20)<<"-"<<setfill(' ')<<endl; fout<<"hello "; // this is line 139
2
2366
by: Dave Johansen | last post by:
Is it ok to call fcloseall() when using both C-style FILE * and C++ std::ofstream's to read files in a program? Thanks, Dave
0
8597
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9145
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8880
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6515
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5857
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4356
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4611
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3033
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2319
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.