1005 fopen函数1

fopen函数

strream open function 

FILE *open(const char *path,const char *mode )这里的函数放心用  常量指针传入 不会修改   

返回的是fille结构体 用一个指针来接受

FILE *fdopen 参数  int fc  整型文件描述符

 

vim test 

char *ptr="abc"; 用一个串常量给字符指针赋值

ptr[0]='x'   ptr==xbc???   取决于这个abc放在哪里 不同编译器结果不一样

 

封装函数给别人用的时候最好加上const修饰确保不会改变

返回值是一个FILE指针  失败会返回NULL指针 并且设置error number 

缺陷  大家都一起用 如果不马上答应 就会被别人覆盖

vim/usr/incluede/asm-generic/errno   errorbash.h    包含了这些这些错误代码  error本身是一个宏定义

 

mkdir stdio

vim stdio.c

#include<stdio.h>

#include<stdlib.h>

int main()

{

  FILE* fp;

  fp=fopen("tmp","ragsdasgasd") 认识第一个字母

  if(fp==NULL)

  {fprintf(stderr,"fopen() failed erroron=%d\n".errno;exit(1);}

//perror("追加在这个串后面 错误信息");  自动关联全局变量errnor

//strerror(int errnum) 返回char* 

//fprintf(stderr,"fopen() %s",strerror(errno))

  puts("ok");

}

 

 

vim test.c【14:01】

#include<erron.h>

 

gcc -E test.c 预处理  所有的#号码开头都是预处理阶段的

于是看不到 error的变量  已经被私有化   数据不会出现和别人冲突

 

 

 

part2

r  The stram is postioned at the begining of the file  从头开始读文件  流定位在文件开头

r+ The stream is postioned at the begining of the file 文件当中的第一个有效字符

只读的时候如果文件不存在,直接返回错误。结束当前调用。

w Truncate file to zero length or create text file for writing 有则清空无则创建

a open for appending (writing at the end of the file) 不存在则会创建 最后一个有效字节的下一个位置

a+ open for reading and appending (writeing at the end of the file) the file is created if it doesnot exist  .The inital file positon for reading is at hte beging of the file .所以文件指针在哪里却决于你的操作

但是如果在linux 中 rb wb 等等都可以忽略。man  fopen  

 

注意头文件很重要

需要按照man手册包含头文件

errno =2  找不到文件 权限 路径 文件泄露

但是怎么查找呢  

通过perror  将系统错误信息   errno转化为错误信息

 

 

make 补充!!!!!!!!!!!!!!!!!

 

posted @ 2021-10-07 18:16  张喆坤  阅读(26)  评论(0编辑  收藏  举报