读文件:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h> //linux下面的头文件
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
int main(int avg, char* avgs[])
{
if (avg < 2)
{
printf("参数错误\n");
return EXIT_FAILURE;
}
char s[] = "abc.txt";
int fd = open(avgs[1], O_RDONLY);
if (fd == -1)
{
printf("error :%s\n", strerror(errno));
}
else
{
printf("fd=%d\n", fd);
char buf[100];
memset(buf, 0, sizeof(buf));
//循环读取文件
while (read(fd, buf, sizeof(buf)-1)>0)
{
printf("buf:%s\n", buf);
memset(buf, 0, sizeof(buf));
}
close(fd);
}
return 0;
}

  

写文件:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h> //linux下面的头文件
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
int main(int avg, char* avgs[])
{
if (avg < 2)
{
printf("参数错误\n");
return EXIT_FAILURE;
}
int fd = open(avgs[1], O_RDWR | O_APPEND);
if (fd==-1)
{
printf("file open error : %s \n", strerror(errno));
return EXIT_FAILURE;
}
else
{
printf("fd= %d\n", fd);
char buf[100];
memset(buf, 0, sizeof(buf));
strcpy(buf, "RtesttesttestR");
int i = write(fd, buf, strlen(buf));
close(fd);
}
return 0;
}

  

获取文件详细信息:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h> //linux下面的头文件
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
int main(int avg, char* avgs[])
{
if (avg < 2)
{
printf("参数错误\n");
return EXIT_FAILURE;
}
int fd = open(avgs[1], O_RDWR | O_APPEND);
if (fd==-1)
{
printf("file open error : %s \n", strerror(errno));
return EXIT_FAILURE;
}
else
{
struct stat buf;
//获取文件信息
fstat(fd, &buf);
//判断文件是否为标准文件
if (S_ISREG(buf.st_mode))
{
printf("%s is a char file \n", avgs[1]);
}
//判断文件是否为目录
if (S_ISDIR(buf.st_mode))
{
printf("%s is a directory \n", avgs[1]);
}
//打印文件的大小
printf("%s file size is %d\n",avgs[1], buf.st_size);
}
return 0;
}

  

c语言读文件

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int avg, char* avgs[])
{
if (avg < 2)
{
printf("avg error\n");
return EXIT_FAILURE;
}
FILE* fp = fopen(avgs[1], "r");
if (fp == NULL)
{
printf("file open error");
return EXIT_FAILURE;
}
char buf[10];
memset(buf, 0, sizeof(buf)); size_t rc = 0;
while (1)
{
//fr返回读了几条记录数(fread第三个参数表示读多少条记录)
size_t fr = fread(buf, 1, 10, fp);
rc += fr;
if (fr == 0)
{
break;
}
printf("%s \n", buf);
memset(buf, 0, sizeof(buf));
}
printf("size:%d\n", rc);
return EXIT_SUCCESS;
}

 

c语言写文件

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//写文件
int main(int avg, char* avgs[])
{
if (avg < 2)
{
printf("avg error !\n");
return EXIT_FAILURE;
}
FILE* fp = fopen(avgs[1], "a+");
if (fp == NULL)
{
printf("file open error!\n");
return EXIT_FAILURE;
}
else
{
char buf[100];
memset(buf, 0, sizeof(buf));
strcpy(buf, "hello world\n");
fwrite(buf, strlen(buf), 1, fp);
fclose(fp);
}
getchar();
}

  

二进制文件的读和写操作

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
typedef struct _Person {
char name[20];
int age;
}Person;
//二进制文件读操作
int main(int avg, char* avgs[])
{
if (avg < 2)
{
printf("avg count error!\n");
return EXIT_SUCCESS;
}
FILE* fp = fopen(avgs[1], "r");
if (printf == NULL)
{
printf("file open error %s\n", strerror(errno));
}
else
{
Person parray[10];
memset(&parray, 0, sizeof(Person));
while (fread(&parray[0],sizeof(Person),1,fp))
{
printf("age:%d,name:%s\n", parray[0].age, parray[0].name);
}
fclose(fp);
}
return EXIT_SUCCESS;
}
//二进制文件写操作
int main(int avg, char* avgs[])
{
if (avg<2)
{
printf("avg count error!\n");
return EXIT_SUCCESS;
}
FILE* fp = fopen(avgs[1], "w");
if (printf==NULL)
{
printf("file open error %s\n", strerror(errno));
}
else
{
Person parray[10];
parray[0].age = 0;
strcpy(parray[0].name, "caoruipeng");
parray[1].age = 1;
strcpy(parray[1].name, "jiaruixin"); fwrite(&parray, sizeof(Person), 2, fp);
fclose(fp);
}
return EXIT_SUCCESS;
}

  

