问题
I'm new to C++ and I'd like to do simple stuff such as writting to disk the content of a char[]
I'm having hard times doing it.
Here's my code:
char x[256],y[256],z[256];
sprintf( x, "%.2f", pCommandHandling->m_dtHandleInformation[i].Xfrms.translation.x ); //pCommandHandling->m_dtHandleInformation[i].Xfrms.translation.x is a float struct
sprintf( y, "%.2f", pCommandHandling->m_dtHandleInformation[i].Xfrms.translation.y ); //pCommandHandling->m_dtHandleInformation[i].Xfrms.translation.y is a float struct
sprintf( z, "%.2f", pCommandHandling->m_dtHandleInformation[i].Xfrms.translation.z ); //pCommandHandling->m_dtHandleInformation[i].Xfrms.translation.z is a float struct
FILE *tracker_file = fopen("NDI_FiMe.TMP","w");
char buffer[] = {x,";",y,";",z};
fwrite(buffer , sizeof(char), sizeof(buffer), tracker_file);
fclose(tracker_file);
The problem I'm having is that I get:
IntelliSense: a value of type "char *" cannot be used to initialize an entity of type "char"
回答1:
You can't put a char array (x) in a list of chars. As the error message says. If you want to copy a char array into another char array see the strcat family of functions.
The next error involves sizeof. It does not compute the length of a string. As you are using it now it will give a 4: the size of the buffer pointer. There is a strlen function for getting the length of C string.
来源:https://stackoverflow.com/questions/24254183/value-of-type-char-cannot-be-used-to-initialize-an-entity-of-type-char