日期:2014-05-16 浏览次数:20688 次
#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楼说的是,各种返回结果判断很重要。否则很多错误都不清楚情况。