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

pread疑惑:pread读完后,将指向后一个位置吗?
C/C++ code

/****************************
stu.dat数据结构 
    int no;
    char name[20];
    float score;
*****************************/
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>

int main()
{
    int fd;
    fd=open("stu.dat",O_RDONLY);
    if(fd==-1) printf("open err::%m\n"), exit(-1);
    
    int i;
    
    float score;
    int no;
    for(i=0;i<2;i++)
    {
        pread(fd,&score,sizeof(float),i*28+24);  //读取学生的成绩
        printf("%.2f\n",score);
        
        read(fd,&no,4);  //这个应该是定位到了下一条记录的int no学生编号了吧?
        printf("%d\n",no);  //但这里打印出来的却不是int no学生编号,很奇怪!是什么原因
    }
    
    close(fd);
    
    return 0;
}



------------------------
read(fd,&no,4); //这个应该是定位到了下一条记录的int no学生编号了吧?
printf("%d\n",no); //但这里打印出来的却不是int no学生编号,很奇怪!是什么原因

高手帮忙看一下,谢谢


------解决方案--------------------
The file offset is not changed.
pread 是不会改变偏移量的值的,read会改变。