You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

能否让第三方库的文件写入函数改为写入内存而非磁盘?

How to Redirect manipulateAndWrite to Memory Instead of Disk

Great question! Since you can’t modify or reimplement manipulateAndWrite, your best bet is to use platform-specific in-memory file mechanisms that present a regular file path to the function—so it behaves exactly like it’s writing to disk, but the data stays in memory (or a memory-backed filesystem) to avoid physical disk I/O. Here’s how to pull this off on common systems:

Linux/Unix (including macOS)

On these systems, you can use the /dev/shm tmpfs filesystem—a dedicated memory-backed storage area that looks like a regular directory to applications. Files stored here reside in RAM (and may swap to disk only if memory is tight), so there’s no physical disk write.

Example Code

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main() {
    FOO_DATA your_data = { /* Initialize your FOO_DATA here */ };
    char temp_path[] = "/dev/shm/foo_temp_XXXXXX"; // Template for unique filename

    // Create a unique temporary file in /dev/shm
    int fd = mkstemp(temp_path);
    if (fd == -1) {
        perror("Failed to create temp file");
        return EXIT_FAILURE;
    }
    close(fd); // Close the file descriptor—manipulateAndWrite will open it itself

    // Call the function with the in-memory file path
    int result = manipulateAndWrite(temp_path, &your_data);
    if (result != 0) {
        fprintf(stderr, "manipulateAndWrite failed\n");
        unlink(temp_path); // Clean up even if the function fails
        return EXIT_FAILURE;
    }

    // Read the result from the temp file into your memory buffer
    FILE *fp = fopen(temp_path, "r");
    if (fp == NULL) {
        perror("Failed to open temp file for reading");
        unlink(temp_path);
        return EXIT_FAILURE;
    }
    // Add your read logic here (e.g., fread into a buffer)
    fclose(fp);

    // Clean up: delete the temporary file
    unlink(temp_path);
    return EXIT_SUCCESS;
}

Key Notes

  • mkstemp generates a unique filename to avoid conflicts with other processes.
  • unlink deletes the file immediately after use—no leftover files cluttering /dev/shm.
  • /dev/shm is typically sized to half your system’s RAM by default; if your result is larger than this, it may swap to disk, but this is still faster than writing to a physical drive.

Windows

Windows doesn’t have a direct equivalent to /dev/shm, but you can create temporary files that the OS will prioritize keeping in memory (and auto-delete when done). Use FILE_ATTRIBUTE_TEMPORARY and FILE_FLAG_DELETE_ON_CLOSE to minimize disk I/O and ensure cleanup.

Example Code

#include <stdio.h>
#include <windows.h>

int main() {
    FOO_DATA your_data = { /* Initialize your FOO_DATA here */ };
    WCHAR temp_path[MAX_PATH];
    WCHAR temp_file[MAX_PATH];

    // Get the system temporary directory
    if (GetTempPathW(MAX_PATH, temp_path) == 0) {
        fprintf(stderr, "GetTempPath failed\n");
        return EXIT_FAILURE;
    }

    // Generate a unique temporary filename
    if (GetTempFileNameW(temp_path, L"FOO", 0, temp_file) == 0) {
        fprintf(stderr, "GetTempFileName failed\n");
        return EXIT_FAILURE;
    }

    // Create the file with memory-friendly flags
    HANDLE hFile = CreateFileW(
        temp_file,
        GENERIC_WRITE | GENERIC_READ,
        0,
        NULL,
        CREATE_ALWAYS,
        FILE_ATTRIBUTE_TEMPORARY | FILE_FLAG_DELETE_ON_CLOSE,
        NULL
    );
    if (hFile == INVALID_HANDLE_VALUE) {
        fprintf(stderr, "CreateFile failed\n");
        DeleteFileW(temp_file); // Clean up the empty temp file
        return EXIT_FAILURE;
    }
    CloseHandle(hFile);

    // Call manipulateAndWrite (convert to ANSI if the function uses char* instead of wchar_t*)
    int result = manipulateAndWrite(temp_file, &your_data);
    if (result != 0) {
        fprintf(stderr, "manipulateAndWrite failed\n");
        DeleteFileW(temp_file);
        return EXIT_FAILURE;
    }

    // Read the result into your memory buffer
    FILE *fp = _wfopen(temp_file, L"r");
    if (fp == NULL) {
        fprintf(stderr, "Failed to open temp file for reading\n");
        DeleteFileW(temp_file);
        return EXIT_FAILURE;
    }
    // Add your read logic here
    fclose(fp);

    // The file will auto-delete when all handles are closed, but explicitly delete to be safe
    DeleteFileW(temp_file);
    return EXIT_SUCCESS;
}

Key Notes

  • FILE_ATTRIBUTE_TEMPORARY tells Windows to keep the file in memory as much as possible, writing to disk only if necessary.
  • FILE_FLAG_DELETE_ON_CLOSE ensures the file is deleted as soon as the last handle to it is closed, so no cleanup is left behind.
  • If manipulateAndWrite uses ANSI (char*) paths instead of wide strings (wchar_t*), convert temp_file to ANSI with WideCharToMultiByte.

General Considerations

  • These methods work because manipulateAndWrite only cares about valid file paths—they don’t require modifying the function or its logic.
  • For cross-platform code, use preprocessor macros (e.g., #ifdef __linux__, #ifdef _WIN32) to select the appropriate implementation.
  • Always handle errors and clean up temporary files to avoid resource leaks.

内容的提问来源于stack exchange,提问作者Ben L

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 09:55:29