Thread: Assign Character Arrays data from an STL Container

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348

    Assign Character Arrays data from an STL Container

    Hi.

    Let say there is a standard STL vector contain with data in it. How do you assign a character arrays the data inside the vector contain either through value or pointer?

    I need to do that so I could write the data to a file in binary mode.

    Thanks,
    Kuphryn

  2. #2
    Registered User Strider's Avatar
    Join Date
    Aug 2001
    Posts
    149
    One way you could do it is to dynamically allocate an array based on the size of the vector:
    Code:
    #include <iostream>
    #include <vector>
    
    using namespace std;
    
    #define ARAY_MAX 4
    
    int main()
    {
        char chString[ARAY_MAX] = {'A', 'B', 'C', 'D'};
        char *pString;
        vector<char> vChar;
    
        for (int i = 0; i < ARAY_MAX; i++)
            vChar.push_back(chString[i]);
    
        pString = new char[vChar.size()];
    
        for (vector<char>::const_iterator iter  = vChar.begin();
             iter != vChar.end();
             ++iter)
        {
            static int siCount = 0;
            pString[siCount] = (*iter);
            ++siCount;
        } // end for loop
    
        for (i = 0; i < vChar.size(); i++)
            cout << pString[i] << " ";
    
        delete [] pString;
    
        return 0;
    } // end main
    David
    One Ring to rule them all, One Ring to find them,
    One Ring to bring them all and in the darkness bind them
    In the Land of Mordor where the Shadows lie.

  3. #3
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    If you just need to convert for file writing/reading then you can cast -

    Code:
    vector <int> vec;
    	ofstream os("file.txt",ios::binary);
    	vec.push_back(10);
    	vec.push_back(5);
    
    	for(vector <int>::iterator it=vec.begin();it!=vec.end();it++)
    						os.write((char*)it,sizeof(int));
    zen

  4. #4
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Hey thanks guys.

    David,

    Nice!

    First, what does:

    vChar.size();

    return?

    Lastly, you used:

    ++iter

    Note the preincrement. In this particular code, it believe it is arbitrary. Am I correct?

    zee,

    Wow! I have not even begun to think of using the write function to write data from an STL container to a file in binary mode. My plan had always been to assign data from the container to a character array and then write the data to a file.

    Also, in the following code:

    for(vector <int>::iterator it=vec.begin();it!=vec.end();it++)
    os.write((char*)it,sizeof(int));

    What is the difference between:

    (char*)

    and

    reinterpret_cast<char *>(it)?

    Thanks,
    Kuphryn

  5. #5
    Registered User Strider's Avatar
    Join Date
    Aug 2001
    Posts
    149
    First, what does:

    vChar.size();

    return?
    This returns a integer count of the total number of elements contained in the vector.
    Lastly, you used:

    ++iter

    Note the preincrement. In this particular code, it believe it is arbitrary. Am I correct?
    Correct. It does not matter if it is pre or post. It is just habit for me to use the preincrement here.

    David
    One Ring to rule them all, One Ring to find them,
    One Ring to bring them all and in the darkness bind them
    In the Land of Mordor where the Shadows lie.

  6. #6
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    What is the difference between:

    (char*)

    and

    reinterpret_cast<char *>(it)?
    They'll both do the same thing, but the second is exclusive to C++, wheres the first comes from C++'s C heritage. The different types of casting in C++ (static_cast, const_cast, etc) allow the compiler to do differing amounts of checking depending on the type of cast you want to do. For instance you might use const_cast to remove a const modifier but still want the compiler to do the normal type checking. AFAIK reinterpret_cast does virtually no type checking and is more or less equivilent to the C cast (and I'm lazy and can never be bothered to type the full C++ syntax for casting).
    zen

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help...accessing character arrays in a structure
    By mathewmc in forum C Programming
    Replies: 7
    Last Post: 10-31-2006, 11:20 AM
  2. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  3. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 07:26 PM
  4. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM
  5. Array of Character Arrays
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 02-09-2002, 06:07 PM