- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
已知fgets libc函数实现内部使用了fgetc()
,我如何使用具有更大缓冲区或其他方法的read()
来加速函数?
例如,我读取 /proc/pid/maps
文件来搜索一些字符串。文件的格式已知,目前我在链接中使用 fgets
实现与 read(fd, &c, 1);
而不是 getc
。我认为从文件中读取单个字节比读取 200 个字节慢。所以我想修改函数以从文件中读取N个字节,然后找到换行符。我认为替换 1 字节读取可以以某种方式加速该功能。
最佳答案
您完全误解了标准 I/O 函数。甚至fgetc
也会被缓冲。使用 strace
测试实际 read
调用的发出。在我的计算机上,读取 /proc/1/maps
:
read(3, "5634f9cf6000-5634f9e44000 r-xp 0"..., 1024) = 1024
read(3, " /lib/x86_64-l"..., 1024) = 1024
read(3, " /lib/x86_64-linux-g"..., 1024) = 1024
read(3, " /lib/x86_64-l"..., 1024) = 1024
read(3, ".0.0\n7feb2b2dc000-7feb2b4db000 -"..., 1024) = 1024
read(3, "0-7feb2b8e7000 r--p 00002000 fd:"..., 1024) = 1024
read(3, "00 rw-p 0001a000 fd:00 145004 "..., 1024) = 1024
read(3, "ux-gnu/liblzma.so.5.2.2\n7feb2c1b"..., 1024) = 1024
read(3, "6_64-linux-gnu/libgcrypt.so.20.2"..., 1024) = 1024
read(3, "000 fd:00 135558 "..., 1024) = 1024
read(3, "--p 0000e000 fd:00 136910 "..., 1024) = 1024
read(3, "001e000 fd:00 131385 "..., 1024) = 1024
read(3, "1.1.0\n7feb2da14000-7feb2da15000 "..., 1024) = 1024
read(3, "0 rw-p 00000000 00:00 0 \n7feb2de"..., 1024) = 1024
read(3, "-237.so\n7feb2e492000-7feb2e69100"..., 1024) = 1024
read(3, " \n7feb2ed15000-7feb2ed36000 rw-p"..., 1024) = 637
read(3, "", 1024) = 0
read
调用尝试读取 1024
字节,而不仅仅是一个。
程序是
#include <stdio.h>
int main(void) {
FILE *f = fopen("/proc/1/maps", "r");
while (1) {
char buf[2048];
if (! fgets(buf, 2048, f)) {
break;
}
}
}
如果 1024 字节对您来说还不够,您可以使用 setvbuf(3) 更改底层缓冲区的大小。 !
关于c - C 中最快的 fgets 实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51221322/
我有以下代码:(cpp14) static int const max_len = 1000; FILE* m_in_log = NULL; FILE* m_log = NULL; ... ... b
标准库函数 fgets() 有两个缺点: 函数的第二个参数是int类型 它在提供的缓冲区中留下一个尾随的换行符 我做了一个类似于 fgets() 的简单函数,排除了上述缺点,试图提高我的一个程序的效率
我看过各种关于在 fgets 之前使用 scanf 可能会导致问题的帖子。但是,对我来说,为什么在 AFTER 之后添加它会导致第一个语句中断,这是没有意义的。 这就是我所拥有的: unsigne
这可能是一个非常愚蠢的问题(可能),但我正在使用 fgets 处理文本文件。没什么太花哨的: while (fgets(buf, sizeof(buf), inputFile) != NULL) 正如
如果我想使用 fgets 从文本中读取多行,根据我的教科书,我会这样做: char str[53]; ... while(fgets(str, max, f)!=NULL){ ... }
我试图在完成未定义数量的另一个输入后获取用户输入。但问题是 while 循环之后的第二个 fgets 永远不会被调用。我用 EOF 结束循环,也许这就是错误。但我不知道应该如何结束循环。 另一个有趣的
我正在尝试将文件中的邮政编码读入 Object * 数组。文件包括123 无处不在柯克兰加州99223 我的 .h 文件看起来像 typedef struct { char *street;
我已经实现了我自己的 fgets(即 myfgets)。当我的文件中有必须由 myfgets 函数读取的 NULL 字符串时,它会打印所有字符串(好)但有一些垃圾(坏),但如果我使用预定义 fgets
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: Why does printf not flush after the call unless a newl
假设 FILE* 有效,请考虑: char buf[128]; if(fgets(buf,sizeof buf,myFile) != NULL) { strlen(buf) == 0; //ca
void verification() { char pass[50]; printf(" Enter Password : "); fgets(pass, 50, stdin
我想知道如何从汇编代码中调用fgets。我读到的这些问题与这个问题完全相同:How to call fgets in x86 assembly?这个:How to use c library func
我有一个程序,用户可以在其中输入两个输入。由于我无法控制用户输入的内容,因此用户可以超过数组的固定大小。由于 fgets() appends 在空字符之前保留了一个换行符,如果用户超出预期大小时换行符
我想使用 fgets() 简单地读取一行并去掉换行符。这是否适用于所有情况,包括 Windows 和 UN*X? fgets(buf, sizeof buf, stdin); strtok(buf,
我正在运行以下代码: #include #include #include int main(){ FILE *fp; if((fp=fopen("test.txt","r"))==N
如果我有以下缓冲区: char buffy[40]; // includes \0 at the end fgets 函数应该有 STLEN 40 还是 39?为什么? char buffy[40];
使用fgets输入字符串,我对读取的字符串长度有疑问。 例如,考虑以下程序。 char str[50]; int i; int len; printf("Enter name:\n"); fgets(
fgets()函数是否将文件指针自动移动到该位置,直到我提到的大小参数为止? 例如 : p.txt文件的内容是“我是个好男孩”。使用fgets(a,5,fp1)之后 文件指针会向前移动5个位置吗? 在
gcc 4.4.4 c89 我正在读取一个文件,在该文件中,我必须对每一行进行一些操作。 在一个循环中,我调用了 2 个函数,它们传入此文件指针以对文件执行 2 种不同的操作。 但是,当我使用 fge
我试图从文件中获取我的 9x9 数字网格并将其存储在 2D int 数组中。为此,我编写了代码,以便它使用文件中的 fgets(),然后将 char 数组值转换为 int,然后将其存储在数组中的相应位
我是一名优秀的程序员,十分优秀!