Linux系统编程-DAY02

一、标准io

1.写文件
    fgets函数中判断有多少行,且判断最后一个是不是终止符        if( buf[strlen(buf) - 1] == ' \n' )
2. wc命令行:字符统计
    wc -l 文件名
    行数 文件名
3. write  用于操作二进制的文件(文办文件和图片文件也可以操作)
    size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream) ptr一般为数组,第一个size表示多大,第二个size表示份数。
    od -t x1 文件名 ,用来看二进制或16进制文件
4.  (int) size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stram)
     bzero(&per, sizeof(per));
     读可以多,写不能多
5.标准io的
    int fseek (FILE *stream, long offset, int whence)
    long ftell (FILE *stream),用ftell获得完大小后,要再把ftell放回去
    void rewind(FILE *stream),把pos置零
6.FILE *
    stdin -> 标准输入,终端,键盘scanf,gets,getchar
    stdout-> 标准输出,终端,屏幕
    stderr -> 标准错误输出,终端,屏幕
7.行缓冲区的大小为1k(1024):缓冲区的功能就是人机交互
    程序结束(正常结束)、缓存区满或者遇到\n,刷新缓冲区。fflush刷新,
    全缓冲区,4k,主要用于文件的读写
    缓存区满刷新,程序结束刷新,fflush刷新

二、文件io
1.c库封装了系统调用
2.c库io都是带缓存区的,文件io不带缓冲区
3.默认情况下 int 0-1023
    unistd.h ---> POSIX标准库
4.int open( const char *pathname, int flags, mode_t mode );  flags 宏(只读、只写、读写文件,三选一);返回值为-1的话就是出错,成功的话就返回一个文件名
   O_APPEND  O_CREAT  O_TRUNG    (eg:创建文件: O_WRONLY |  O_CREAT | O_TRUNG )
    -    rw-rw-r--  作者,组,其他 (-普通文件  d目录)
    r w -  对应的一个名字(如果三个有的话就为 r w x ,没有的话就是r w -)
    可以读,可以写,不能执行
    mode_t mode : r w -的二进制代表mode
    一般文件用0666,目录文件用0777
5.write写文件 包头文件<unistd.h>
        ssize_t write(int fd, const void *buf, size_t count);第三个参数要选择数据的有效长度         strlen(buf),ssize是一个带符号的整数。
        返回值大于是实际写入的字节数,返0是设备的文件,返-1就错误了
 6.close函数  包头文件<unistd.h>
        int close (int fd)
 7.open(读文件)
        int open ( const char *pathname, int flags)
        int open(“1.txt”, O_RDONLY)
        ssize_t read(int fd,  void *buf, size_t count)    
        read 返回值:    返回实际读到的字节数、读完了返回0(结束条件)、读错返回-1
  8.  如果操作的目标是文本文件,用fgets/fputs    
       如果是二进制文件,用read,write

#include <stdio.h>
#include <string.h>
typedef struct
{
    char name[10];
    int age;
    char addr[50];
}PER;

int main(int argc, char **argv)
{
    FILE *fp = fopen(argv[1], "w");
    if(NULL == fp)
    {
        fprintf(stderr, "fopen error\n");
        return 1;
    }
    PER per;
    strcpy(per.name, "maodan");
    per.age = 20;
    strcpy(per.addr, "jeetRuna");
    fwrite(&per, sizeof(per), 3, fp);
    fclose(fp);
    return 0;
}

2.读文件

#include <stddef.h>
#include <stdio.h>
#include <string.h>
#include <strings.h>
typedef struct
{
    char name[10];
    int age;
    char addr[50];
}PER;

int main(int argc, char **argv)
{
    FILE *fp = fopen(argv[1], "r");
    if(NULL == fp)
    {
        fprintf(stderr, "fopen error\n");
        return 1;
    }
    PER per;
    bzero(&per, sizeof(per));
    size_t ret = fread(&per, sizeof(per), 1, fp);

    if(ret > 0)
    {
        printf("ret : %lu %s %d %s\n", ret, per.name, per.age, per.addr);
    }
    fclose(fp);
    return 0;
}

