挂马检测,uibia,重庆市人力资源和社会保障公众信息网
FILE * fopen(const char * path,const char * mode);
返回值
path
mode
size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);
size_t fwrite(const void *ptr, size_t size, size_t nmemb,FILE *stream);
返回值
ptr
size
nmemb
PS:尽量将size大小写为最小值(1字节)
int fclose(FILE *stream);
示例1:
#include<string.h> #include<stdio.h> int main(void) { FILE *fp = NULL; const char *buf = "0123456789"; fp = fopen("DUMMY.FIL","w");/*创建一个包含10个字节的文件*/ fwrite(buf,strlen(buf),1,fp);/*将buf内容写入到文件中*/ fclose(fp);/*关闭文件*/return 0; }
int fseek(FILE *stream, long offset, int fromwhere);
返回值
offset
Fromwhere
示例2-读写十六进制:
#include <stdio.h> #include <stdlib.h> #include <string.h> int main(int argc,char* argv[]) { FILE *fp = NULL; int readbuf; int readEnd; int writebuf=100; int len; fp = fopen("./1.txt","rb+"); printf("read 1.txt: fp==NULL=%d size=%d\n",fp == NULL,sizeof(readbuf)); if(fp!=NULL) //打开成功,读数据 { len=fread(&readbuf,sizeof(int),1,fp); printf("read len=%d data=%d\n",len,readbuf); fseek(fp,-sizeof(int),SEEK_END); fread(&readEnd,sizeof(int),1,fp); printf("read file end =%d\n",readEnd); fclose(fp); } else //打开失败,则创建文件 { fp = fopen("./1.txt","wb+"); printf("write 1.txt: fp==NULL=%d size=%d\n",fp == NULL,sizeof(readbuf)); if(fp!=NULL) { len=fwrite(&writebuf,sizeof(int),1,fp); printf("write len=%d \n",len); fclose(fp); } } return 0; }
运行第一次:
运行第二次
char *fgets(char *buf, int bufsize, FILE *stream);
从结构体指针stream中读取,每次读取一行。
读取的数据保存在buf指向的字符数组中,每次最多读取bufsize-1个字符(第bufsize个字符赋'\0'),如果文件中的该行,不足bufsize-1个字符,则读完该行就结束。
如若该行(包括最后一个换行符)的字符数超过bufsize-1,则fgets只一个不完整的行
返回值
bufsize
示例-打印文件全部数据:
#include <stdio.h> #include <stdlib.h> #include <string.h> int main(int argc,char* argv[]) { FILE *fp = NULL; char readbuf[5]; fp = fopen("./1.txt","r+"); printf("(DEBUG)read 1.txt: fp==NULL=%d\n",fp == NULL); if(fp!=NULL) //打开成功,读数据 { while(fgets(readbuf, sizeof(readbuf),fp)) { printf("%s",readbuf); } fclose(fp); } return 0; }
FILE *popen(const char *command, const char *type);
int pclose(FILE *stream);
头文件#include <stdio.h>
示例
#include <stdio.h> #include <stdlib.h> #include <string.h> int run_command(const char *cmd, const char *type) { FILE *fp; int res; char buf[1024];
if ((fp = popen(cmd, type)) ==NULL) { printf("popen err \n"); return -1; } if(type[0]=='r') //如果是读数据 { while(fgets(buf, sizeof(buf),fp)) { printf("%s",buf); } } pclose(fp); return 0; } int main(int argc,char* argv[]) { run_command("vi 1.txt","r"); return 0; }
效果:
int access(const char *pathname, int mode);
头文件:#include <unistd.h>
用来检测访问的文件属性,是否可以读写,存在,执行
mode
模式有以下几种:
#define F_OK 0 /* Check for file existence */ #define X_OK 1 /* Check for execute permission. */ #define W_OK 2 /* Check for write permission */ #define R_OK 4 /* Check for read permission */
示例-检测文件是否存在
#include <stdio.h> #include <unistd.h> int file_exists(char *filename); int main(void) { printf("Does NOTEXIST.FIL exist: %s\n", file_exists("./1.txt") ? "YES" : "NO"); return 0; } int file_exists(char *filename) { return (access(filename, 0) == 0); }
如果在嵌入式linux中,则有可能在写数据后强制关电,此时数据还在缓冲区,并没写到flash中,所以需要在fclose()前面加上:
fflush(fp); //会把缓冲区中的文件写到文件系统中 fsync(fileno(fp)); //同步数据到flash fclose(fp);
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
如何在没有core文件的情况下用dmesg+addr2line定位段错误
用QT制作3D点云显示器——QtDataVisualization
网友评论