我已经编写了以下代码,用于使用 read 函数从“file.dat”中读取
#include<stdio.h>
int main()
{
int fd,xr;
char b;
if ((fd=open("file.dat"))==-1)
{
puts("Cannot open file");
exit(0);
}
else
{
puts("File opened successfully");
}
puts("Trying to read");
do
{
xr = read(fd,b,1);
printf("%s",b);
} while(xr!=-1)
close(fd);
}
文件 file.dat 包含字符串“hello”,但我得到一些垃圾字符作为输出。错误是什么?