我在Ubuntu 14.04中使用GCC编译了一个C代码,除其他外,创建一个文件,写入8个字节,然后关闭它.该代码在我的i7 64位pc中运行良好.问题是,当我在32位架构(raspberry pi 2 with raspbian)上编译和执行我的代码时,此操作会创建一个大小为4294967304字节的文件.我不知道出了什么问题.奇怪的是,我的程序创建3个文件的方式应该是空的,但每个文件的大小为4 Gb,而我的可用内存只有8 Gb.这让我相信我打破了文件系统(ext4),但我不知道为什么.代码是这样的:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main(int argc, char *argv[])
{
int outImpFile = open(argv[1], O_RDWR | O_LARGEFILE | O_CREAT, 0);
long long int imp = 89;
write(outImpFile, &imp, sizeof(long long int));
close(outImpFile);
}
Run Code Online (Sandbox Code Playgroud)
当我用ghex打开创建的文件时,我只看到8个字节,但是当我使用hexdiff时,开头有很多空字节,最后写了8个字节.
奇怪错误的原因是缺少正确的标题,特别是<unistd.h>.这会导致参数write()类型不正确,尤其是count参数.
试试你的程序的更正版本:
#define _POSIX_C_SOURCE 200809L
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
int main(int argc, char *argv[])
{
long long int value = 89;
ssize_t n;
int descriptor;
if (argc != 2) {
fprintf(stderr, "\nUsage: %s FILENAME\n\n", argv[0]);
return EXIT_FAILURE;
}
descriptor = open(argv[1], O_RDWR | O_CREAT, 0666);
if (descriptor == -1) {
fprintf(stderr, "%s: %s.\n", argv[1], strerror(errno));
return EXIT_FAILURE;
}
n = write(descriptor, &value, sizeof value);
if (n != sizeof value) {
if (n == -1)
fprintf(stderr, "%s: %s.\n", argv[1], strerror(errno));
else
fprintf(stderr, "%s: Partial write (%zd bytes).\n", argv[1], n);
close(descriptor);
return EXIT_FAILURE;
}
if (close(descriptor)) {
fprintf(stderr, "%s: Error closing file.\n", argv[1]);
return EXIT_FAILURE;
}
printf("%zd bytes written successfully to '%s'.\n", n, argv[1]);
return EXIT_SUCCESS;
}
Run Code Online (Sandbox Code Playgroud)
始终,始终在编译代码时启用警告.使用GCC,我使用gcc -Wall -O2(对于警告和编译器优化的结果).我热烈建议你这样做.如果将上面保存为fixed.c,则使用编译它
gcc -Wall -O2 fixed.c -o fixed-example
Run Code Online (Sandbox Code Playgroud)
并使用它运行它
./fixed-example output-file
Run Code Online (Sandbox Code Playgroud)
如果需要针对例如数学库进行编译,请在-o标志之前包含选项; 即gcc -Wall -O2 fixed.c -lm -o fixed-example对于上述计划.选项的顺序对GCC很重要.