文件目录读写

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <time.h>
#include <dirent.h>
int main(int arg, char* args[])
{
DIR* dp;
struct dirent* dirp;
dp = opendir(args[1]);
if (dp == NULL)
{
printf("open dir error %s\n", strerror(errno));
return 0;
}
while ((dirp = readdir(dp)) != NULL)
{
printf("name:%s\n", dirp->d_name);
}
closedir(dp);
}

  

Linux文件读写笔记的更多相关文章

  1. Linux文件读写机制及优化方式

    导读 Linux是一个可控性强的,安全高效的操作系统.本文只讨论Linux下文件的读写机制,不涉及不同读取方式如read,fread,cin等的对比,这些读取方式本质上都是调用系统api read,只 ...

  2. (转)linux文件读写的流程

    转自http://hi.baidu.com/_kouu/item/4e9db87580328244ef1e53d0 在<linux内核虚拟文件系统浅析>这篇文章中,我们看到文件是如何被打开 ...

  3. Linux 文件读写操作与磁盘挂载

    文件读写 [文件描述符] Linux下,通常通过open打开一个文件,它然后返回给我们一个整数,通过这个整数便可以操作文件,这个整数我们称文件描述符(fd).对应被打开的文件,它也是一种系统资源,那么 ...

  4. python的文件读写笔记

    读写文件是最常见的IO操作.Python内置了读写文件的函数,用法和C是兼容的. 读写文件前,我们先必须了解一下,在磁盘上读写文件的功能都是由操作系统提供的,现代操作系统不允许普通的程序直接操作磁盘, ...

  5. Linux 文件类型笔记

    在UNIX中一切都是文件https://ph7spot.com/musings/in-unix-everything-is-a-file在UNIX中,一切都是字节流 ==== linux系统的文件类型 ...

  6. Linux文件权限-笔记

    文件权限共10个字符,第一个字符表示该文件是[文件夹]或[文件]——如果是字符“d"则表示该文件是文件夹:如果是字符“-”则表示是文件. 后九个字符,三个一组,共三组,分别表示[所有者权限] ...

  7. Linux文件操作 笔记

    fstat stat lstat 原型 #include <unistd.h> #include <sys/stat.h> #include <sys/types.h&g ...

  8. linux文件读写 文件锁、select、poll【转】

    本文转载自:http://blog.csdn.net/fansongy/article/details/6853395 一.文件锁 文件锁用于多个用户共同使用或操作同一个文件.有读锁的时候可以再加读锁 ...

  9. LINUX - 文件读写缓存

    遇到一个进程core掉后日志打印不出来的问题: 参考如下: [引用] 只有正常退出,才能做到flush.否则将写失败. 之后有百度了下中文资料,发现同样的结论. "fflush库函数的作用是 ...

随机推荐

  1. 【转】Diamond -- 分布式配置中心

    特别提示:本人博客部分有参考网络其他博客,但均是本人亲手编写过并验证通过.如发现博客有错误,请及时提出以免误导其他人,谢谢!欢迎转载,但记得标明文章出处:http://www.cnblogs.com/ ...

  2. spring的IOC——依赖注入的两种实现类型

    一.构造器注入: 构造器注入,即通过构造函数完成依赖关系的设定.我们看一下spring的配置文件: <constructor-arg ref="userDao4Oracle" ...

  3. java特殊运算符

    按位运算符 定义:按位运算符是来操作整数基本数据类型中的单个“比特”(bit),即二进制位,位运算符会对两个参数中对应的位执行布尔代数运算,并最终生成一个结果. 分类:与(&).或(|).异或 ...

  4. forms authentication原理

    细说ASP.NET Forms身份认证 asp.net 登陆验证 Form表单验证的3种方式 Understanding and Implementing ASP.NET Custom Forms A ...

  5. Windows下C/C++内存泄露检测机制

    1.概述 在Windows下微软给我们提供了一个十分强大的C/C++运行时库,这个运行时库中包含了很多有用的功能.而众多强大功能之一就是内存泄露的检测. C/C++提供了强大的内存管理功能,不过随之而 ...

  6. springboot2.0+mycat实验读写分离

    声明:用户到达一定程度,架构就必须要考虑,因为在这个前提下,读写分离,尤为重要. 1.搭建mysql主从复制 https://www.cnblogs.com/ywjfx/p/10264383.html ...

  7. C#连接Oracle数据库的四种方法

    C#连接数据库的四种方法 在进行以下连接数据库之前,请先在本地安装好Oracle Client,同时本次测试System.Data的版本为:2.0.0.0. 在安装Oracle Client上请注意, ...

  8. lnmp 环境下 部署 laravel 项目

    出现错误 Warning: require(): open_basedir restriction in effect. File(/xxxx/vendor/autoload.php) is not ...

  9. java带图形界面的五子棋

    Main: package BlackWhite; import java.awt.BorderLayout; import java.awt.Color; import java.awt.FlowL ...

  10. (转)Maven的pom.xml文件配置使用

    转载:http://www.cnblogs.com/GarfieldTom/p/3707160.html <project xmlns="http://maven.apache.org ...