能否让第三方库的文件写入函数改为写入内存而非磁盘?
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
mkstempgenerates a unique filename to avoid conflicts with other processes.unlinkdeletes the file immediately after use—no leftover files cluttering/dev/shm./dev/shmis 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_TEMPORARYtells Windows to keep the file in memory as much as possible, writing to disk only if necessary.FILE_FLAG_DELETE_ON_CLOSEensures the file is deleted as soon as the last handle to it is closed, so no cleanup is left behind.- If
manipulateAndWriteuses ANSI (char*) paths instead of wide strings (wchar_t*), converttemp_fileto ANSI withWideCharToMultiByte.
General Considerations
- These methods work because
manipulateAndWriteonly 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

