Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Saving and loading arrays in C++

Name: Anonymous 2007-06-19 8:03 ID:3ejRjd7v

How do I save and load arrays to and from a text file in C++?

Name: Anonymous 2007-06-19 8:04 ID:Os9vn9jC

man 2 open
man 2 read
man 2 write
man 2 close

Name: Anonymous 2007-06-19 8:05 ID:3ejRjd7v

That makes no sense... can you elaborate?

Name: Anonymous 2007-06-19 8:11 ID:+KOcMMAt

unix man pages. RTFM or GTFO

Name: Anonymous 2007-06-19 8:21 ID:zUAxMIar

man 2 open read write close
more concise, no?

Name: Anonymous 2007-06-19 8:37 ID:GYbNYKbv

Before you follow the other advice in this thread and write C code in C++, check out Boost's serialisation library (I presume you already know iostreams).

Name: Anonymous 2007-06-19 9:00 ID:Heaven

>>1
#include <fstream>
ofstream ofs("filename", ios::out|ios::binary);
ofs.write((char*)array, sizeof(array_type)*size of array);
ofs.close();
???
return

Name: Anonymous 2007-06-19 9:53 ID:C2WpDSLA

Don't forget the STDs

Name: Anonymous 2007-06-19 10:11 ID:OMt8AVuW

template<typename T>
void save_vector(const vector<T>& v, ostream& out)
{
    copy(v.begin(), v.end(), ostream_iterator<T>(out, "\n"));
}

template<typename T>
vector<T> load_vector(istream& in)
{
    vector v;
    copy(istream_iterator<T>(in), istream_iterator<T>(), back_inserter(v));
    return v;
}

Name: Anonymous 2007-06-19 12:10 ID:C2WpDSLA

ooh pretty, no char *.

Name: Anonymous 2009-03-06 8:28

ed to wonder why   there was no   reason why a   twat like you   ARE CODING TIMES   A MILLION AND   soon one billion   transistor processors where   the actual computing   takes place in   my nightgown while   humming the SICP   video lectures and   then read the   Design of Everyday   Things and About   design and you   can run the   computation another day.

Name: Anonymous 2009-03-06 14:29

Meditate on lexical and!

Name: Anonymous 2010-11-25 6:26

Name: Anonymous 2010-11-27 12:38

Don't change these.
Name: Email:
Entire Thread Thread List