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

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

作者: 特立独行的佩奇 | 来源:发表于2022-10-08 21:41 被阅读0次
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 

相关文章

网友评论

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

      本文链接:https://www.haomeiwen.com/subject/oalnartx.html