Writing a <char> Array to Disk and Reading it in again

K

KevinSimonson

How does one use C++ to write an array of <char> to disk and then read
it in again?

Kevin S
 
V

Victor Bazarov

How does one use C++ to write an array of<char> to disk and then read
it in again?

What is "an array of<char>"? Do you mean std::array<char> ? Or do you
mean char[N] (for some known N)?

V
 
L

Luc Danton

How does one use C++ to write an array of<char> to disk and then read
it in again?

Kevin S

You can try (assuming a regular array):

const int size = /* ... */
char data[size];

{
ofstream file("path/to/file");
// check fstream here
file.write(data, size);
// check that write was successful
}

{
ifstream file("path/to/file");
// check fstream here
char read[size];
file.read(read, size);
// check that read was successful
}

If you're using an std::array (or std::tr1::array), then you can use the
begin and size methods to do the same [ i.e. file.write(data.begin(),
data.size()); ].

Note that all of the above assume a known, constant size for the data.
If instead you're using a dynamic size (e.g. std::vector<char>,
std::string) or if the size varies across invocations, then a possible
solution is to write first the size (which is another question and
problem on its own right!), then the buffer. When reading, you'd read
the size first, then e.g. resize an std::vector<char> before reading the
data proper.

There are other caveats that you might need to take into account (are
you reading/writing from the same machine? same OS?) depending on what
you want to achieve.
 
K

KevinSimonson

You can try (assuming a regular array):

const int size = /* ... */
char data[size];

{
   ofstream file("path/to/file");
   // check fstream here
   file.write(data, size);
   // check that write was successful

}

{
   ifstream file("path/to/file");
   // check fstream here
   char read[size];
   file.read(read, size);
   // check that read was successful

}

If you're using an std::array (or std::tr1::array), then you can use the
begin and size methods to do the same [ i.e. file.write(data.begin(),
data.size()); ].

Note that all of the above assume a known, constant size for the data.
If instead you're using a dynamic size (e.g. std::vector<char>,
std::string) or if the size varies across invocations, then a possible
solution is to write first the size (which is another question and
problem on its own right!), then the buffer. When reading, you'd read
the size first, then e.g. resize an std::vector<char> before reading the
data proper.

There are other caveats that you might need to take into account (are
you reading/writing from the same machine? same OS?) depending on what
you want to achieve.

What header file do I have to include in order for the compiler to see
<ofstream> and <ifstream>? I tried writing a C++ program that wrote
to a file right now, and the compiler called 'ofstream' an "undeclared
identifier".

Kevin S
 
L

Luc Danton

You can try (assuming a regular array):

const int size = /* ... */
char data[size];

{
ofstream file("path/to/file");
// check fstream here
file.write(data, size);
// check that write was successful

}

{
ifstream file("path/to/file");
// check fstream here
char read[size];
file.read(read, size);
// check that read was successful

}

If you're using an std::array (or std::tr1::array), then you can use the
begin and size methods to do the same [ i.e. file.write(data.begin(),
data.size()); ].

Note that all of the above assume a known, constant size for the data.
If instead you're using a dynamic size (e.g. std::vector<char>,
std::string) or if the size varies across invocations, then a possible
solution is to write first the size (which is another question and
problem on its own right!), then the buffer. When reading, you'd read
the size first, then e.g. resize an std::vector<char> before reading the
data proper.

There are other caveats that you might need to take into account (are
you reading/writing from the same machine? same OS?) depending on what
you want to achieve.
What header file do I have to include in order for the compiler to see
<ofstream> and<ifstream>? I tried writing a C++ program that wrote
to a file right now, and the compiler called 'ofstream' an "undeclared
identifier".

Kevin S

Try #include <fstream>.
 
K

KevinSimonson

Try #include <fstream>.

Great; thanks; I got it working.

Now how _would_ I write an integer to the <ofstream> object, and then
read the integer from the <ifstream> object, so that I could have
dynamically sized <char> arrays?

Kevin S
 
J

James Kanze

Great; thanks; I got it working.
Now how _would_ I write an integer to the <ofstream> object,
and then read the integer from the <ifstream> object, so that
I could have dynamically sized <char> arrays?

For everything but char's, it depends on the defined format of
the file. What format do you want for the integer?
 
B

Brian Wood

For everything but char's, it depends on the defined format of
the file.  What format do you want for the integer?

I have an archive --
http://webEbenezer.net/misc/direct.tar.bz2 -- that has code related
to the format James is talking about. Formatting.hh and
Formatting.cc are in the archive. I developed those files with
help from James and others on newsgroups.

Brian Wood
Ebenezer Enterprises
 
R

red floyd

Red Floyd, this isn't a homework problem, and I don't have a book.
Can you recommend a good book for someone who wants to know things
like this in C++?  Or, better yet, is there a C++ tutorial website?
Also, what does STFW stand for?

I'm sorry, maybe I was a bit hasty in pointing you at the homework
FAQ. But it seems to me you could put a bit more effort into
researching
it yourself, instead of having us tell you how. Then you'll *KNOW*
it.
You didn't seem to want to do any of the legwork yourself, hence my
reactions.

By the way, STFW stands for "Search The F[ine] Web".
 
P

Paul N

Can you recommend a good book for someone who wants to know things
like this in C++?  Or, better yet, is there a C++ tutorial website?

This question came up recently and Alf Steinbach, one of the regulars
here, mentioned http://www.cplusplus.com though with the comment
"while it's not yet very good on C++ practices, at least now it's
mostly technically correct". Let us know if you find it useful.
Also, what does STFW stand for?

It stands for "Search The, er, Web" in the same way that RTFM means
"Read The, er, Manual". Another abbreviation sometimes used is GIYF
meaning "Google Is Your Friend", which, like STFW, suggests that you
can easily find the answers from a search engine.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top