C语言标准IO概述
标准IO(Standard Input/Output)是C语言中用于处理文件和数据流的一组函数库,定义在<stdio.h>头文件中。与低级IO(如read/write)相比,标准IO提供了缓冲机制,提高了数据读写的效率。标准IO的核心是FILE结构体指针,通过它操作文件流。
fopen/fclose
功能
- fopen:打开文件并返回文件指针。
- fclose:关闭文件并释放资源。
函数原型
FILE *fopen(const char *filename, const char *mode);
int fclose(FILE *stream);
参数说明
- filename:文件路径。
- mode:打开模式(如"r"只读,"w"写入,"a"追加等)。
- stream:已打开的文件指针。
示例
FILE *fp = fopen("test.txt", "w");
if (fp == NULL) {
perror("Error opening file");
return 1;
}
fclose(fp);
fgetc/fputc
功能
- fgetc:从文件流中读取一个字符。
- fputc:向文件流写入一个字符。
函数原型
int fgetc(FILE *stream);
int fputc(int c, FILE *stream);
示例
FILE *fp = fopen("test.txt", "r");
int ch = fgetc(fp); // 读取一个字符
fputc('A', fp); // 写入字符'A'
fgets/fputs
功能
- fgets:从文件流读取一行字符串。
- fputs:向文件流写入字符串(不自动添加换行符)。
函数原型
char *fgets(char *str, int n, FILE *stream);
int fputs(const char *str, FILE *stream);
参数说明
- str:存储读取数据的缓冲区。
- n:最多读取的字符数(包括\\0)。
示例
char buffer[100];
fgets(buffer, 100, stdin); // 从标准输入读取
fputs("Hello", stdout); // 输出到标准输出
fscanf/fprintf
功能
- fscanf:格式化输入(类似scanf,但针对文件流)。
- fprintf:格式化输出(类似printf,但针对文件流)。
函数原型
int fscanf(FILE *stream, const char *format, …);
int fprintf(FILE *stream, const char *format, …);
示例
int num;
fscanf(fp, "%d", &num); // 从文件读取整数
fprintf(fp, "Number: %d", 42); // 写入格式化字符串
fread/fwrite
功能
- fread:从文件流读取二进制数据块。
- fwrite:向文件流写入二进制数据块。
函数原型
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:要读写的数据项数量。
示例
int arr[5] = {1, 2, 3, 4, 5};
fwrite(arr, sizeof(int), 5, fp); // 写入数组
fread(arr, sizeof(int), 5, fp); // 读取数组
fseek/rewind/ftell
功能
- fseek:设置文件流的位置指针。
- rewind:重置文件指针到开头。
- ftell:返回当前文件指针位置(字节偏移量)。
函数原型
int fseek(FILE *stream, long offset, int whence);
void rewind(FILE *stream);
long ftell(FILE *stream);
参数说明
- offset:偏移量。
- whence:基准位置(SEEK_SET开头,SEEK_CUR当前位置,SEEK_END末尾)。
示例
fseek(fp, 10, SEEK_SET); // 移动到第10字节
rewind(fp); // 回到文件开头
long pos = ftell(fp); // 获取当前位置
利用fread和fwrite实现图片的拷贝
#include "../head.h"
int main(void)
{
FILE *fsrc = NULL;
FILE *fdst = NULL;
ssize_t nret = 0;
char srcfile[256] = {0};
char dstfile[256] = {0};
char tmpbuff[4096] = {0};
printf("请输入源图片:\\n");
fgets(srcfile, sizeof(srcfile), stdin);
srcfile[strlen(srcfile)-1] = '\\0';
printf("请输入目的图片:\\n");
fgets(dstfile, sizeof(dstfile), stdin);
dstfile[strlen(dstfile)-1] = '\\0';
fsrc = fopen(srcfile, "r");
if (NULL == fsrc)
{
perror("fail to fopen");
return -1;
}
fdst = fopen(dstfile, "w");
if (NULL == fdst)
{
perror("fail to fopen");
return -1;
}
while (1)
{
nret = fread(tmpbuff, 1, sizeof(tmpbuff), fsrc);
if (0 == nret)
{
break;
}
fwrite(tmpbuff, 1, nret, fdst);
}
fclose(fsrc);
fclose(fdst);
return 0;
}
统计一个文件中出现最多的字符是哪个,出现了多少次
链表法
#include "../head.h"
#include "cirlist.h"
int find_max_char(char *filename, int *pmaxchar, int *pmaxcnt)
{
FILE *fp = NULL;
linknode *pcirlist = NULL;
linknode *ptmpnode = NULL;
char ch = 0;
pcirlist = create_empty_linklist();
fp = fopen(filename, "r");
if (NULL == fp)
{
perror("fail to fopen");
return -1;
}
while (1)
{
ch = fgetc(fp);
if (EOF == ch)
{
break;
}
/* 如果第一次出现则新增该节点 */
ptmpnode = find_linklist(pcirlist, ch);
if (NULL == ptmpnode)
{
insert_tail_linklist(pcirlist, ch);
}
/* 从链表中找到节点,计数器自加 */
ptmpnode = find_linklist(pcirlist, ch);
ptmpnode->cnt++;
}
fclose(fp);
ptmpnode = find_max_node(pcirlist);
if (ptmpnode != NULL)
{
*pmaxchar = ptmpnode->data;
*pmaxcnt = ptmpnode->cnt;
}
destroy_linklist(&pcirlist);
return 0;
}
int main(void)
{
char filename[256] = {0};
int maxcnt = 0;
int maxchar = 0;
int ret = 0;
printf("请输入要检测的文件名:\\n");
m_fgets(filename);
ret = find_max_char(filename, &maxchar, &maxcnt);
if (ret != 0)
{
printf("fild max char failed\\n");
return -1;
}
printf("出现最多的字符:%c, 次数:%d\\n", maxchar, maxcnt);
return 0;
}
数组法
#include "../head.h"
int find_max_char(char *filename, int *pmaxchar, int *pmaxcnt)
{
FILE *fp = NULL;
int cnt[256] = {0};
char ch = 0;
int max = 0;
int i = 0;
fp = fopen(filename, "r");
if (NULL == fp)
{
perror("fail to fopen");
return -1;
}
while (1)
{
ch = fgetc(fp);
if (EOF == ch)
{
break;
}
cnt[ch]++;
}
fclose(fp);
max = 0;
for (i = 1; i < 256; i++)
{
if (cnt[i] > cnt[max])
{
max = i;
}
}
*pmaxchar = max;
*pmaxcnt = cnt[max];
return 0;
}
int main(void)
{
char filename[256] = {0};
int maxcnt = 0;
int maxchar = 0;
int ret = 0;
printf("请输入要检测的文件名:\\n");
m_fgets(filename);
ret = find_max_char(filename, &maxchar, &maxcnt);
if (ret != 0)
{
printf("fild max char failed\\n");
return -1;
}
printf("出现最多的字符:%c, 次数:%d\\n", maxchar, maxcnt);
return 0;
}
评论前必须登录!
注册