日期:2014-05-16  浏览次数:20616 次

LINUX C 关于读取文件的问题,不知道哪里写错了,请高手指点,谢谢!
C/C++ code

#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");    
    
}





[root@localhost]# gcc t3.c
t3.c: 在函数 ‘main’ 中:
t3.c:47: 错误:实参 1(属于 ‘print’)类型不兼容

我是初学者,怎么改正,请高手指教,谢谢!


------解决方案--------------------
print ->> printf
函数写错了,printf("%s",record); 而printf(record)不好,有漏洞。

C/C++ code
int openfile(const char* filename)
{
    int fd;
    fd=open(filename, O_RDONLY);
    [color=#FF0000]return fd;[/color]
}

------解决方案--------------------
C/C++ code
int openfile(const char* filename)
{
    int fd = -1;
    fd=open(filename, O_RDONLY);
    return fd;
}

------解决方案--------------------
2楼说的是,各种返回结果判断很重要。否则很多错误都不清楚情况。