C:将数据写入内存映射文件显示垃圾

标签 c

我正在尝试将数据写入内存映射文件,但它显示为垃圾。不知道我做错了什么。我正在创建一个内存映射文件并将 int 写入其中。我看到垃圾输出。

我以读/写方式打开的文件,理想情况下我希望其他进程读取写入其中的数据

下面的代码

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>

#define FILEPATH "test"
#define NUMINTS  (1000)
#define FILESIZE (NUMINTS * sizeof(int))

int main(int argc, char *argv[])
{
    int i;
    int fd;
    int result;
    int *map;  /* mmapped array of int's */

    /* Open a file for writing.
     *  - Creating the file if it doesn't exist.
     *  - Truncating it to 0 size if it already exists. (not really needed)
     *
     * Note: "O_WRONLY" mode is not sufficient when mmaping.
     */
    fd = open(FILEPATH, O_RDWR | O_CREAT | O_TRUNC, (mode_t)0600);
    if (fd == -1) {
    perror("Error opening file for writing");
    exit(EXIT_FAILURE);
    }

    /* Stretch the file size to the size of the (mmapped) array of ints
     */
    result = lseek(fd, FILESIZE-1, SEEK_SET);
    if (result == -1) {
    close(fd);
    perror("Error calling lseek() to 'stretch' the file");
    exit(EXIT_FAILURE);
    }

    /* Something needs to be written at the end of the file to
     * have the file actually have the new size.
     * Just writing an empty string at the current file position will do.
     *
     * Note:
     *  - The current position in the file is at the end of the stretched
     *    file due to the call to lseek().
     *  - An empty string is actually a single '\0' character, so a zero-byte
     *    will be written at the last byte of the file.
     */
    result = write(fd, "", 1);
    if (result != 1) {
    close(fd);
    perror("Error writing last byte of the file");
    exit(EXIT_FAILURE);
    }

    /* Now the file is ready to be mmapped.
     */
    map = mmap(0, FILESIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
    if (map == MAP_FAILED) {
    close(fd);
    perror("Error mmapping the file");
    exit(EXIT_FAILURE);
    }

    /* Now write int's to the file as if it were memory (an array of ints).
     */
    for (i = 1; i <=10; ++i) {
        map[i] = i;
    }

    /* Don't forget to free the mmapped memory
     */
    if (munmap(map, FILESIZE) == -1) {
    perror("Error un-mmapping the file");
    /* Decide here whether to close(fd) and exit() or not. Depends... */
    }

    /* Un-mmaping doesn't close the file, so we still need to do that.
     */
    close(fd);
    return 0;
}

最佳答案

test 的内容和我预期的一样:

$ hexdump -C test
00000000  00 00 00 00 01 00 00 00  02 00 00 00 03 00 00 00  |................|
00000010  04 00 00 00 05 00 00 00  06 00 00 00 07 00 00 00  |................|
00000020  08 00 00 00 09 00 00 00  0a 00 00 00 00 00 00 00  |................|
00000030  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00000fa0

有你的 10 个整数。

当然,在看起来像垃圾的文本编辑器中,因为它是整数的二进制值,而不是 ASCII “1”, "2"...

关于C:将数据写入内存映射文件显示垃圾,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43360713/

相关文章:

c++ - 链接问题与 g++ 有关,但与 cc 无关

c - 从模块的外部上下文中正确隐藏函数

c - 出现编译错误

c - 为什么这个指针交换在字符串反转中会失败?

c - 为什么我的计算机在使用并行代码时没有显示加速?

python - 如何优化 cython 中的 jonswap 频谱

c - 使用有限的字符串自动填充数组元素

c - C 中的字符串数组操作

c - 是否有 "null"printf 代码不打印任何内容,用于跳过参数?

android - 在 Android/OpenSL 中,我可以释放单个缓冲区并将其排入 bufferQueue 中,而不是清除整个缓冲区吗?