首页 > 其他 > 详细

IO高级编程——获取特定位置上的数据(lseek函数的使用)

时间:2014-02-05 17:16:56      阅读:378      评论:0      收藏:0      [点我收藏+]


1、demo1.c

#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
struct stu
{
    int no;
    char name[20];
    float score;
};
/*
1.判定文件是否存在,存在打开,不存在创建
2.输入记录
3.保存记录
4.提示继续输入
5.继续/不继续
6.关闭文件
*/
int openfile(const char *filename)
{
    int fd;
    fd=open(filename,O_RDWR|O_CREAT|O_EXCL,0666);
    if(fd==-1)//表示文件存在,则打开
    {
        fd=open(filename,O_RDWR|O_APPEND);
        return fd;
    }
    return fd;
}
void input(struct stu *record)
{
    bzero(record,sizeof(struct stu));
    printf("输入学号:");
    scanf("%d",&(record->no));
    printf("输入姓名:");
    scanf("%s",record->name);
    printf("输入成绩:");
    scanf("%f",&(record->score));
}
void save(int fd,struct stu *record)
{
    write(fd,record,sizeof(struct stu));
}
int iscontinue()
{
    char c;
    printf("是否继续输入:\n");
    //fflush(stdin);
    //fflush(stdout);
    scanf("\n%c",&c);    
    if(c==‘Y‘ || c==‘y‘)
    {
        return 1;
    }
    return 0;
}

int main()
{
    int fd;
    int r;
    struct stu s={0};
    fd=openfile("stu.dat");
    if(fd==-1) printf("openfile:%m\n"),exit(-1);
    
    while(1)
    {
        input(&s);
        save(fd,&s);
        r=iscontinue();
        if(r==0) break;
        system("clear");
    }
    close(fd);
    printf("输入完毕!\n");    
}


2、main.c

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>


int main(){

   int fd = open("stu.dat",O_RDONLY);
   if(fd == -1){
     printf("open error:%m\n");
     exit(-1);
   }

   float score;

   int i;
   for(i = 0 ; i < 2 ; ++i){
      int r = lseek(fd,i*28+24,SEEK_SET);
      r = read(fd,&score,sizeof(float));
      printf("%.2f\n",score);
   }
}


3、编写makefile文件

lseek:
        gcc lseek.c -omain


4、在命令行中输入命令:  make


IO高级编程——获取特定位置上的数据(lseek函数的使用)

原文:http://blog.csdn.net/hjd_love_zzt/article/details/18938203

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!