3.1.1、标准输入输出函数
3.1.1.1、printf
和 fprintf
- 功能:向标准输出或文件输出格式化字符串。
- 示例:(需要在执行文件同级目录下新建一个output.txt的文件)
#include <stdio.h>
int main()
{
int a = 10;
float b = 3.14;
printf("数值为:%d,浮点数为:%.2f\n", a, b); // 输出到屏幕
FILE *file = fopen("output.txt", "w");
fprintf(file, "文件输出:数值:%d,浮点数:%.2f\n", a, b); // 输出文件
fclose(file);
return 0;
}
3.1.1.2、scanf
和 fscanf
- 功能:从标准输入或文件中读取格式化数据。
- 示例:(注意:先写入数据,然后在读取数据,避免手动输入造成错误)
#include <stdio.h>
int main()
{
int a;
float b;
printf("请输入一个数值和浮点数:\n");
scanf("%d %f", &a, &b); // 从用户输入中读取数据
printf("输入的数组是:%d.浮点数是:%.2f\n", a, b);
FILE *file_w = fopen("intput.txt", "w");
fprintf(file_w, "文件输出:数值:%d,浮点数:%.2f\n", a, b); // 输出文件
fclose(file_w);
FILE *file = fopen("input.txt", "r");
fscanf(file, "%d %f", &a, &b); // 从文件中读取数据
printf("文件中的数组是:%d.浮点数是:%.2f\n", a, b);
fclose(file);
return 0;
}
3.1.1.3、puts
和 gets
- 功能:处理整行字符串的输入输出。
- 注意:
gets
在C11中已被弃用,推荐使用fgets
。 - 示例:(sizeof计算出申请的字符串长度,此例中输出长度为100)
#include <stdio.h>
int main()
{
char str[100];
printf("请输入一个字符串:\n");
fgets(str, sizeof(str), stdin); // 安全从用户输入中读取字符串
puts("输入的字符串是:");
puts(str);
return 0;
}
3.1.2、文件操作函数
3.1.2.1、fopen
和 fclose
- 功能:打开或关闭文件。
- 示例:
#include <stdio.h>
int main()
{
FILE *file = fopen("input.txt", "w"); // 打开文件(w代表写入值)
if (file == NULL)
{
printf("文件打开失败\n");
return 1;
}
fprintf(file, "文件输出:数值:%d,浮点数:%.2f\n", 10, 3.14); // 输出文件
fclose(file);
int a[100];
FILE *file_r = fopen("input.txt", "r"); // 打开文件(r代表读取值)
fscanf(file_r, "%s", &a); // 从文件中读取数据
printf("文件中的数据是:%s.\n", a);
fclose(file_r);
return 0;
}
3.1.2.2、fread
和 fwrite
- 功能:二进制文件的读取与写入。
- 示例:
#include <stdio.h>
struct Data
{
int id;
char name[20];
};
int main()
{
struct Data d1 = {1, "Alice"}, d2;
FILE *file = fopen("input.bin", "wb"); // 打开文件(wb代表写入二进制值)
fwrite(&d1, sizeof(struct Data), 1, file); // 输出文件
fclose(file);
file = fopen("input.bin", "rb"); // 打开文件(rb代表读取二进制值)
fread(&d2, sizeof(struct Data), 1, file); // 从文件中读取数据
printf("文件中的数据是:ID = %d, Name = %s.\n", d2.id, d2.name);
fclose(file);
return 0;
}
3.1.2.3、fseek
和 ftell
- 功能:文件指针的移动和获取。
- 示例:
#include <stdio.h>
int main()
{
FILE *file = fopen("input.txt", "w+");
fputs("Hello, World!", file);
fseek(file, 7, SEEK_SET); // 将文件指针移动到文件开头的第7个字节
fputs("C语言程序测试", file);
fseek(file, 0, SEEK_END); // 将文件指针移动到文件末尾
long size = ftell(file); // 获取文件大小
printf("文件大小:%ld字节\n", size);
fclose(file);
return 0;
}
3.1.3、字符操作函数
3.1.3.1、fgetc
和 fputc
- 功能:处理单个字符的读取与写入。
- 示例:
#include <stdio.h>
int main()
{
FILE *file = fopen("input.txt", "w+");
fputc('A', file); // 写入字符
fputc('B', file);
rewind(file); // 重置文件指针
printf("文件中的字符是:");
printf("%c", fgetc(file)); // 读取字符
printf("%c\n", fgetc(file));
fclose(file);
return 0;
}
3.1.3.2、getc
和 putc
- 功能:类似于
fgetc
和fputc
,但更通用。 - 示例:
#include <stdio.h>
int main()
{
FILE *file = fopen("input.txt", "w+");
putc('X', file);
putc('Y', file);
rewind(file); // 重置文件指针
printf("文件中的字符是:%c%c\n", getc(file), getc(file));
fclose(file);
return 0;
}
3.1.4、错误处理函数
3.1.4.1、perror
- 功能:输出最后一次错误信息。
- 示例:
#include <stdio.h>
#include <errno.h>
int main()
{
FILE *file = fopen("input.txt", "r");
if (file == NULL)
{
perror("打开文件时发生错误");
return 1;
}
return 0;
}
3.1.4.2、clearerr
和 feof
- 功能:清除文件流错误标志和检测文件结束。
- 示例:
#include <stdio.h>
int main()
{
FILE *file = fopen("input.txt", "r");
if (file == NULL)
{
perror("打开文件时发生错误");
return 1;
}
while (!feof(file))
{
char c = fgetc(file);
if (ferror(file))
{
perror("读取文件时发生错误");
break;
}
putchar(c);
}
clearerr(file); // 清除错误状态
fclose(file);
return 0;
}
3.1.5、临时文件和缓冲
3.1.5.1、tmpfile
和 tmpnam
- 功能:创建临时文件。
- 示例:
#include <stdio.h>
int main()
{
FILE *file = tmpfile();
if (file == NULL)
{
perror("创建临时文件时发生错误");
return 1;
}
fprintf(file, "临时文件\n");
rewind(file);
char name[20];
fgets(name, sizeof(name), file);
printf("文件内容:%s\n", name);
fclose(file);
return 0;
}