Skip to content

FileWrite

Description

Write up to n bytes from buf. Same short-write semantics as FileRead.

Success

Returns the number of bytes written (>= 0).

Failure

Returns -1 on error.

Usage example (Cross-references)

Usage examples (Cross-references)
    
        File out = (type == LOG_MESSAGE_TYPE_INFO) ? FileFromFd(1) : FileFromFd(2);
        (void)FileWrite(&out, StrBegin(&full), StrLen(&full));
    
    #if FEATURE_SYS_BACKTRACE && (!defined(LOG_NO_BACKTRACE) || !LOG_NO_BACKTRACE)
            // boundary; pass at the call site, no intermediate variable.
            FormatStackTrace(&trace, frames, n, ALLOCATOR_OF(&h));
            (void)FileWrite(&out, StrBegin(&trace), StrLen(&trace));
            StrDeinit(&trace);
        }
    }
    
    i64 FileWrite(File *f, const void *buf, u64 n) {
        if (!FileIsOpen(f) || !buf) {
            return -1;
        }
    
        if (ok && StrLen(&out) > 0 && FileWrite(stream, StrBegin(&out), StrLen(&out)) != (i64)StrLen(&out)) {
            LOG_ERROR("Failed to write formatted output");
            ok = false;
        }
        size n       = ZstrLen(text);
        i64  written = FileWrite(&f, text, (u64)n);
        bool ok      = (written == (i64)n);
        FileClose(&f);
        char scratch[8] = {0};
        ok              = ok && (FileRead(&f, scratch, sizeof(scratch)) == -1);
        ok              = ok && (FileWrite(&f, "x", 1) == -1);
        ok              = ok && (FileSeek(&f, 0, FILE_SEEK_SET) == -1);
        ok              = ok && (FileTell(&f) == -1);
    // observable values.
    bool test_write_seek_read_roundtrip(void) {
        WriteFmt("Testing FileWrite/FileSeek/FileTell/FileRead round-trip\n");
    
        DefaultAllocator alloc      = DefaultAllocatorInit();
    
        Zstr msg   = "abcdefgh"; // 8 bytes
        i64  wrote = FileWrite(&f, msg, 8);
        bool ok    = (wrote == 8);
        File w = FileOpen(&path, "w");
        ok     = ok && FileIsOpen(&w);
        ok     = ok && (FileWrite(&w, "new", 3) == 3);
        FileClose(&w);
        File a = FileOpen(&path, "a");
        ok     = ok && FileIsOpen(&a);
        ok     = ok && (FileWrite(&a, "tail", 4) == 4);
        FileClose(&a);
        ok     = ok && FileIsOpen(&f);
        // A read-only handle must reject writes.
        ok = ok && (FileWrite(&f, "X", 1) == -1);
        FileClose(&f);
        ok     = ok && FileIsOpen(&f);
        // "r+" must permit writes (does not truncate). Overwrite first 3 bytes.
        ok = ok && (FileWrite(&f, "AAA", 3) == 3);
        FileClose(&f);
        // breaking at least one of these.
        ok = ok && (got == 7) && (MemCompare(buf, "read-me", 7) == 0);
        ok = ok && (FileWrite(&f, "X", 1) == -1);
        FileClose(&f);
            return false;
        }
        bool ok = (FileWrite(&owner, "hello", 5) == 5);
    
        // Borrow the same fd and close the borrowed wrapper. owns==false means
    
        // Owner's fd must still be live.
        ok = ok && (FileWrite(&owner, "world", 5) == 5);
        FileClose(&owner);
        }
        // FileWrite must report exactly 12 bytes written.
        bool ok = (FileWrite(&f, "ABCDEFGHIJKL", 12) == 12);
        ok      = ok && (FileSeek(&f, 0, FILE_SEEK_SET) == 0);
            return false;
        }
        bool ok = (FileWrite(&f, "0123456789", 10) == 10);
        ok      = ok && (FileSeek(&f, 0, FILE_SEEK_SET) == 0);
        // Advance 3 from current (0 -> 3).
            BufData(&src)[i] = (u8)('A' + (i % 26));
        }
        ok = ok && (FileWrite(&f, BufData(&src), (u64)N) == (i64)N);
        FileClose(&f);
    
        // Round-trip a payload through the freshly created temp file.
        ok           = ok && (FileWrite(&f, "temp-data", 9) == 9);
        ok           = ok && (FileSeek(&f, 0, FILE_SEEK_SET) == 0);
        char buf[10] = {0};
    
        // They are independent files: writing to one does not affect the other.
        ok = ok && (FileWrite(&f1, "one", 3) == 3);
        ok = ok && (FileWrite(&f2, "twotwo", 6) == 6);
        FileClose(&f1);
        // They are independent files: writing to one does not affect the other.
        ok = ok && (FileWrite(&f1, "one", 3) == 3);
        ok = ok && (FileWrite(&f2, "twotwo", 6) == 6);
        FileClose(&f1);
        FileClose(&f2);
        if (!FileIsOpen(&f))
            return f;
        FileWrite(&f, content, len);
        FileSeek(&f, 0, FILE_SEEK_SET);
        return f;
        if (!FileIsOpen(&f))
            return f;
        FileWrite(&f, content, len);
        FileSeek(&f, 0, FILE_SEEK_SET);
        return f;
        bool             ok    = FileIsOpen(&f);
        if (ok) {
            FileWrite(&f, "42", 2);
            FileSeek(&f, 0, FILE_SEEK_SET);
            i32 v = 0;
        bool             ok    = FileIsOpen(&f);
        if (ok) {
            FileWrite(&f, "xx", 2);
            FileSeek(&f, 0, FILE_SEEK_SET);
            i32 v = 123;
        if (!FileIsOpen(&f))
            return false;
        bool ok = (u64)FileWrite(&f, data, size) == size;
        FileClose(&f);
        return ok;
            u64 n = ZstrLen(body);
            if (n > 0) {
                FileWrite(&f, body, n);
            }
            FileClose(&f);
        if (!FileIsOpen(&f))
            return false;
        bool ok = FileWrite(&f, data, size) == (i64)size;
        FileClose(&f);
        return ok;
        if (!FileIsOpen(&f))
            return false;
        bool ok = FileWrite(&f, data, size) == (i64)size;
        FileClose(&f);
        return ok;
        if (!FileIsOpen(&f))
            return false;
        bool ok = FileWrite(&f, data, size) == (i64)size;
        FileClose(&f);
        return ok;
        bool wrote_ok = true;
        for (int i = 0; i < 200 && wrote_ok; ++i) {
            i64 w    = FileWrite(&f, filler, (u64)ZstrLen(filler));
            wrote_ok = (w == (i64)ZstrLen(filler));
        }
        Zstr late = "deadbeef000-deadbeef100 r-xp 0 0:0 0 /late\n";
        if (wrote_ok) {
            i64 w    = FileWrite(&f, late, (u64)ZstrLen(late));
            wrote_ok = (w == (i64)ZstrLen(late));
        }
    
        Zstr payload = "hello-body";
        ok           = ok && (FileWrite(&f, payload, ZstrLen(payload)) == (i64)ZstrLen(payload));
        FileClose(&f);
        bool ok      = FileIsOpen(&f);
        Zstr payload = "file-payload-long-enough-to-heap-allocate-body";
        ok           = ok && (FileWrite(&f, payload, ZstrLen(payload)) == (i64)ZstrLen(payload));
        FileClose(&f);
Last updated on