嵌入式应用开发学习笔记——标准IO

嵌入式应用开发学习笔记——标准IO

文件指针

“文件指针”是缓冲文件系统的一个重要概念,在C系统的头文件stdio.h中定义了一个名叫FILE的结构体类型:

typedef struct
{
	short			level;			//缓冲区饱和程度
	unsigned 		flage;			//文件状态标志
	char			fd;				//文件号
	unsigned char 	hold;			//无缓冲区取消字符输入
	short			bsize;			//缓冲区大小,缺省值512
	unsigned char 	*buffer;		//缓冲区
	unsigned char 	*curp;			//当前活动指针
	unsigned 		istemp;			//草稿文件标识
	short			token;			//作正确性检验
}FILE;

库函数——创建和打开

FILE *fopen(const char *filename, const char *mode)
filename:
打开的文件名(包含路径,缺省为当前路径)
mode:
打开模式
返回值:成功打开返回FILE指针,失败返回NULL
常见打开模式:
r, rb 只读方式打开
w, wb 只写方式打开,如果文件不存在,则创建该文件
a, ab 追加方式打开,如果文件不存在,则创建该文件
r+, r+b, rb+ 读写方式打开
w+, w+b, wh+ 读写方式打开,如果文件不存在,则创建该文件
a+, a+b, ab+ 读和追加方式打开,如果文件不存在,则创建该文件
注意:b用于区分二进制文件和文本文件,这一点在DOS、Windows系统中是有区分的,但linux不区分二进制文件和文本文件

  1 #include <stdio.h>
  2 #include <errno.h>
  3 #include <stdlib.h>
  4 
  5 int main(void)
  6 {
  7     FILE *fp;
  8     fp = fopen("test.txt", "r");
  9     if(fp == NULL)
 10     {
 11         perror("fopen");
 12         exit(1);
 13     }
 14     printf("success open the file\n");
 15     if(fclose(fp) == EOF)
 16     {
 17         perror("fclose");
 18         exit(1);
 19     }
 20     return 0;
 21 }

库函数——读写

size_t fread(void *ptr, size_t size, size_t n, FILE *stream)
功能:从stream指向的文件中读取n个字段,每个字段为size个字节,并将读取的数据放入ptr所指的字符数组中,返回实际已读取的字节数
size_t fwrite(const void *ptr, size_t size, size_t n, FILE *stream)
功能:从缓冲区ptr所指的数组中把n个字段写到stream指向的文件中,每个字段长为size个字节,返回实际写入的字段数
提示:返回0时,使用feof或者ferror函数判断原因

  1 #include <stdio.h>
  2 #include <string.h>
  3 #include <errno.h>
  4 #include <stdlib.h>
  5 
  6 int main(void)
  7 {
  8     int readnum;
  9     FILE *fp;
 10     char buf[1024];
 11     fp = fopen("test.txt", "r");
 12     if(fp == NULL)
 13     {
 14         perror("fopen");
 15         exit(1);
 16     }
 17 
 18     readnum = fread(buf, sizeof(char), 1024, fp);
 19     if(readnum > 0)
 20     {
 21         printf("fread:%s\n", buf);
 22         return 0;
 23     }
 24     if(feof(fp))
 25     {
 26         printf("read file end\n");
 27         return 0;
 28     }
 29     perror("fread");
 30     exit(1);
 31 }
  1 #include <stdio.h>
  2 #include <stdlib.h>
  3 #include <errno.h>
  4 #include <string.h>
  5 
  6 int main(void)
  7 {
  8     FILE *fp;
  9     int writenum;
 10     char buf[100];
 11     fp = fopen("test.txt", "w");
 12     if(fp == NULL)
 13     {
 14         perror("fopen");
 15         exit(1);
 16     }
 17 
 18 
 19     memset(buf, 0, sizeof(buf));
 20     strcpy(buf, "this is second test file for fwrite\n");
 21     writenum = fwrite(buf, sizeof(char), strlen(buf), fp);
 22     if(writenum > 0)
 23     {
 24         fclose(fp);
 25         printf("write ok\n");
 26         system("cat test.txt");
 27         return 0;
 28     }
 29     perror("fwrite");
 30     exit(1);
 31 
 32 }

int fgetc(FILE *stream)
从指定的文件中读一个字符
返回值:返回字符,文件结尾、错误返回EOF, 使用ferror判断
char *fgets(char *s, int size, FILE *stream)
从指定的文件中读取size-1个字符到s中,返回s的首地址
int getc(FILE *stream)
int getchar(void) (getc(c, stdin))
char *gets(FILE *stream)

