CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jun 2002
    Location
    England
    Posts
    53

    stupid question about arrays

    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

  2. #2
    Join Date
    May 2000
    Location
    Phoenix, AZ [USA]
    Posts
    1,347
    Code:
    #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);
    }

  3. #3
    Join Date
    May 2000
    Location
    Wi, USA
    Posts
    144

    Re: stupid question about arrays

    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

  4. #4
    Join Date
    May 2000
    Location
    Phoenix, AZ [USA]
    Posts
    1,347
    I'm sorry; this example actually uses a file like you wanted in the
    first place:
    Code:
    #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);
    }

  5. #5
    Join Date
    Jun 2002
    Location
    England
    Posts
    53
    ok that worked a treat, thanks.

    cheers, bigdaz.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured