//C库函数读取文件的代码

I/O缓冲机制 C语言库函数写文件都是写在内存中,然后一次写入磁盘。提高了效率。

读写文件,不对系统进行操作,一般采用C语言库函数。移植可以在任何可以对C支持的操作系统,而不用修改。

FILE *fopen(const char *path, const char *mode);

mode 参数:

r    Open text file for reading. The stream is positioned at the beginning of the file.

r+  Open for reading and writing. The stream is positioned at the beginning of the file.

w  Truncate file to zero length or create text file for writing. The stream is positioned at the beginning of the file.

w+ Open for reading and writing. The file is created if it does not exist, otherwiseit is truncated. The stream is positioned at the beginning of the file.

a Open for appending (writing at end of file). The file is created if it does notexist. The stream is positioned at the end of the file.

a+ Open for reading and appending (writing at end of file). The file is created if itdoes not exist. The initial file position for reading is at the beginning of the
     file, but output is always appended to the end of the file.

r  读的方法打开文件,文件的开始开始读

r+ 读和写,位置也是在文件的开始

w  截断文件(清空)并创建文件 写入,位置也是文件开始

w+ 读写文件,如果文件不存在就创建文件,存在就截断文件,位置也是文件开始。

a   追加写入的方式打开文件,如果文件不存在就创建文件,位置在文件尾部

a+ 打开追加文件读写。如果文件不存在,则创建文件。

int fclose(FILE *fp);

size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);

size 表示读的大小,nmemb表示读记录次数。

如100个字符的buff 可以读size =100, nmemb =1

也可以size =1 ,nmemb =100

每次读满size的时候才有返回,否则为0

size_t fwrite(const void *ptr, size_t size, size_t nmemb,FILE *stream);

size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);

返回的是记录数或者字节数,通过改变参数size 和nmemb来确定。

int main(int arg, char *args[])
{
  FILE *p = fopen(args[1], "r+");
  if (p == NULL)
  {
    printf("error is %s\n", strerror(errno));
  }

  else
  {
    printf("success\n");
    char buf[100];
    size_t rc = 0;

    while(1)
    {
      size_t tmp = fread(buf, 1, sizeof(buf), p);//原则是第二个参数乘以第三个参数的大小不能超过缓冲区
      rc += tmp;
      if (tmp == 0)
      break;

    }
    printf("rc = %d\n", rc);
    fclose(p);
  }
  return 0;
}

int fprintf(FILE *stream, const char *format, ...);

int main(int argc ,char* argv[])
{
  FILE* p= fopen(argv[1] , "r+");

  if(p==NULL)
  {
    return 0;
  }
  else
  {
    char buf[100];
    memset(buf, 0,100);
    sprintf(buf, "hello world!");
    printf("%s\n" , buf);
    fprintf(p , "%s\n", buf);
    fclose(p);
  }
}

int fputs(const char *s, FILE *stream);

char *gets(char *s);

int fscanf(FILE *stream, const char *format, ...);

//写log的代码
void writelog(const char *log)
{
  time_t  tDate;
  struct  tm *eventTime;
  time(&tDate);//得到系统当前时间
  eventTime = localtime(&tDate);//将time_t数据类型转化为struct tm结构
  int iYear = eventTime->tm_year + 1900;//以1900年开始
  int iMon = eventTime->tm_mon + 1;//从0开始--11 ,所有+1
  int iDay = eventTime->tm_mday;
  int iHour = eventTime->tm_hour;
  int iMin = eventTime->tm_min;
  int iSec = eventTime->tm_sec;

  printf("tm_isdst = %d\n", eventTime->tm_isdst);

  char sDate[16];
  sprintf(sDate, "%04d-%02d-%02d", iYear, iMon, iDay);
  char sTime[16];
  sprintf(sTime, "%02d:%02d:%02d", iHour, iMin, iSec);
  char s[1024];
  sprintf(s, "%s %s %s\n", sDate, sTime, log);
  FILE *p = fopen("my.log", "a+");
  if (p == NULL)
  {
    printf("write log my.log error:%s\n", strerror(errno));
  }else
  {
    fputs(s, p);
    fclose(p);
  }
  return;
}

//读目录的代码

int main(int arg, char *args[])
{
  if (arg <2)
  return 0;

  DIR *dp;
  struct dirent *dirp;
  dp = opendir(args[1]);//打开目录文件
  if (dp == NULL)
  {
  printf("error is %s\n", strerror(errno));
  return 0;
  }

  while((dirp = readdir(dp)) != NULL)//用readdir循环读取目录内容,读到目录尾,循环break
  {
    printf("%s\n", dirp->d_name);//将目录下的文件名打印到屏幕

  }

  closedir(dp);//关闭目录

  return 0;
}

