I/O文件测试

来源:互联网 发布:java 选择 编辑:程序博客网 时间:2024/06/05 03:07

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

int OpenToRW(char *filename);

int OpenToRW(char *filename)
{
    int fd;
    char str[30];
    char stw[50];
    int num;
    char ch;
    int temp = 0;
    fd = open(filename, O_CREAT|O_RDWR, 0777);/*文件存在就打开,不存在就创建*/
    if(fd == -1){
        perror("Open File Failed!\n");
        return 2;
    }
    while((num = read(fd, str, 29)) != 0){
        str[num] = '\0'; //防止读出乱码
        printf("%s", str);
    }
    printf("Input YOUR write:(<50)\n");
    getchar(); //屏蔽掉上一个回车,很重要

    while((ch = getchar()) != '#'){//接受字符遇到‘#’号结束
        stw[temp++] = ch;
    }
    stw[temp] = '\0';
    num = write(fd, stw, strlen(stw));//写入文件
    if(num == -1){
        printf("Write ERROR!\n");
        return 3;
    }
    close(fd);
    return 0;
}
int main(int argc, char *argv[])
{
    int flag;
    char filename[20];
    printf("Input The filename you want to RW:\n");
    scanf("%s", filename);
    flag = OpenToRW(filename);
    printf("OpenToWR:flag = %d\n", flag);
    return 0;
}

阅读(233) | 评论(0) | 转发(0) |
0

上一篇:myshell

下一篇:linux下的led驱动程序

相关热门文章
  • 欢迎calibrecalibre在ChinaUni...
  • 欢迎qbr2134在ChinaUnix博客安...
  • 欢迎qq1972397729在ChinaUnix...
  • 欢迎gtrtrtiuu在ChinaUnix博客...
  • 欢迎uouo88在ChinaUnix博客安...
  • linux 常见服务端口
  • 【ROOTFS搭建】busybox的httpd...
  • xmanager 2.0 for linux配置
  • 什么是shell
  • linux socket的bug??
  • 初学UNIX环境高级编程的,关于...
  • chinaunix博客什么时候可以设...
  • 一个访问量较大网站的服务器。...
  • 收音机驱动与v4l2架构的关系,...
  • 如何将linux驱动改为裸机驱动(...
给主人留下些什么吧!~~