C 语言学习(3) ---- C语言文件和基本I/O 操作

ANSI C File 操作

File 操作参考网址 : https://zh.cppreference.com/w/c/io

API接口 说明
fopen int fopen(const char* filename,const char *mode)
fclose int fclose(FILE * stream)
ftell ftell(FILE* stream) 获取文件位置的指针
feof feof(FILE*stream) 如果到了文件末尾 返回 非0的值 否则返回0
rewind void rewind(FILE *stream) 将文件指针移动到文件的开头位置
remove remove(const char *filename)
rename rename(const char*oldname,const char *newname)
fflush int fflush(FILE*stream); 刷新文件缓冲区
fgets char* fgets(char str,int num , FILEstream) 从文件中读取一行
fputs int fputs(const char *str,FILE *stream); 输出一行字符串到文件中
fread int fread(void*buffer,int size,int count,FILE *stream)
fwrite int fwrite(void *buffer,int size,int count,FILE *stream)
fseek int fseek(FILE*stream,long int offset,int origin)
fprintf int fprintf(FILE*stream,const char *format[,argument])
fputc/fgetc 从文件中读取/输出一个字符
fopen 函数原型

int fopen(const char* filename,const char *mode)
mode 可选项如下:

mode 说明
r 以只读方式打开文件,该文件必须存在
w 打开只写文件,如果文件不存在,则会创建一个新文件;程序会从文件的开头写入内容,如果文件存在,则该会被截断为零长度,重新写入
a 以追加模式写入文件,如果文件不存在,则会创建一个新文件,如果文件存在,程序会在已有的文件内容中追加内容
r+ 打开一个文本文件,允许读写文件,该文件必须存在
w+ 打开一个文本文件,允许读写文件;如果文件已存在,则文件会被截断为零长度,如果文件不存在,则会创建一个新文件
a+ 打开一个文本文件,允许读写文件;如果文件不存在,则会创建一个新文件;读取会从文件的开头开始,写入则只能是追加模式

如果打开的二进制文件,需要使用下面的访问模式:

rb", "wb", "ab", "rb+", "r+b", "wb+", "w+b", "ab+", "a+b"

预定义标准流:
stdin:和标准输入流关联的 File* 类型
stdout:和标准输出流关联的 File* 类型
stderr:和标准错误输出流关联的 File* 类型

int main(int argc,char * argv[])
{
MI_PRINT("This is my standard C demo! %s build date:%s %s\n",__FUNCTION__,__DATE__,__TIME__);
MI_PRINT("argv[1] is %s \n", argv[1]); // argv[0] is programme name
FILE *fp = fopen(argv[1],"w+");
CHECK_RET(fp);

MI_S32 count = 0;
while( !feof(fp))
{
count = fread(read_buffer,sizeof(MI_U8),buffer_size,fp);
// ftell(FILE* stream) 获取文件位置的指针
DBG_ERR("the read buffer length is %d content is %s \n",count,read_buffer);
MI_PRINT("The Current file location is %d \n",ftell(fp));
}
rewind(fp);  
MI_PRINT("The Current file location is %d \n",ftell(fp));

strcpy(write_buffer,"this is tonychen add  hahaha !!\n");
count = fwrite(write_buffer,sizeof(MI_U8),strlen(write_buffer),fp);
MI_PRINT("The write count is %d \n",count);

fflush(fp);
//MI_PRINT("The Current file location is %d \n",ftell(fp));

rewind(fp);  
fgets(read_buffer,20,fp);
MI_PRINT("The readbuffer is %s \n",read_buffer);

fgets(read_buffer,100,fp);
MI_PRINT("The readbuffer is %s \n",read_buffer);

MI_S8 ret = -1;
if( (ret = remove("my.txt")) == 0) {
    MI_PRINT("remove file success !! \n");
} else {
    MI_PRINT("remove file fail \n");
}

ret = rename("my1.txt","my2.txt");
MI_PRINT("The ret value of my.txt is %d \n",ret);
rewind(fp);
fgets(read_buffer,100,stdin);
MI_PRINT("The readbuffer is %s \n",read_buffer);


fseek(fp,0,SEEK_SET);
fputs(read_buffer,fp);

fseek(fp,0,SEEK_SET);
MI_PRINT("The Current file location is %d \n",ftell(fp));
fseek(fp,0,SEEK_END);
MI_PRINT("The Current file location is %d \n",ftell(fp));
fseek(fp,-20,SEEK_END);
MI_PRINT("The Current file location is %d \n",ftell(fp));
rewind(fp);
fseek(fp,100,SEEK_CUR);
MI_PRINT("The Current file location is %d \n",ftell(fp));
fseek(fp,0,SEEK_END);
fprintf(fp,"This is MY demo %s %s \n",__DATE__,__TIME__);

fclose(fp);
return 0;
}

