
IO
zxy131072
嵌入式linux
展开
-
fcntl改变文件状态标志
可以用fcntl改变文件状态标志,改变文件为非阻塞状态。1、获取文件的flags,即open函数的第二个参数: flags = fcntl(fd,F_GETFL,0);2、设置文件的flags: fcntl(fd,F_SETFL,flags);3、增加文件的某个flags,比如文件是阻塞的,想设置成非阻塞: flags = fcntl(fd,F_GETFL,0); flags |= O_NONBLOCK; fcntl(fd,F_SE原创 2020-09-23 16:00:02 · 279 阅读 · 0 评论 -
Linux下C中fcntl函数
#include <stdio.h>#include <string.h>#include <stdlib.h>#include <unistd.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>void fcntl_dup_fd(void){ int fd = open("temp", O_RDWR | O_CREAT, 066原创 2020-09-13 20:52:39 · 365 阅读 · 0 评论 -
Linux下使用stat函数得到文件大小
#include <sys/types.h>#include <sys/stat.h>#include <unistd.h>#include <stdio.h>#include <stdlib.h>int main(int argc, const char* argv[]){ if(argc < 2) { fprintf(stderr, "%s <filename>\n", argv原创 2020-09-13 20:52:26 · 450 阅读 · 0 评论 -
Linux下C中trunc函数
#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <sys/types.h>int main(int argc, const char* argv[]){ if(argc < 3) { fprintf(stderr, "%s <filename> <size>\n", argv[0]); ex原创 2020-09-13 20:52:14 · 493 阅读 · 0 评论 -
Linux下非阻塞读操作
#include <unistd.h>#include <fcntl.h>#include <errno.h>#include <string.h>#include <stdlib.h>#include <stdio.h>#define MSG_TRY "try again\n"#define MSG_TIMEOUT "timeout\n"// 非阻塞读终端和等待超时int main(int原创 2020-09-13 20:52:01 · 1611 阅读 · 0 评论 -
Linux下C中rename函数
#include <stdio.h>#include <stdlib.h>int main(int argc, char* argv[]){ if(argc < 3) { fprintf(stderr, "%s <oldname> <newname>\n", argv[0]); exit(1); } int ret = rename(argv[1], argv[2]); i原创 2020-09-13 20:51:49 · 1207 阅读 · 0 评论 -
Linux下C中chmod函数
#include <stdio.h>#include <stdlib.h>#include <sys/stat.h>#include <unistd.h>int main(int argc, const char* argv[]){ if(argc < 3) { fprintf(stderr, "%s <filename> <mode>\n", argv[0]); exi原创 2020-09-13 20:51:38 · 1380 阅读 · 0 评论 -
Linux下C中chdir函数
#include <stdio.h>#include <fcntl.h>#include <sys/types.h>#include <sys/stat.h>#include <stdlib.h>#include <unistd.h>int main(int argc, const char* argv[]){ if(argc < 2) { fprintf(stderr, "usag原创 2020-09-13 20:51:25 · 1377 阅读 · 0 评论 -
Linux下C中access函数
#include <stdio.h>#include <stdlib.h>#include <unistd.h>int main(int argc, const char *argv[]){ int ret = -1; if(argc < 2) { fprintf(stderr, "%s <filename>\n", argv[0]); exit(1); } ret =原创 2020-09-13 20:51:10 · 258 阅读 · 0 评论 -
Linux下C中mkdir函数
#include <stdio.h>#include <stdlib.h>#include <sys/types.h>#include <sys/stat.h>int main(int argc, const char* argv[]){ if(argc < 3) { fprintf(stderr, "%s <newDir> <mode>\n", argv[0]); ex原创 2020-09-11 09:03:31 · 1293 阅读 · 0 评论 -
单机模式下的文件上传与下载
单机模式下的文件上传与下载,只是一个简单地示例,目前自己发现德里面存在很多问题,比如不能下载子目录中的文件,下载的文件的权限与上传的文件的权限的一致性问题。如果后期的某些项目中确实需要这个功能,再去深究。/* 单机模式下的文件上传送和下载* (1) 输入服务器的地址: 路径和目录名* (2) 列出服务器中有哪些文件* (3) 输入从服务器下载的文件名 或 上传文件到服务器的文件名* (4) 文件下载 * (5) 文件上传送*/#include原创 2020-09-03 15:09:41 · 318 阅读 · 0 评论 -
getline函数使用
/* * int getline(char **lineptr, size_t *n, FILE *stream)* * 失败:成功返回读取的该行的字符数,返回-1。* 参数:* lineptr:指向存放该行字符的指针,如果是NULL,则有系统帮助malloc,* 请在使用完成后free释放。* n:如果是由系统malloc的指针,填0* stream:文件原创 2020-09-01 10:35:12 · 413 阅读 · 0 评论 -
tmpfile创建临时文件
/** FILE *tmpfile(void);* 功能:以二进制更新模式(wb+)创建临时文件。* 被创建的临时文件会在流关闭的时候或者在程序终止的时候自动删除。* 返回值:* FILE *:成功文件指针,失败NULL。*/#include <stdio.h>#include <string.h>#define BUFFSIZE (1024)typedef struct data{ i原创 2020-09-01 10:06:22 · 603 阅读 · 0 评论 -
tmpnam函数创建临时文件
/** char *tmpnam(char *s);* 功能: 在当前目录下创建一个唯一的临时文件。* 参数:* 若s非空,则返回的临时文件名的数组指针保存在s所指向的数组中。* 若s为NULL,则该临时文件名将保存在一个静态数组中。* 返回值:* char *:保存临时文件名的数组指针 ,失败返回NULL。*/#include <stdio.h>#define BUFFSIZE原创 2020-09-01 09:46:07 · 340 阅读 · 0 评论 -
C实现输入输出重定向
/* 实现"+"为输入重定向,"-"为输出重定向 */#include <stdio.h>#include <string.h>#include <stdlib.h>#include <unistd.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#define BUFF (1024)int my_copy(int f原创 2020-08-31 16:45:34 · 600 阅读 · 0 评论 -
文件描述符与文件指针之间的转换
#include <stdio.h>#include <string.h>#include <stdlib.h>#include <unistd.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#define BUFF (1024)int fd2file(const char * path){ FILE *fp =原创 2020-08-31 09:48:24 · 284 阅读 · 0 评论 -
Linux下静态库与动态库的制作
本质上来说库是一种可执行代码的二进制形式,可以被操作系统载入内存执行。由于windows和linux的本质不同,因此二者库的二进制是不兼容的。静态库和共享库(动态库)二者的不同点在于代码被载入的时刻不同。静态库在程序编译时会被连接到目标代码中,程序运行时将不再需要该静态库,因此体积较大动态库在程序编译时并不会被连接到目标代码中,而是在程序运行是才被载入,因此在程序运行时还需要动态库存在,因此代码体积较小。现实中每个程序都要依赖很多基础的底层库,不可能每个人的代码都从零开始,因此库的存在意义原创 2020-08-18 08:55:54 · 195 阅读 · 0 评论 -
Linux下C中打印文件的属性信息
#include <stdio.h>#include <unistd.h>#include <time.h>#include <sys/types.h>#include <sys/stat.h>int main(int argc, char *argv[]){ struct stat buf; int n; struct tm *tp; if(argc < 2) { printf原创 2020-08-27 13:49:45 · 370 阅读 · 0 评论 -
Linux下C实现打印指定的目录下所有文件名称
#include <stdio.h>#include <dirent.h>int main(int argc, char *argv[]) {{ DIR *dirp; struct dirent *dp; if (argc < 2) { printf(“Usage : %s <directory>\n”, argv[0]); return -1; } if ((dirp = opend原创 2020-08-26 14:01:40 · 998 阅读 · 0 评论 -
文件IO中的目录操作
opendir函数#include <sys/types.h>#include <dirent.h>DIR *opendir(const char *name);功能:打开一个目录,获取目录流指针参数: name:目录名返回值: DIR是用来描述一个打开的目录文件的结构体类型,成功时返回目录流指针;出错时返回NULL。readdir函数#include <dirent.h>struct dirent *readdir(DIR *dirp);功能原创 2020-08-26 13:59:45 · 255 阅读 · 0 评论 -
文件IO基础总结
特点1、posix(可移植操作系统接口)定义的一组函数2、不提供缓冲机制,每次读写操作都引起系统调用3、核心概念是文件描述符4、访问各种类型文件5、Linux下, 标准IO基于文件IO实现一、open函数int open(const char *pathname, int flags);int open(const char *pathname, int flags, mode_t mode);功能:打开一个文件,获得文件的文件描述符参数: pathname:指定打开的文件路原创 2020-08-26 13:49:19 · 219 阅读 · 0 评论 -
Linux下C中使用write函数实现将键盘输入的内容写入文件,直到输入quit退出程序
#include <stdio.h>#include <fcntl.h>#include <unistd.h>#include <string.h>#define N 128int main(int argc, const char *argv[]){ int fd; char buf[N] = { 0 }; memset(buf, 0, sizeof(buf)); if ((fd = open原创 2020-08-26 13:45:49 · 2160 阅读 · 0 评论 -
Linux下使用C中read函数实现统计文件大小
#include <stdio.h>#include <fcntl.h>#include <unistd.h>#define N 128int main(int argc, const char *argv[]){ int fd = -1; int n = 0; int total = 0; char buf[N]; if(argc < 2) { printf("原创 2020-08-26 13:38:27 · 594 阅读 · 0 评论 -
fprintf sprintf函数使用示例
//fprintf sprintf函数使用示例//以指定格式 “年-月-日” 分别写入文件和缓冲区#include <stdio.h>#include <string.h>int main(int argc, char *argv[]){ int year = 2020; int month = 8; int day =26; FILE *fp; char buf[64]; if((fp = fopen("test原创 2020-08-26 10:56:44 · 306 阅读 · 0 评论 -
标准IO函数获取文件的大小
#include <stdio.h>int main(int argc, char *argv[]){ FILE *fp; long length = 0; if((fp = fopen("test.txt", "r")) == NULL) { perror("fopen"); return -1; } fseek(fp, 0, SEEK_END); length = ftell(fp); printf("原创 2020-08-26 10:47:02 · 902 阅读 · 0 评论 -
Linux下C实现统计文本文件含有多少行
#include <stdio.h>#include <string.h>#define N 64int main(int argc, char *argv[]){ FILE *fp; int line = 0; char buf[N]; if(argc < 2) { printf("Usage: %s <file>\n", argv[0]); return -1; } if(原创 2020-08-26 10:29:18 · 310 阅读 · 0 评论 -
strerror函数使用
通过标准错误的标号,获得错误的描述字符串 ,将单纯的错误标号转为字符串描述。#include <stdio.h>#include <string.h>#include <errno.h>int main(int argc, char *argv[]){ FILE *fp; if ((fp = fopen("test.txt", "r")) == NULL) { printf("errno=%d\n",errno);原创 2020-08-26 10:03:57 · 1046 阅读 · 0 评论 -
freopen函数使用
freopen是被包含于C标准库头文件<stdio.h>中的一个函数,用于重定向输入输出流。该函数可以在不改变代码原貌的情况下改变输入输出环境,但使用时应当保证流是可靠的。FILE *freopen( const char *filename, const char *mode, FILE *stream );功能:以指定模式重新指定到另一个文件。模式用于指定新文件的访问方式。形参:filename:需要重定向到的文件名或文件路径。mode:文件访问权限。stream:需要被重定向的原创 2020-08-26 09:58:29 · 1468 阅读 · 0 评论 -
fseek函数
#include <stdio.h>int main(int argc, char const *argv[]){ FILE *fp = fopen("./a.out", "r"); if(NULL == fp) { perror("fopen"); return -1; } printf("ftell_offset:%ld\n",ftell(fp)); fprintf(fp,"def"); fflush(fp); printf("ftell_offs原创 2020-08-12 00:03:22 · 161 阅读 · 0 评论 -
fread和fwrite函数
fwrite.c#include <stdio.h>typedef struct data{ int num;}md_t;md_t arr[] = { [0] = { .num = 20, }, [1] = { .num = 40, }};int main(int argc, char const *argv[]){ FILE *fp ; fp = fopen("./file.txt","w+"); if(NULL == fp) {原创 2020-08-12 00:03:12 · 199 阅读 · 0 评论 -
fputs函数
#include <stdio.h>int main(int argc, const char *argv[]){ char s[128] = "this is a fputs test demo!"; fputs(s,stdout); return 0;}测试结果原创 2020-08-12 00:01:51 · 618 阅读 · 0 评论 -
fgets函数
#include <stdio.h>#include <string.h>int main(int argc, const char *argv[]){ char buf[32] = { 0 }; char *p = NULL; memset(buf, 0, sizeof(buf)); p = fgets(buf, 5, stdin); if(NULL == p) { perror("fgets"); return -1; } prin原创 2020-08-12 00:02:38 · 180 阅读 · 0 评论 -
fputc函数
#include <stdio.h>int main(int argc, const char *argv[]){ char c = 0; scanf("%c", &c); int ch = fputc(c, stdout); if(EOF == ch) { perror("fputc"); return -1; } fputc(10,stdout); //换行 return 0;}测试结果...原创 2020-08-12 00:02:47 · 4410 阅读 · 0 评论 -
fgetc函数
#include <stdio.h>int main(int argc, const char *argv[]){ char ch; FILE *fp = NULL; fp = fopen("test.txt","a+"); if(NULL == fp) { perror("fopen"); return -1; } scanf("%c",&ch); fprintf(fp,"hello world"); printf("原创 2020-08-12 00:03:04 · 965 阅读 · 0 评论 -
fopen函数
#include <stdio.h>int main(int argc, const char *argv[]){ char ch; FILE *fp = NULL; fp = fopen("test.txt","a+"); if(NULL == fp) { perror("fopen"); return -1; } scanf("%c",&ch); fprintf(fp,"hello world"); printf("原创 2020-08-11 15:18:46 · 369 阅读 · 0 评论 -
lseek函数
#include <stdio.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <unistd.h>#if 0off_t lseek(int fd, off_t offset, int whence);功能:改变文件的读写位置参数:offset:偏移量,以字节为单位 whence:相对位置 SEEK_SET 文件的原创 2020-08-11 15:06:56 · 140 阅读 · 0 评论 -
目录IO操作
#include <stdio.h>#include <sys/types.h>#include <dirent.h>#if 0int closedir(DIR *dirp);功能:关闭一个目录指针返回:成功返回0,失败返回-1DIR *opendir(const char *name);功能:打开一个目录返回:成功返回目录指针,失败返回NULLstruct dirent *readdir(DIR *dirp);功能:读取接下来的一个目录项返原创 2020-08-11 14:57:49 · 138 阅读 · 0 评论 -
IO中write函数
#include <stdio.h>#include <sys/types.h> //open#include <sys/stat.h> //open#include <fcntl.h> //O_RDWR O_CREAT#include <unistd.h> //close#if 0ssize_t read(int fd, void *buf, size_t count);功能:从文件fd中读取count字节的数据存储到原创 2020-08-11 14:51:59 · 2714 阅读 · 0 评论 -
Linux下获取文件属性函数 stat
#include <stdio.h>#include <sys/stat.h>#include <sys/types.h>#include <unistd.h>#include <time.h>#if 0int stat(const char *path, struct stat *buf);struct stat { ino_t st_ino; /* inode number */ mode_t st_m原创 2020-08-11 14:44:35 · 322 阅读 · 4 评论 -
IO中read函数
#include <stdio.h>#include <sys/types.h> //open#include <sys/stat.h> //open#include <fcntl.h> //O_RDWR O_CREAT#include <unistd.h> //close#if 0size_t read(int fd, void *buf, size_t count);功能:从文件fd中读取count字节的数据存储到b原创 2020-08-11 14:40:05 · 1889 阅读 · 0 评论