![]() ![]() ![]() Let's see what POSIX documentation for fopen(3) has to say: What we see here is actually a documented behavior. But how can fread(3) actually write anything?įreeBSD - nope: ~/data/dev/tests/fopen_fwrite]$ cat hello | xxd There's also a lot of stuff that we didn't write. What the hell is going on? We wanted to write 6 bytes, but the size of our new file is 0x106 bytes. Unless the fopen family of functions uses two separate pointers, read and write, in which case we should read what we've just written. Then, it should read some bytes from this file, but most probably the read operation will fail, since obviously we're at the end of the file and there's nothing to read. This program should write 6 bytes into the hello file. So let's write a simple program that will create a read/write stream in the form of a file named hello. This family of functions are using special internal buffers, so we don't risk performing syscalls too often than what's necessary. ![]() In C, we can use fopen(3) to open the file, fread(3) to read from it, and fwrite(3) to write to the file. Well, who is better at those things than the C programmer? But sometimes we want pure speed and hand-crafted optimalization. Multiple frameworks like Qt offer classes like QFile that allow the programmer to read and write to files. In C++ there are some ways you can perform file I/O: std::ifstream is one example. Is it? Well, actually it is, especially if you're using your programming language in a non-recommended way. Everyone says that programming is complicated. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |