Skip to content

FileTell

Description

Return the current file offset.

Success

Returns the offset (>= 0).

Failure

Returns -1 on error.

Usage example (Cross-references)

Usage examples (Cross-references)
    }
    
    i64 FileTell(File *f) {
        return FileSeek(f, 0, FILE_SEEK_CUR);
    }
        ok              = ok && (FileWrite(&f, "x", 1) == -1);
        ok              = ok && (FileSeek(&f, 0, FILE_SEEK_SET) == -1);
        ok              = ok && (FileTell(&f) == -1);
    
        FileRemove(&path);
    // observable values.
    bool test_write_seek_read_roundtrip(void) {
        WriteFmt("Testing FileWrite/FileSeek/FileTell/FileRead round-trip\n");
    
        DefaultAllocator alloc      = DefaultAllocatorInit();
    
        // After writing 8 bytes the cursor is at offset 8.
        ok = ok && (FileTell(&f) == 8);
    
        // Seek to absolute offset 2 and confirm Tell agrees.
        // Seek to absolute offset 2 and confirm Tell agrees.
        ok = ok && (FileSeek(&f, 2, FILE_SEEK_SET) == 2);
        ok = ok && (FileTell(&f) == 2);
    
        char got[4] = {0};
    
        // Cursor advanced by the 4 read bytes (2 -> 6).
        ok = ok && (FileTell(&f) == 6);
    
        // Seek to end yields the file length.
        ok = ok && (v == 42);
        // Seekable branch advances to exactly the consumed bytes ("42").
        ok = ok && (FileTell(&f) == 2);
    
        m4_cleanup(&f, &path);
        FReadFmt(&f, "{}", v);
        ok = ok && (v == 53);
        ok = ok && (FileTell(&f) == 2);
    
        m4_cleanup(&f, &path);
        FReadFmt(&f, "{}", v);
        ok = ok && (v == 1234);         // untouched
        ok = ok && (FileTell(&f) == 0); // no advance
    
        m4_cleanup(&f, &path);
Last updated on