LINUX C 关于读取文件的问题,不知道哪里写错了,请高手指点,谢谢!
#include <stdio.h>#include <fcntl.h>#include <stdlib.h>struct stu{ int no; char name[30]; float score;};//打开文件 int openfile(const char* filename){ int fd; fd=open(filename, O_RDONLY);}//打印数据void print(struct stu *record){ printf("%d,\t%s,\t%.2f\n",record->no,record->name,record->score);}main(){ int fd; fd=openfile("stu.dat"); if(fd==-1) printf("open err::%m\n"), exit(-1); struct stu record={0}; while( (read(fd,record,sizeof(struct stu)))!=0 ) { print(record); } close(fd); printf("finished!\n"); }int openfile(const char* filename){ int fd; fd=open(filename, O_RDONLY); [color=#FF0000]return fd;[/color]}
[解决办法]
int openfile(const char* filename){ int fd = -1; fd=open(filename, O_RDONLY); return fd;}
[解决办法]
2楼说的是,各种返回结果判断很重要。否则很多错误都不清楚情况。