int fputc(int c, FILE *stream)
向指定的文件中写入一个字符
int fputs(const char *s, FILE *stream)
向指定的文件中写入一个字符串,成功返回0,不成功返回非0
int putc(int c, FILE *stream);
int putchar(void) (putc(c, stdout));
int puts(const char *s);

  1 #include <stdio.h>
  2 #include <stdlib.h>
  3 
  4 int main(void)
  5 {
  6     FILE *fp;
  7     char ch;
  8     if((fp = fopen("test.txt", "r")) == NULL)
  9     {
 10         perror("fopen");
 11         exit(1);
 12     }
 13     ch = fgetc(fp);
 14     while(ch != EOF)
 15     {
 16         putchar(ch);
 17         ch = fgetc(fp);
 18     }
 19     fclose(fp);
 20 
 21     return 0;
 22 }

  1 #include <stdio.h>
  2 #include <stdlib.h>
  3 #include <errno.h>
  4 #include <string.h>
  5 
  6 int main(void)
  7 {
  8     FILE *fp1;
  9     FILE *fp2;
 10 
 11     int readnum = 0;
 12     int writenum = 0;
 13     char buf[1024];
 14     int i;
 15 
 16     fp1 = fopen("test.txt", "r");
 17     fp2 = fopen("1.txt", "w+");
 18 
 19     if((fp1 == NULL) || (fp2 == NULL))
 20     {
 21         perror("fopen");
 22         exit(1);
 23     }
 24 
 25     memset(buf, 0, sizeof(buf));
 26 
 27     readnum = fread(buf, sizeof(char), 1024, fp1);
 28     if(readnum > 0)
 29     {
 30         for(i = 0; i < readnum; i++)
 31         {
 32             fputc(buf[i], fp2);
 33         }
 34     }
 35 
 36     fclose(fp1);
 37     fclose(fp2);
 38 
 39     return 0;
 40 }

库函数——定位

int fseek(FILE *stream, long offset, int whence)
whence:
SEEK_SET 从文件的开始处开始搜索
SEEK_CUR 从当前位置开始搜索
SEEK_END 从文件的结束处开始搜索

  1 #include <stdio.h>
  2 #include <stdlib.h>
  3 #include <string.h>
  4 #include <errno.h>
  5 
  6 int main(void)
  7 {
  8     FILE *fp;
  9     char buf[1024];
 10 
 11     if((fp = fopen("test.txt", "r+")) == NULL)
 12     {
 13         perror("fopen");
 14         exit(1);
 15 
 16     }
 17     memset(buf, 0, sizeof(buf));
 18     fgets(buf, 1024, fp);
 19     printf("%s\n", buf);
 20 
 21     fseek(fp, 0, SEEK_SET);
 22 
 23     memset(buf, 0, sizeof(buf));
 24     strcpy(buf, "this is fgets fputs test, we use fseek function");
 25 
 26     fputs(buf, fp);
 27     fclose(fp);
 28 
 29     return 0;
 30 }

库函数——格式化读写

fscanf(FILE *stream, char *format[, argument…])
从一个流中进行格式化输入
int fprintf(FILE *stream, char *format[, argument…])
格式化输出到一个流中

  1 #include <stdio.h>
  2 #include <errno.h>
  3 #include <stdlib.h>
  4 
  5 int main(void)
  6 {
  7     FILE *fp;
  8     int i[2] = {0};
  9     int k = 0;
 10     if((fp = fopen("num.txt", "r")) == NULL)
 11     {
 12         perror("fopen");
 13         exit(1);
 14     }
 15     for(k = 0; k < 2; k++)
 16     {
 17         fscanf(fp, "%d", &i[k]);
 18         printf("get the number is %d\n", i[k]);
 19     }
 20     return 0;
 21 }

路径获取

在编写程序的时候,有时候需要得到当前路径,C库函数提供了getcwd来解决这个问题。
char *getcwd(char *buffer, size_t size)
我们提供一个size大小的buffer, getcwd会把当前的路径名copy到buffer中,如果buffer太小,函数会返回-1。

创建目录

#include <sys/stat.h>
int mkdir(char *dir, int mode)
功能:创建一个新目录
返回值:0表示成功,-1表示出错。

  1 #include <stdio.h>
  2 #include <unistd.h>
  3 #include <string.h>
  4 #include <sys/stat.h>
  5 
  6 int main(void)
  7 {
  8     char buf[80];
  9 
 10     getcwd(buf, 80);
 11     printf("pwd = %s\n", buf);
 12 
 13     strcat(buf, "/test");
 14     mkdir(buf, 0777);
 15 
 16     if(!access(buf, F_OK))
 17     {
 18         printf("directory create ok\n");
 19     }
 20 
 21     return 0;
 22 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值