- 使用 Spring Initializr 创建 Spring Boot 应用程序
- 在Spring Boot中配置Cassandra
- 在 Spring Boot 上配置 Tomcat 连接池
- 将Camel消息路由到嵌入WildFly的Artemis上
(4)fputs()
(5)fprintf()
(6)fscanf()
(7)fwrite()
(8)fread()
4.1 对比一组函数
4.1.1 了解sscanf()与sprintf()函数
5.1 fseek()
5.2 ftell()
5.3 rewind()
文本文件和二进制文件
文件读取结束的判定
7.1 被错误使用的feof
8.文件缓冲区
8.1 什么是文件缓冲区
8.2 缓冲区存在的意义
8.3 缓冲区的分类
作用:写一个字符串到文件流里去
#include<stdio.h>
#include<errno.h>
int main()
{
char arr[1024] = { 0 };
FILE* pf = fopen("text.txt", "w");
if (pf == NULL)
{
printf("%s\n", strerror(errno));
}
fputs("hello", pf);
fputs("world", pf);
fclose(pf);
pf = NULL;
}
运行后文件截图:
注意:fputs()函数不会自动带换行符
函数fgets()和函数fputs()函数直接应用举例:
#include<stdio.h>
int main()
{
//从键盘读取一行信息
char buf[1024] = { 0 };
fgets(buf, 1024, stdin);//从标准输入流读取
fputs(buf,stdout);//输出到标准输出流
//上面这两行等价于下面的这两行
//gets(buf);
//puts(buf);
return 0;
}
运行截图:
上面之所以出现一个换行符是因为我们在输入的末尾输入了一个换行符。
对比一下printf()
使用举例:
#include<stdio.h>
#include<string.h>
#include<errno.h>
struct S
{
int n;
float score;
char arr[10];
};
int main()
{
struct S s = { 100,3.14f,"bit" };
FILE* pf = fopen("text.txt", "w");
if (pf == NULL)
{
printf("%s\n", strerror(errno));
return 0;
}
fprintf(pf, "%d %f %s", s.n, s.score, s.arr);
fclose(pf);
pf = NULL;
return 0;
}
运行后截图:
对比scanf()
使用举例:
#include<stdio.h>
#include<string.h>
#include<errno.h>
struct S
{
int n;
float score;
char arr[10];
};
int main()
{
struct S s = {0};
FILE* pf = fopen("text.txt", "r");
if (pf == NULL)
{
printf("%s\n", strerror(errno));
return 0;
}
fscanf(pf, "%d %f %s", &s.n, &s.score, &s.arr);
printf("%d %f %s", s.n, s.score, s.arr);
fclose(pf);
pf = NULL;
return 0;
}
运行截图:
函数fprintf()和fscanf()的直接应用举例:
#include<stdio.h>
struct S
{
int n;
float score;
char arr[10];
};
int main()
{
struct S s = {0};
fscanf(stdin, "%d %f %s", &s.n, &s.score, &s.arr);
fprintf(stdout, "%d %f %s", s.n, s.score, s.arr);
return 0;
}
运行截图:
** buffer:指针指向被写入的数据**
size:被写入元素的大小,单位是字节
count:被写入的数据的数目
stream:写入的目的流
使用举例:
#include<stdio.h>
struct S
{
char name[20];
int age;
double score;
};
int main()
{
struct S s = { "张三",20,55.6 };
FILE* pf = fopen("text.txt", "wb");
if (pf == NULL)
{
return 0;
}
//以二进制的形式写文件
fwrite(&s, sizeof(struct S), 1, pf);
fclose(pf);
pf = NULL;
return 0;
}
** buffer:存储数据的位置**
size:元素的大小
count:读取元素的数目
stream:指针指向的文件结构
使用举例:
#include<stdio.h>
struct S
{
char name[20];
int age;
double score;
};
int main()
{
struct S tmp = { 0 };
FILE* pf = fopen("text.txt", "rb");
if (pf == NULL)
{
return 0;
}
//以二进制的形式读文件
fread(&tmp, sizeof(struct S), 1, pf);
printf("%s %d %lf", tmp.name, tmp.age, tmp.score);
fclose(pf);
pf = NULL;
return 0;
}
运行截图:
scanf/fscanf/sscanf printf/fprintf/sprintf
(1)sscanf()
作用描述:从一个字符串中读取一段格式化的数据。
使用举例:
#include<stdio.h>
struct S
{
int n;
float score;
char arr[10];
};
int main()
{
struct S s = { 100,3.14,"hello"};
struct S tmp = { 0 };
char buf[1024] = { 0 };
//把格式化的数据转换成字符串存储到buf中
sprintf(buf, "%d %f %s", s.n, s.score, s.arr);
//从buf中读取格式化的数据到tmp中
sscanf(buf, "%d %f %s", &(tmp.n), &(tmp.score), &(tmp.arr));
printf("%d %f %s\n", tmp.n, tmp.score, tmp.arr);
return 0;
}
运行截图:
(2)sprintf()
作用描述:将格式化的数据写入到一个字符串中去。
使用举例:
#include<stdio.h>
struct S
{
int n;
float score;
char arr[10];
};
int main()
{
struct S s = { 100,3.14,"hello"};
char buf[1024] = { 0 };
sprintf(buf, "%d %f %s", s.n, s.score, s.arr);
printf("%s", buf);
return 0;
}
scanf/printf 是针对标准输入流/标准输出流的 格式化输入/输出语句。
fscanf/fprintf 是针对所有输入流/所有输出流的 格式化输入输出语句,包含上面函数的功能。
sscanf/sprintf sscanf是从字符串中读取格式化的数据,sprintf是把格式化的数据输出成(存储到)字符串。
作用:移动文件指针到一个特定的位置
stream:要移动的文件指针
offset:偏移量(可以为负数,整数就是向右偏移,负数就是向左偏移)
origin:文件指针的当前位置
orifon起始位置的三个选择:
SEEK_CUR:文件指针的当前位置
SEEK_END:文件的末尾位置(在最后一个字符的后面,比如在下面的例子中就是在f的后面)
SEEK_SET:文件的起始位置
使用举例:
文件初始截图:
#include<stdio.h>
#include<errno.h>
#include<string.h>
int main()
{
FILE* pf = fopen("test.txt", "r");
if (pf == NULL)
{
printf("%s\n", strerror(errno));
return 0;
}
//1.定位文件指针
fseek(pf, 2, SEEK_CUR);
//2.读取文件
int ch = fgetc(pf);
printf("%c", ch);
fclose(pf);
pf = NULL;
return 0;
}
运行截图:
作用:计算文件指针举例相对起始位置的偏移量
使用举例:
#include<stdio.h>
#include<errno.h>
#include<string.h>
int main()
{
FILE* pf = fopen("test.txt", "r");
if (pf == NULL)
{
printf("%s\n", strerror(errno));
return 0;
}
//1.定位文件指针
fseek(pf, 2, SEEK_CUR);
//2.计算偏移量
int pos = ftell(pf);
printf("%d\n", pos);
fclose(pf);
pf = NULL;
return 0;
}
运行截图:
注意:fgetc()函数读取过后文件指针会自动向后偏移一位
#include<stdio.h>
#include<errno.h>
#include<string.h>
int main()
{
FILE* pf = fopen("test.txt", "r");
if (pf == NULL)
{
printf("%s\n", strerror(errno));
return 0;
}
fgetc(pf);
int pos = ftell(pf);
printf("%d\n", pos);
fclose(pf);
pf = NULL;
return 0;
}
运行截图:
作用:使文件指针回到文件的起始位置(此时举例文件起始位置的偏移量为0)。
使用举例:
#include<stdio.h>
#include<errno.h>
#include<string.h>
int main()
{
FILE* pf = fopen("test.txt", "r");
if (pf == NULL)
{
printf("%s\n", strerror(errno));
return 0;
}
fgetc(pf);
rewind(pf);
int pos = ftell(pf);
printf("%d\n", pos);
fclose(pf);
pf = NULL;
return 0;
}
运行截图:
根据数据的组织形式,数据文件被称为文本文件或者二进制文件。
数据在内存中以二进制的形式存储,如果不加转换的输出到外存,就是二进制文件。
如果要求在外存上以ASCII码的形式存储,则需要在存储前转换。以ASCII字符的形式存储的文件就是文本文件。
一个数据在内存中是怎么存储的呢?
字符一律以ASCII形式存储,数值型数据既可以用ASCII形式存储,也可以使用二进制形式存储。
如有整数10000,如果以ASCII码的形式输出到磁盘,则磁盘中占用5个字节(每个字符一个字节),而 二进制形式输出,则在磁盘上只占4个字节(VS2013测试)。
10000在内存中的存储形式:10 27 00 00(16进制形式显示的)
测试代码:
#include <stdio.h>
int main()
{
int a = 10000;
FILE* pf = fopen("test.txt", "wb");
fwrite(&a, 4, 1, pf);//二进制的形式写到文件中
fclose(pf);
pf = NULL;
return 0;
}
牢记:在文件读取过程中,不能用feof函数的返回值直接用来判断文件的是否结束。 而是应用于当文件读取结束的时候,判断是读取失败结束,还是遇到文件尾结束。
文本文件读取是否结束,判断返回值是否为 EOF ( fgetc ),或者 NULL ( fgets ),如果是,就返回一个非0的值。 例如: fgetc 判断是否为 EOF fgets 判断返回值是否为 NULL
二进制文件的读取结束判断,判断返回值是否小于实际要读的个数。 例如: fread判断返回值是否小于实际要读的个数。
正确的使用: 文本文件的例子:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int c; // 注意:int,非char,要求处理EOF
FILE* fp = fopen("test.txt", "r");
if (!fp) {
perror("File opening failed");
return EXIT_FAILURE;
}
//fgetc 当读取失败的时候或者遇到文件结束的时候,都会返回EOF
while ((c = fgetc(fp)) != EOF) // 标准C I/O读取文件循环
{
putchar(c);
}
//判断是什么原因结束的
if (ferror(fp))
puts("I/O error when reading");
else if (feof(fp))
puts("End of file reached successfully");
fclose(fp);
}
二进制文件的例子:
#include <stdio.h>
enum { SIZE = 5 };
int main(void)
{
double a[SIZE] = { 1.,2.,3.,4.,5. };
FILE* fp = fopen("test.bin", "wb"); // 必须用二进制模式
fwrite(a, sizeof * a, SIZE, fp); // 写 double 的数组
fclose(fp);
double b[SIZE];
fp = fopen("test.bin", "rb");
size_t ret_code = fread(b, sizeof * b, SIZE, fp); // 读 double 的数组
if (ret_code == SIZE) {
puts("Array read successfully, contents: ");
for (int n = 0; n < SIZE; ++n) printf("%f ", b[n]);
putchar('\n');
}
else { // error handling
if (feof(fp))
printf("Error reading test.bin: unexpected end of file\n");
else if (ferror(fp)) {
perror("Error reading test.bin");
}
}
fclose(fp);
}
ANSIC 标准采用“缓冲文件系统”处理的数据文件的,所谓缓冲文件系统是指系统自动地在内存中为程序 中每一个正在使用的文件开辟一块“文件缓冲区”。从内存向磁盘输出数据会先送到内存中的缓冲区,装 满缓冲区后才一起送到磁盘上。如果从磁盘向计算机读入数据,则从磁盘文件中读取数据输入到内存缓 冲区(充满缓冲区),然后再从缓冲区逐个地将数据送到程序数据区(程序变量等)。缓冲区的大小根 据C编译系统决定的。
#include <stdio.h>
#include <windows.h>
//VS2013 WIN10环境测试
int main()
{
FILE* pf = fopen("test.txt", "w");
fputs("abcdef", pf);//先将代码放在输出缓冲区
printf("睡眠10秒-已经写数据了,打开test.txt文件,发现文件没有内容\n");
Sleep(10000);
printf("刷新缓冲区\n");
fflush(pf);//刷新缓冲区时,才将输出缓冲区的数据写到文件(磁盘)
//注:fflush 在高版本的VS上不能使用了
printf("再睡眠10秒-此时,再次打开test.txt文件,文件有内容了\n");
Sleep(10000);
fclose(pf);
//注:fclose在关闭文件的时候,也会刷新缓冲区
pf = NULL;
return 0;
}
这里可以得出一个结论: 因为有缓冲区的存在,C语言在操作文件的时候,需要做刷新缓冲区或者在文件操作结束的时候关闭文 件。 如果不做,可能导致读写文件的问题。
首先,把若干字符作为一个块传输比逐个发送字符节省时间;其次,如果用户打错字符,可以直接通过键盘修改错误。当最后按下Enter键时,发送的才是正确的输入。
C语言在不同的地方根据需要使用不同的缓冲区。
缓冲区分为两类:完全缓冲I/O和行缓冲I/O和不带缓冲。
(1)完全缓冲是指的是当前缓冲区被填满时才刷新缓冲区(内容被发送至目的地),通常出现在文件输入中。缓冲区的大小取决于系统,常见的大小是512字节和4096字节。
(2)行缓冲指的是在出现换行时刷新缓冲区。键盘输入通常是行缓冲输入,所以在按下Enter键时才刷新缓冲区。常见的例子就是getchar()函数,当程序调用getchar()函数时,程序就等着用户按键,用户输入的字符被存放在键盘缓冲区中,直到用户按回车为止(回车字符也放在缓冲区中)。当用户键入回车之后,getchar()函数才开始从键盘缓冲区中每次读入一个字符。也就是说,后续的getchar()函数调用不会等待用户按键,而直接读取缓冲区中的字符,直到缓冲区中的字符读完后,才重新等待用户按键。当缓冲区满时不会自动刷新且无法继续输入,需要按回车才可以刷新缓冲区。
(3)不带缓冲:标准输出不带缓冲(例如:cerr
(4)缓冲区的刷新(执行真正的I/O操作)
当缓冲区满时:执行flush语句;执行end语句;关闭文件。
我正在做一个关于代码学院的教程,我在这里收到一个错误,说“看起来你的函数没有返回‘唉,你没有资格获得信用卡。资本主义就是这样残酷。’”当收入参数为 75 时。”但是该字符串在控制台中返回(由于某种原因
我正在阅读 Go 的官方教程,但很难理解 Channel 和 Buffered Channels 之间的区别。教程的链接是 https://tour.golang.org/concurrency/2和
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
关闭。这个问题是off-topic .它目前不接受答案。 想改进这个问题? Update the question所以它是on-topic对于堆栈溢出。 9年前关闭。 Improve this que
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,因为
作为 iOS 新手,有大量书籍可以满足学习基础知识的需求。现在,我想转向一些高级阅读,例如 OAuth 和 SQLite 以及动态 API 派生的 TableView 等。您可以推荐任何资源吗? 最佳
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
关闭。这个问题是opinion-based .它目前不接受答案。 想要改进这个问题? 更新问题,以便 editing this post 可以用事实和引用来回答它. 关闭 8 年前。 Improve
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 8 年前。
前言 很多同学都知道,我们常见的CTF赛事除了解题赛之外,还有一种赛制叫AWD赛制。在这种赛制下,我们战队会拿到一个或多个服务器。服务器的连接方式通常是SSH链接,并且可能一个战队可能会同时有
Memcached是一个自由开源的,高性能,分布式内存键值对缓存系统 Memcached 是一种基于内存的key-value存储,用来存储小块的任意数据(字符串、对象),这些数据可以是数据库调用、A
Perl 又名实用报表提取语言, 是 Practical Extraction and Report Language 的缩写 Perl 是由 拉里·沃尔(Larry Wall)于19
WSDL 是 Web Services Description Language 的缩写,翻译成中文就是网络服务描述语言 WSDL 是一门基于 XML 的语言,用于描述 Web Services 以
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 6年前关闭。 Improve thi
我正在寻找解释在 WPF 中创建自定义用户控件的教程。 我想要一个控件,它结合了一个文本 block 、一个文本框和一个启动通用文件打开对话框的按钮。我已经完成了布局,一切都连接好了。它有效,但它是三
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我接近 fourth page of the Django tutorial 的开始看着vote查看,最后是这样的: # Always return an HttpResponseRedirect a
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
是否有任何好的 Qt QSS 教程,或者在某个地方我可以看到样式小部件的示例?如果某处可用,我想要一些完整的引用。除了有关如何设置按钮或某些选项卡样式的小教程外,我找不到任何其他内容。 最佳答案 Qt
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引起辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the he
我是一名优秀的程序员,十分优秀!