Public Group

# Reading data from a char array

This topic is 4972 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

##### Share on other sites
for a char array you do:

unsigned char *buffer;

unsigned char firstUCHAR = buffer[0];
unsigned char secondUCHAR = buffer[1];

unsigned char chars[32];

for(int i = 0; i < 32; i++) chars = buffer;

Or you can do it like this

unsigned char *bp = buffer;

unsigned char firstUCHAR = *bp;
unsigned char secondUCHAR = *(bp + 1);

EDIT: Or do you want to read the file like you read a uchar array?

/MindWipe

##### Share on other sites
True sscanf would do read a uchar array like a file.

/MindWipe

##### Share on other sites
In general, stdio operations that start with 'f' and operate on files, have equivalents that start with 's' and operate on strings. Hence sscanf, for example. However, there is no 'sread' or 'swrite' largely because of the binary (as opposed to text) nature of those operations; since they work with raw bytes, the expectation is that you do equivalent things with strings by just doing pointer arithmetic (or its higher-level cousin, array indexing).

(The reasons you can't just do pointer arithmetic on a FILE * are presumably (a) it's likely a typedef for some structure that hides indirection from you, such that you'd be writing to the wrong place; (b) files aren't random-access in the same way that memory is, so pointer arithmetic allows operations that can't cleanly be implemented.)

Anyway... in C++, you should consider reading text data using the stream extraction '>>' operator, and binary data by... I forgot the exact details, but there is a nice way with the STL to dump a file into a vector<char> ... someone want to provide it? :)

##### Share on other sites
Quote:
 Original post by ZahlmanAnyway... in C++, you should consider reading text data using the stream extraction '>>' operator, and binary data by... I forgot the exact details, but there is a nice way with the STL to dump a file into a vector ... someone want to provide it? :)

Sure:
#include <vector>#include <fstream>#include <iterator>int main(int, char **) {  std::ifstream ifs("temp.cpp");    std::vector<char> vector_with_files_contents( (std::istreambuf_iterator<char>(ifs)),                                                (std::istreambuf_iterator<char>()) );  return 0;}

But why is this relevant?

##### Share on other sites
Hey all, thanks for the reply.. sorry it took me so long to respond (school, etc)

I'm actually trying to read in compressed world map data which is in the format:
int width, int height, unsigned char data[width*height]
from a compressed file. So I take huffman compression algo, and load the file, decompress it. I then have an array of data, but i would like to load the width and height right from that binary array, instead of resaving it to a file first, then loading it.

If this isnt possible.. I will make my own binary numeral format (char[0]*255+char[1] or similiar)

Thanks again
~Zix~

##### Share on other sites
Ok here is a "sread" function that does what fread does, except on a byte array (could be char, but let's be explicit). Please note I am not actually suggesting this function is the best way to do this, my hope is that by understanding it you can solve your problem.

size_t sread(void *buffer, size_t size, size_t count, BYTE *stream, size_t *position, size_t length) {        if(*position + (size * count) > length) {        count = (length - *position) / size;    }    memcpy(buffer, stream + *position, size * count);    *position = *position + (size * count);    return count;}

Now it's important to understand that there are a few differences between a file stream and a c array. The first difference is that streams store their own size, c arrays do not. sread gets around this by passing in the length of the array as the "length" param. You could also use a more complex structure such a a vector which tracks it's own length. The if statement modifies count to prevent overflowing the arrray.

A second difference is that files track a file pointer which is updated after every read. Since arrays don't track the last read you made, sread does it with the position param. Note that position is a pointer, so changes made to it will not be limited to the scope of the function.

The real meat of the function is the call to memcpy, which actually copies the data from the array to your buffer.

Now, once again I'm not claiming this is the best way to do this. This is all c code and it's written for clarity, not efficiency. My goal here was to give you exactly what you asked for to help you understand how c arrays work compared to c file streams.

##### Share on other sites
Thanks a bunch solias, i'll try it out.
~zix~

• 18
• 11
• 16
• 9
• 50
• ### Forum Statistics

• Total Topics
631396
• Total Posts
2999783
×