ksrfiles : do not null-terminate buffer when reading a file, and fix file path when testing.

This commit is contained in:
Madeorsk 2023-02-12 16:50:33 +01:00
parent de5075571b
commit 71a16f22f1
2 changed files with 4 additions and 6 deletions

View File

@ -57,12 +57,11 @@ ksrbuffer* read_file(FILE *file)
// getting back to the beginning of the file. // getting back to the beginning of the file.
fseek(file, 0, SEEK_SET); fseek(file, 0, SEEK_SET);
// allocating a buffer corresponding to the file size, plus the end of string char. // allocating a buffer corresponding to the file size.
ksrbuffer *buffer = ksrbuffer_new(filesize + 1); ksrbuffer *buffer = ksrbuffer_new(filesize);
// reading the file into the buffer. // reading the file into the buffer.
fread(buffer->bytes, 1, buffer->length, file); fread(buffer->bytes, 1, buffer->length, file);
buffer->bytes[buffer->length - 1] = 0; // 0-terminated string.
return buffer; // returning allocated buffer. return buffer; // returning allocated buffer.
} }

View File

@ -11,13 +11,12 @@ int main(void)
write_file("ksrtesttesttmp/tmp/test.tmp", buffer); write_file("ksrtesttesttmp/tmp/test.tmp", buffer);
// try to read the previously written file, and checking that the buffer has been read properly. // try to read the previously written file, and checking that the buffer has been read properly.
ksrbuffer *read_buffer = read_file_from_path("test.tmp"); ksrbuffer *read_buffer = read_file_from_path("ksrtesttesttmp/tmp/test.tmp");
assert(read_buffer->length == 13); assert(read_buffer->length == 12);
assert(read_buffer->bytes[0] == 'H'); assert(read_buffer->bytes[0] == 'H');
assert(read_buffer->bytes[3] == 'l'); assert(read_buffer->bytes[3] == 'l');
assert(read_buffer->bytes[7] == 'o'); assert(read_buffer->bytes[7] == 'o');
assert(read_buffer->bytes[11] == '!'); assert(read_buffer->bytes[11] == '!');
assert(read_buffer->bytes[12] == 0);
// delete test directory. // delete test directory.
system("rm -Rf ksrtesttesttmp"); system("rm -Rf ksrtesttesttmp");