Click to See Complete Forum and Search --> : stupid question about arrays


BigDaz
September 25th, 2002, 04:55 AM
if i have an array such as:

unsigned short m_usArray[1000];

and i fill it up with unsigned shorts, is there away i can write the entire array to a file with just one command instead of doing something like:

ofstream ("file.txt");
for( int i = 0; i < 1000; i++ )
{
fout << m_usArray[i];
}
fout.close();

?

cheers, bigdaz

PaulWendt
September 25th, 2002, 06:31 AM
#include <algorithm>
#include <iostream>
#include <iterator>
using namespace std;

const unsigned int SIZE = 10;

int main(int argc, char *argv[])
{
unsigned short numberList[SIZE] = {1,2,3,4,5,6,7,8,9,10};
copy(numberList, numberList + SIZE, ostream_iterator<unsigned short>(cout, "\n"));
return (0);
}

Dave Sell
September 25th, 2002, 06:32 AM
Hi,

to answer your question, maybe :-) If there were a command
out there, please tell me how you would want it to look.

My guess would be:

WriteArrayToFile(char *FileName, int Options, USHORT *pusArray, int Length);

It is quite possible it does exist, or if not, someone could make
it for you.

Dave

Originally posted by BigDaz
if i have an array such as:

unsigned short m_usArray[1000];

and i fill it up with unsigned shorts, is there away i can write the entire array to a file with just one command instead of doing something like:

ofstream ("file.txt");
for( int i = 0; i < 1000; i++ )
{
fout << m_usArray[i];
}
fout.close();

?

cheers, bigdaz

PaulWendt
September 25th, 2002, 06:35 AM
I'm sorry; this example actually uses a file like you wanted in the
first place:

#include <algorithm>
#include <iostream>
#include <iterator>
#include <fstream>
using namespace std;

const unsigned int SIZE = 10;

int main(int argc, char *argv[])
{
ofstream of("file.txt");
unsigned short numberList[SIZE] = {1,2,3,4,5,6,7,8,9,10};
copy(numberList, numberList + SIZE, ostream_iterator<unsigned short>(of, "\n"));
return (0);
}

BigDaz
September 25th, 2002, 09:57 AM
ok that worked a treat, thanks.

cheers, bigdaz.