K
KevinSimonson
How does one use C++ to write an array of <char> to disk and then read
it in again?
Kevin S
it in again?
Kevin S
How does one use C++ to write an array of<char> to disk and then read
it in again?
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.
What header file do I have to include in order for the compiler to seeYou 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.
<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>.
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?
Look here: http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.2
Read your book or STFW first. We're not going to write it for you.
You
have to put out *SOME* effort.
KevinSimonson said: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?
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?
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?
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?
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.