How can I convert short int[] to char*?
short int test[4000];
char* test2;
I tried this:
test2 = (char*)test[4000]
Error–> PTR is not valid
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
So you have a buffer in form of an array, and you want to write the binary contents of it into a file. You do it like this:
fwrite()function is used to write binary data to files (andfread()to read). It takes avoid*pointer, so it can work with any type (and C++ implicitly converts any other pointer/array to it).The
sizeof(test)determines the exact size of the array. If you don’t want to write the whole of it (i.e. just filled part of it), you want to usesizeof(short) * N, where N is the number of filled elements.1here means that there is one block of data to write; sofwrite()will write the whole data at once.fis the file you’re writing to. And it returns the number of blocks written (so1on success and0on failure).For completeness, I should note that’s only one of the approaches to use of
fwrite(). It may be a bit more semantic to use something like:but then the
fwrite()may actually write only part of the data, and you will need to care about that. In other words, if it returned less than N, you’d have to retry writing the remaining part.