So the scenario is this. I have to implement minimal stdio library in C and the unit test that fails writes binary data to a file and then calls the fread function from my library to read that data. And the problem is that when using memcmp
on the initial buffer and the data read through my API, they differ.
Have to mention that on printable ASCII characters, it works just fine. The problem is with binary data.
FUNC_DECL_PREFIX int so_fgetc(SO_FILE *stream) {
ssize_t bytes_read = 0;
unsigned char c = 0;
if (stream == NULL)
return SO_EOF;
if (stream->fd < 0) {
stream->flag_error = 1;
return SO_EOF;
}
if (stream->buffer_size <= 0) {
bytes_read = read(stream->fd, stream->buffer, (ssize_t) BUFFER_SIZE-1);
if (bytes_read == 0) {
stream->flag_eof = 1;
return SO_EOF;
} else if (bytes_read < 0) {
stream->flag_error = 1;
return SO_EOF;
}
stream->buffer_size = bytes_read;
}
c = (unsigned char) stream->buffer[stream->current_index];
char *temp_buffer = NULL;
temp_buffer = calloc(BUFFER_SIZE, sizeof(char));
if (!temp_buffer) {
stream->flag_error = 1;
perror("Memory allocation");
return SO_EOF;
}
if (stream->buffer != NULL) {
strcpy(temp_buffer, stream->buffer);
strcpy(stream->buffer, temp_buffer+1);
}
free(temp_buffer);
if (stream->buffer == NULL) {
stream->flag_error = 1;
return SO_EOF;
}
stream->buffer_size -= 1;
stream->cursor_position += 1;
stream->last_operation = 0;
return (int) c;
}
That was the function that reads data in a buffer and provides data from the buffer one char at a time
FUNC_DECL_PREFIX
size_t so_fread(void *ptr, size_t size, size_t nmemb, SO_FILE *stream) {
if (size <= 0 || nmemb <= 0 || !stream) return 0;
ssize_t bytes_read = 0;
char *p = NULL;
p = ptr;
int i = 0, c = 0;
for(i=0; i<nmemb*size; i++) {
c = so_fgetc(stream);
if (so_ferror(stream)) {
return 0;
}
if (c == SO_EOF) return bytes_read;
bytes_read += 1;
memcpy((void*) p+i, (char*) &c, 1);
}
return bytes_read;
}
And that is the fread i implemented.
I suspect there is a problem casting the data and storing it to the buffer but i cannot figure it out.
Copyright Notice:Content Author:「Andrei Plesoianu」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/60771956/problems-writing-binary-data-to-a-void-buffer-in-c