POSIX 호환 C로 작성된 코드가 있는데 올바르게 작동하지 않는 것 같습니다. 목표는 Linux/BSD/Darwin 커널의 난수 생성기에 대한 인터페이스 인/dev/random을 읽고 파일에 기록 된 바이트를 출력하는 것입니다. 나는 내가 모든 것을 뒤덮었 음을 분명히 알기 때문에 내가 간과하는 것을 확실히 모른다. 어쨌든 여기에 있습니다 :/dev/random에서 바이트 읽기가 실패합니다.
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에서 개발되고 있지만 리눅스 나 FreeBSD에서도 작동해서는 안된다.
도움이된다면, 내가 포함 한 다음 헤더 :
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
작동하지 않는 기능 - 오류가 무엇인가요? – Mark
'read()'/'write()'호출에 오류 검사를 추가하고'strerror (errno)'를 사용하여 오류를보고합니다. 게다가 당신은 'bytes'를 읽고 쓰지 않고'int's를 읽고 쓰고 있습니다. – trojanfoe
그것은 단순히 조용히 실패합니다 ... OS는 파일 크기가 동일하다고보고하고 파일을 열 때 아무 것도 변경되지 않았습니다. 텍스트 파일에서이를 테스트하여 변경 사항을 감지 할 수있었습니다. – SevenBits