writing the contents of a struct in binary mode

This is the second thread I've started regarding this topic in as many days, but I'm still stumped as how to get it to work. :(

Given:
There is a structure that contains a pointer to an array of c-style strings.
The total number of c-style strings is unknown at compile-time.
Each element of the array must be assigned its value from the cooresponding
element of an array of std::string.
The contents of the structure will be written to a binary file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
//elsewhere in the code
std::string sArr[] = ...
//sArr "knows" its size
int count = ...

struct data
{
   //...
   char * cArr[];
};

//function that retreives values from sArr and stores them in cArr
void transferValues(const std::string str[])
{
   int limit = count;

/* 
   error: expected type specifier before '*' token
   error: cannot convert 'int**' to 'char**' in initialization
   error: expected ',' or ';' before 'char'
                                                              */
   char ** cArr = new *char[limit]; // problems
   for(int i=0; i<limit; ++i)
      {
         cArr[i] = new char[5]; //the values in sArr[] have a 4 letter maximum
         strncpy(cArr[i], str[i], 4);
      }  
}


I've been working on this problem to help with my understanding of file I/O and c library functions, but I may have bitten off more than I could chew :/
If anyone can help me with this (hint at something, point me in a different direction, etc.) it will be GREATLY APPRECIATED.
Last edited on
Just use string.c_str() to get the c-style string out of your std::string:
1
2
3
4
5
6
const char **c;
c = new const char *[2];
std::string a = "hi";
std::string b = "bye";
c[0] = (char*)a.c_str(); //If you don't want to use const char** you can just typecast the return value
c[1] = b.c_str();
Last edited on
Topic archived. No new replies allowed.