How to have a fixed size string / char in my structure

I'd want to have a fixed char element for my structure, in example, 200 characters, but I dont know how to do it.
Any help ?

My_structure {
char(200) title;
float a_float;
};

Additionally I want to have a knowed fixed structure size.
Any idea ? Have I to dim the string element at the constructor?
Thanks
Try this:

1
2
3
4
5
6
7
8

struct My_structure
    {
    char title[ 200 ];
    float a_float;

    };
Thanks.
I can't use std::string in my structure ? SIzeof does not understand the size of the string. This is the reason to use char, but I think that using c++ there must be another aproach.
Any idea ?
Thanks
http://cplusplus.com/reference/string/string/
Why not to use size operation for string-object?

code from: http://cplusplus.com/reference/string/string/size/
1
2
3
4
5
6
7
8
9
10
11
// string::size
#include <iostream>
#include <string>
using namespace std;

int main ()
{
  string str ("Test string");
  cout << "The size of str is " << str.size() << " characters.\n";
  return 0;
}
Uf.
Oh my friend I want to have my std::string into a structure and I want to know the size of the structure.
I have seen that it is imposible to use std::string inside a structure if you are going to load_save this structure. ( i think ... )
Thanks.
I really don't know what are you talking about :D

Anyways here is again something that tries to help:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <string>
using namespace std;

struct My_structure
{
        string title;
        float x;
        int y;
};

int main ()
{
        My_structure a;
        a.title = "this is title for struct a";

        cout << "The size of str in struct is " << a.title.size() << " characters.\n";
        cout << "Size of the struct is " << sizeof(My_structure) << endl;
        cout << "Max size for string inside struct is " << a.title.max_size() << endl;

        return 0;
}


After running i get following output:
The size of str in struct is 26 characters.
Size of the struct is 12
Max size for string inside struct is 1073741820
Last edited on
Yes, as you can see using std::string makes impossible to get the 26+4 bytes I need to convert my_structure to byte array (that it is what I want to do )

If you need the struct object ptr to be converted to a byte array, then yes you need it to be POD. it is no longer POD the moment that you had a std::string variable. However, you can learn to serialize by overloading the stream operators. That would be more of an object oriented approach. Do a google search on "c++ serialization", if you want to learn about other ways.
I have fix the problem.

If you want a generic function so save to a byte array any class using templates, you cannot use std:string inside your classes, because inside this generic type template function you cannot know the real size of the class you want to store.

That is to say, if you want to use std::string into a class, you need a custom function to can compute the size of the string and so, can to know the right number of bytes of your instance.

So, you have to use char[] and strcpy the std::string to the char . Its a pitty, because then you need to include stdio.h.

Good weekend
Tonnot, you really should take kempofighter's advice and do some Googling about serialization.

Are you trying to persist your class? Are you trying to stream it? In either case, you need to either know the size of your object, or the end of your stream.

If I were trying to persist this class, I would probably add a size_t to the structure to contain the string's length:

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

struct My_structure
{
    size_t stTitleLength;
    string title;
    float x;
    int y;
};

The do something like this:

My_structure s;

s.title = "Persist me!";

s.stTitleLength = s.title.length();

size_t ThisMuch = sizeof( s ) + s.stTitleLength;

ofstream Out( "something.out" );

if( Out.good() )
    Out << s.stTitleLength << s.title.data() << s.float << s.y;

This will make it easy to read it back in.  There are very many ways to do this.
@tonnot
You need to google "c++ serialization".

You are trying to use a generic function the wrong way. They weren't meant to solve what you are trying to do. The standard streams are meant to do it, so use them.
Topic archived. No new replies allowed.