Well, Python is my friend now.
The Python read()
function, which is basically the same as fread()
also accepts a length parameter and 1024 is a sensible number to give it.
You can read the entire file in python but you probably shouldn’t do that. In the real world, your software will be given files larger than it can handle and it’s important to have logic in place to process the file incrementally instead of all at once. Honestly, it’s what makes this particular problem so challenging.
It’s definitely possible to write a C program that reads the entire file. You just won’t find many examples of that because it’s a bad idea.
You said you used an “AI to assist” but it failed? What were you using? I pasted it into ChatGPT 4 and it found eight problems - including the buffer issue.
Max_P@lemmy.max-p.me 1 year ago
It’s really not all that bad considering that you’re pretty close to talking directly to the kernel. The C language itself is pretty simple: it doesn’t come with any built-in functions or anything. It’s code, it gets compiled to a binary. But we need a way to talk to the operating system, and that’s where the C standard library or libc comes in. It contains a standard set of operating system utilities and functions that you can expect most operating systems to implement. The C standard library is pretty old, from an era when a megabyte of RAM was a lot of RAM and where every CPU cycle counted. So it doesn’t do a whole lot: it’s supposed to be the building block for everything, it needs to be fast and flexible and as small as possible.
What you want is nicer C libraries that makes those things easier to work with. Or to write a function whenever you encounter something repetitive, in this case it’s probably 20-25 lines to properly implement the necessary
malloc
,fopen
andfread
then you’re done with it forever. Copy paste it in your next project, or make yourself a library of C gadgets you accumulate over time.If you’re looking for an experience close to C but a little more batteries included, you might want to consider C++ which does still get new modern features to it. Most C code is valid C++ code, it’s not like learning an entirely new language. Reading a whole file for example is a lot more straightforward (source):
You can, however, appreciate how close you’re working with the hardware and kernel, just like the Arduino: the interface with the kernel (on Linux) to read a file is basically:
That’s the whole, unabridged thing. From your perspective, you made a syscall and the data magically appeared in your memory. The kernel has no idea what your intentions are, you just ask it to read stuff and it gives it to you. If you misplace it or tell it to read too much or not enough, it doesn’t know, it obliges. If you want to read the whole file, it’s your job to keep asking the kernel for more, just how the kernel will ask the hard drive for more. That’s exactly what
read(fd, buffer, bufsize)
does, nothing more, nothing less. It’s the smallest unit of work you can ask, and it’s efficient. The data possibly went straight from your NVMe to the memory of your program. Copied exactly once.And this is why
fread
is the way it is, as are most libc things. Interestingly, even that has become too inefficient, and now we have things likeio_uring
, that’s even more complicated to use but really really fast.It’s a whole rabbit hole and that’s why we use libraries and avoid using those too much.