我有一段用符合 POSIX 的 C 语言编写的代码,但它似乎无法正常工作。目标是从 /dev/random(Linux/BSD/Darwin 内核的随机数生成器的接口)读取并将写入的字节输出到文件中。我不太确定我忽略了什么,因为我确信我已经覆盖了所有领域。无论如何,这里是:
int incinerate(int number, const char * names[]) {
if (number == 0) {
// this shouldn't happen, but if it does, print an error message
fprintf(stderr, "Incinerator: no input files\n");
return 1;
}
// declare some stuff we'll be using
long long lengthOfFile = 0, bytesRead = 0;
int myRandomInteger;
// open the random file block device
int zeroPoint = open("/dev/random", O_RDONLY);
// start looping through and nuking files
for (int i = 1; i < number; i++) {
int filePoint = open(names[i], O_WRONLY);
// get the file size
struct stat st;
stat(names[i], &st);
lengthOfFile = st.st_size;
printf("The size of the file is %llu bytes.\n", lengthOfFile);
while (lengthOfFile != bytesRead) {
read(zeroPoint, &myRandomInteger, sizeof myRandomInteger);
write(filePoint, (const void*) myRandomInteger, sizeof(myRandomInteger));
bytesRead++;
}
close(filePoint);
}
return 0;
}
有任何想法吗?这是在 OS X 上开发的,但我看不出它为什么不能在 Linux 或 FreeBSD 上运行。
如果有帮助,我已包含以下标题:
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>