我写了下面的代码从“FILE.DAT”读书读()函数中使用读取功能无法读取通过在C
#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包含字符串“你好”,但我我得到一些垃圾字符作为输出。什么是错误?的b
printf(“%c”,b); 试试这个.....否则试试这个char b [1]; – Gangadhar
此代码写得很差,很难知道从哪里开始。查看您在Google'C read example'中找到的许多发布示例中的一个。以下仅供初学者参考:http://ubuntuforums.org/showthread.php?t=1103327 – Gene
Manoj首先缩进您的代码 –