C语言库函数--操作文件的更多相关文章

  1. Unix/Linux环境C编程入门教程(41) C语言库函数的文件操作详解

     上一篇博客我们讲解了如何使用Linux提供的文件操作函数,本文主要讲解使用C语言提供的文件操作的库函数. 1.函数介绍 fopen(打开文件) 相关函数 open,fclose 表头文件 #in ...

  2. Unix/Linux环境C编程新手教程(41) C语言库函数的文件操作具体解释

     上一篇博客我们解说了怎样使用Linux提供的文件操作函数,本文主要解说使用C语言提供的文件操作的库函数. 1.函数介绍 fopen(打开文件) 相关函数 open,fclose 表头文件 #in ...

  3. C++使用C语言库函数创建文件夹

    概述 本文演示环境: win10 + vs2017 头文件 #include <io.h> #include <direct.h> 函数 下面的函数,从左至右依次检查文件夹是否 ...

  4. C语言基础(20)-文件操作(fopen,getc,fclose)

    一.文件操作 读文件的顺序: 1.先打开文件 2.读写文件 3.关闭文件 1.1 fopen FILE *fopen( const char *path, const char *mode ); 函数 ...

  5. C语言基础 (12) 文件的操作 FILE

    课程回顾 结构体基本操作: 结构体类型的定义 // struct为关键字 Stu为自定义标识符 // struct Stu才是结构体类型 // 结构体成员不能在定义类型时赋值 struct Stu { ...

  6. C语言字符串操作常用库函数

    C语言字符串操作常用库函数 *********************************************************************************** 函数 ...

  7. C语言样式的文件操作函数

    使用C语言样式的文件操作函数,需要包含stdio.h头文件. 1.打开文件的函数: //oflag的取值为“w”或“r”,分别表示以写或读的方式打开 FILE* fd = fopen(filename ...

  8. go语言之行--文件操作、命令行参数、序列化与反序列化详解

    一.简介 文件操作对于我们来说也是非常常用的,在python中使用open函数来对文件进行操作,而在go语言中我们使用os.File对文件进行操作. 二.终端读写 操作终端句柄常量 os.Stdin: ...

  9. 转:C语言字符串操作函数 - strcpy、strcmp、strcat、反转、回文

    转自:C语言字符串操作函数 - strcpy.strcmp.strcat.反转.回文 C++常用库函数atoi,itoa,strcpy,strcmp的实现 作者:jcsu C语言字符串操作函数 1. ...

随机推荐

  1. hdoj 2 括号配对问题【数组模拟实现+STL实现】

    栈遵循先进后出的原则 括号配对问题 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 现在,有一行括号序列,请你检查这行括号是否配对.   输入 第一行输入一个数N(0 ...

  2. Modules you should know in Python Libray

    前两天被问到常用的python lib和module有哪些?最常用的那几个,其他的一下子竟然回答不上.想想也是,一般情况下,遇到一个问题,在网上一搜,顺着线索找到可用的例子,然后基本没有怎么深究.结果 ...

  3. bootstrap多层模态窗

    <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8&quo ...

  4. [SCOI2005]互不侵犯King

    题目描述 在N×N的棋盘里面放K个国王,使他们互不攻击,共有多少种摆放方案.国王能攻击到它上下左右,以及左上左下右上右下八个方向上附近的各一个格子,共8个格子. ——by洛谷 https://www. ...

  5. 使用dispatch_once:创建单列

    无论是爱还是恨,你都需要单例.实际上每个iOS或Mac OS应用都至少会有UIApplication或NSApplication. 什么是单例呢?Wikipedia是如此定义的: 在软件工程中,单例是 ...

  6. eclipse 中修改 M2_REPO的值--转载

    从eclipse中增加了maven2的插件之后,maven默认的本地库的路径是${user}/.m2/repository/下,一般windows用户的操作系统都安装在C盘,所以这个目录 下的jar包 ...

  7. Android 自定义View修炼-【2014年最后的分享啦】Android实现自定义刮刮卡效果View

    一.简介: 今天是2014年最后一天啦,首先在这里,我祝福大家在新的2015年都一个个的新健康,新收入,新顺利,新如意!!! 上一偏,我介绍了用Xfermode实现自定义圆角和椭圆图片view的博文& ...

  8. c基础语法

    1 连续写两个分号,第2个分号就是一条空语句,空语句实际是什么也不做,语法是正确的,编译不会出错. 空语句可以增加程序的可读性,可以作为待写的函数体.循环体.语句块.所以,空语句是可以有用的. 2 s ...

  9. Java Inner class

    2016-03-27 内部类:就是把一个类的定义放在另外一个外围类定义的里面. class OutterClass{ class InnerClass { } } 1. 内部类主要有四种:成员内部类( ...

  10. inux设置普通用户无密码sudo权限

    配置普通用户无密码sudo权限: root用户进入到Linux系统的/etc目录下 cd /etc 将sudoers文件赋予写的权限 chmod u+w /etc/sudoers 编辑sudoers文 ...