I am currently working on a toy python implementation. I was previously working on a Mac, but when I moved my project over to a Windows machine, I encountered an issue with reading file contents.
This is my file:
But for some reason, when I read it into memory, 12
is being appended to the end, as seen in the debug memory dump.
I am completely clueless as to what is causing this. Here is my source core for reading the file:
// OS-specific headers
#ifdef _WIN32
#include <io.h>
#define open _open
#define read _read
#else
#include <unistd.h>
#endif
namespace Parse
{
// Buffers the source file and creates a Lexer instance.
Lexer Lexer::from_src_file(const char *src_file_name)
{
// Get a file descriptor for the source file.
errno = 0;
int fd = open(src_file_name, O_RDONLY);
if (fd == -1)
throw std::runtime_error{strerror(errno)};
// Get the file information from the OS.
struct stat file_stat {
};
errno = 0;
if (fstat(fd, &file_stat) == -1)
throw std::runtime_error{strerror(errno)};
size_t file_size = file_stat.st_size;
char *buffer = new char[file_size + 1];
// Read the file into the buffer in chunks.
constexpr int CHUNK_SIZE = 1024;
size_t offset = 0;
while (true) {
size_t bytes_read = read(fd, buffer + offset, CHUNK_SIZE);
if (!bytes_read)
break;
offset += bytes_read;
}
buffer[file_size] = EOF;
// When we're done reading, close the file.
close(fd);
return Lexer{buffer, &buffer[file_size], src_file_name};
}
Any ideas as to what may be causing this?
Thanks in advance.