tags:

views:

102

answers:

4

Like this function in C:

size_t fwrite ( const void * ptr, size_t size, size_t count, FILE * stream );

I've looked in c++ file stream and found this one:

ostream& write ( const char* s , streamsize n );

this one only accepts char* instead of void*

but does it really matter if i use a c-style fwrite function in c++>

A: 

In C++ you will want to use std::ofstream objects to write to a file. They can accept any type of data using the << operator, in much the same way that std::cout works for writing to the console. Of course, just like std::cout, if you want to print a custom type, you will need to define an operator<< overload for it.

An example:

std::ofstream outfile("myfile.txt");

int i = 5;
double d = 3.1415926535898;
std::string s = "Hello, World!";

outfile << i << std::endl;
outfile << d << std::endl;
outfile << s << std::endl;

To use std::ofstream, you need to #include <fstream>.

The outfile object will automatically close the file when it destructs, or you can call its close() method.

Tyler McHenry
+2  A: 

You can use either one. Using char * instead of void * doesn't make much real difference -- both fwrite and ostream::write are typically used for a variety of data types (with with C++, you need to add an explicit cast to char *, where in C the cast will happen implicitly, assuming you've included a proper prototype for fwrite).

Jerry Coffin
Just make sure you opened the stream with the std::ios::bin flag.
Billy ONeal
+2  A: 

Streams are probably what you're looking for unless I misunderstand your question. There are many flavors that handle different jobs, like outputting to a file:

#include <cstdlib>
#include <fstream>
#include <string>
using namespace std;


int main()
{
    ofstream f("c:\\out.txt");

    const char foo[] = "foo";
    string bar = "bar";
    int answer = 42;

    f << foo << bar<< answer;

    return 0;
}

...building strings like you would with printf:

#include <cstdlib>
#include <sstream>
#include <string>
#include <iostream>
using namespace std;


int main()
{
    stringstream ss;

    const char foo[] = "foo";
    string bar = "bar";
    int answer = 42;

    ss << foo << bar<< answer;
    string my_out = ss.str();

    return 0;
}

...and they can even handle your own types, if you tell them how:

#include <cstdlib>
#include <string>
#include <iostream>
using namespace std;

class MyGizmo
{
public:
    string bar_;
    int answer_;

    MyGizmo() : bar_("my_bar"), answer_(43) {};
};

ostream& operator<<(ostream& os, const MyGizmo& g)
{
    os << g.bar_ << " = " << g.answer_;
    return os;
}
int main()
{
    MyGizmo gizmo;
    cout << gizmo;

    return 0;
}
John Dibling
A: 

Contrary to already given answers, there is an important difference between fwrite() and ostream::write().

fwrite() writes binary data unmodified (well, on those poor non-Unix platforms there is endline translation, unless the file is opened in binary mode).

ostream::write() uses locale to transform every character, this is why it accepts char* rather than void*. Normally, it uses the default "C" locale, which does not do any transformation.

Just keep in mind that basic_ostream is a formatter on top of basic_streambuf, not a binary sink.

Maxim Yegorushkin
fwrite writes binary data if you open the file in binary mode, and writes it as text if you open the file in text mode. On some systems there's no difference between text and binary mode though.
nos
dude, you should have read my post before commenting: ...(well, on those poor non-Unix platforms there is endline translation, unless the file is opened in binary mode)
Maxim Yegorushkin