基本输入输出函数
元素 说明
printf 向终端输出格式化字符串
puts/gets puts(const char* buffer)
getc getchar getc(char c,File* stream)
scanf 从终端按照格式获取内容
sscanf 从Buffer 中按照格式获取内容
sprintf 按照格式化字符串组成buffer
  • sscanf 用法详解
    sscanf int sscanf(char *buffer,const char *format[argument])

format:
%s 字符串 读取字符串截止的标志是 空格 跳格 或者 换行
%*s 忽略某个字符串
%[^/] 匹配字符 /
%[^=] 匹配字符 =
%*[^/] 忽略开始到 / 中间的字符串
%4s 截取前面四个字符串

void testsscanfOperation() {

    printf(" %s start...... \n", __FUNCTION__);
    uint8_t ValueA = 0;
    uint8_t ValueB = 0;
    uint8_t StringBufferP[100] = { 0 };
    uint8_t StringBufferV[100] = { 0 };
    uint8_t InputBuffer[100] = { 0 };

#if 0
    /* %*s 表示忽略 某个字符串  */
    /*读取字符串截止的标志是 空格 跳格 或者 换行*/
    strcpy(InputBuffer, "3+5=4");
    //strcpy(InputBuffer, "tony like reading");
    sscanf(InputBuffer, "%d %s %d", &ValueA, StringBufferP, &ValueB);
    printf("The ValueA is %d ValueB is %d \n", ValueA, ValueB);
#endif

    strcpy(InputBuffer, "tony like reading");
    sscanf(InputBuffer, "%s %*s %s", StringBufferP, &StringBufferV);
    printf("The stringbufferP is %s \n", StringBufferP);
    printf("The stringbufferV is %s \n", StringBufferV);

    /*用于字符串的截取 特定的长度*/
    strcpy(InputBuffer, "GodBlessyou");
    sscanf(InputBuffer, "%3s", StringBufferP);
    sscanf(InputBuffer, "%8s", StringBufferV);
    printf("The stringbufferP is %s \n", StringBufferP);
    printf("The stringbufferV is %s \n", StringBufferV);


    strcpy(InputBuffer, "bootvideo=/system/media/bootvideo.mp4");
    sscanf(InputBuffer, "%[^=] %*1s %s", StringBufferP, StringBufferV); // 忽略掉 = 字符
    printf("The stringbufferP is %s \n", StringBufferP);
    printf("The stringbufferV is %s \n", StringBufferV);

    strcpy(InputBuffer, "/system/media/bootvideo.mp4");
    sscanf(InputBuffer, "%*[^/] %s %*[^/]", StringBufferP);
    printf("The stringbufferP is %s \n", StringBufferP);

    // 截取 空格之前的字符串
    sscanf("123456 abcdedf", "%[^ ]", StringBufferP);
    printf("%s\n", StringBufferP);

    // 表示只是匹配 1-9a-z 的字符串
    sscanf("123456abcdedfBCDEF", "%[1-9a-z]", StringBufferP);
    printf("%s\n", StringBufferP);

    printf("--------------------- \n");
    memset(InputBuffer, 0x00, sizeof(InputBuffer));
    ValueA = 100;
    ValueB = 200;
    sprintf(InputBuffer, "%d is %s %d %s %d", ValueA + ValueB, "equal", ValueA, "+", ValueB);
    printf("The InputBuffer is %s \n", InputBuffer);
}
运行结果:
demoMain Run ....
 testsscanfOperation start...... 
The stringbufferP is tony 
The stringbufferV is reading 
The stringbufferP is God 
The stringbufferV is GodBless 
The stringbufferP is bootvideo 
The stringbufferV is /system/media/bootvideo.mp4 
The stringbufferP is bootvideo 
123456
123456abcdedf
--------------------- 
The InputBuffer is 300 is equal 100 + 200 
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 218,386评论 6 506
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,142评论 3 394
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,704评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,702评论 1 294
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,716评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,573评论 1 305
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,314评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,230评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,680评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,873评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,991评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,706评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,329评论 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,910评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,038评论 1 270
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,158评论 3 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,941评论 2 355

推荐阅读更多精彩内容