3.基于fread和fwrite的复制文件

int main(int argc, char **argv)
{
    FILE *src = fopen(argv[1], "r");
    FILE *dest = fopen(argv[2], "w");
    if(NULL == src || NULL == dest)
    {
        fprintf(stderr,"fopen error\n");
        return 1;
    }
    int size = 145;
    char *data = malloc(size);
    size_t ret = fread(data, size, 1, src);
    if(1 == ret)
    {
        fwrite(data, size, 1, dest);
    }
    fclose(src);
    fclose(dest);
    return 0;
}

4.fseek

#include <stdio.h>

int main(int argc, char *argv[])
{
    FILE* fp = fopen("2.txt","r");
    if(NULL == fp)
    {
        return 1;
    }

    fseek(fp,5,SEEK_SET);

    char buf[1024]={0};
    fgets(buf,sizeof(buf),fp);
    printf("%s\n",buf);
    fclose(fp);
    return 0;
}

5.ftell

#include <stdio.h>

int main(int argc, char *argv[])
{
    FILE* fp = fopen("2.png","r");
    if(NULL == fp)
    {
        return 1;
    }

    fseek(fp,0,SEEK_END);
    long size = ftell(fp);
    printf("size %ld\n",size);
    fclose(fp);
    return 0;
}

6.rewind

#include <stdio.h>

int main(int argc, char *argv[])
{
    FILE* fp = fopen("2.txt","r");
    if(NULL == fp)
    {
        return 1;
    }

    fseek(fp,0,SEEK_END);
    long size = ftell(fp);
    printf("size:%ld\n",size);
    
    rewind(fp);
    char buf[1024]={0};
    fgets(buf,sizeof(buf),fp);
    printf("%s",buf);
    fclose(fp);
    return 0;
}

7.stdin.c

#include <stdio.h>
int main(int argc, char *argv[])
{
    char buf[100]={0};

    fgets(buf,sizeof(buf),stdin);//FILE*  //scanf ();%s

    fputs(buf,stdout);
    fprintf(stderr,"%s 123",buf);
    return 0;
}

8.行缓冲

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
    
    printf("hello");
    fflush(stdout);
    while(1)sleep(1);
    return 0;
}

9.全局缓冲

#include <stdio.h>
#include <unistd.h>
// a.out aadf
int main(int argc, char *argv[])
{
    FILE* fp = fopen("1.txt","w");
    if(NULL == fp)
    {
        fprintf(stderr,"fopen error\n");
        return 1;
    }

    char buf[]="world";
    char str2[]="aaabbcc";
    fputs("hello",fp);// puts();
    fflush(fp);
    while(1)sleep(1);
    fclose(fp);
    return 0;
}

10.stderr

#include <stdio.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
    
    fprintf(stderr,"aaa");
    while(1)sleep(1);
    return 0;
}

11.文件io---open

#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>

#include <stdio.h>

int main(int argc, char **argv)
{
  int a = 12312;
  int fd = open("1.txt", O_WRONLY | O_CREAT | O_TRUNC, 0666);
  if (-1 == fd)
    {
      fprintf(stderr, "open error\n");
      return 1;
    }

  // system("pause");
  return 0;
}

12.文件io --- write

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

int main(int argc, char **argv)
{
  int a = 12312;
  int fd = open("1.txt", O_WRONLY | O_CREAT | O_TRUNC, 0666);
  if (-1 == fd)
    {
      fprintf(stderr, "open error\n");
      return 1;
    }

  char buf[1024] = "hello";
  ssize_t ret = write(fd, buf, strlen(buf));
  printf("write ret:%ld\n", ret);

  close(fd);

  // system("pause");
  return 0;
}

13.文件io --- read

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

int main(int argc, char **argv)
{
  
  int fd = open("1.txt", O_RDONLY);
  if (-1 == fd)
    {
      fprintf(stderr, "open error\n");
      return 1;
    }

  char buf[1024] = {0};
  ssize_t ret = read(fd,buf,sizeof(buf));
  printf("readret:%ld %s\n", ret,buf);
  
  close(fd);

  // system("pause");
  return 0;
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值