这是我试图理解的代码:
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
int main(void){
unsigned long word;
ssize_t nr;
int file = open("koray.txt",O_RDONLY);
nr = read(file,&word,sizeof(unsigned long));
printf("%li\n",word);
}
Run Code Online (Sandbox Code Playgroud)
koray.txt只有1个字符k.
当我运行程序时,我看到:
koray@koray-VirtualBox:~$ ./a.out
4195435
Run Code Online (Sandbox Code Playgroud)
这个大价值是多少?
word变量中会有随机垃圾,因为你从未初始化它.然后read只能从文件中获取一个字节(nr可能返回1,你应该检查!),它保存了一个字节,但word变量仍然有3-7个字节的未初始化的垃圾进行打印.