是否可以为此使用 stdio 缓冲区?如果现在,有什么替代方案(不亲自实施)?

发布于 2024-12-15 17:08:20 字数 695 浏览 0 评论 0原文

(注意:这个问题是针对我正在做的一些功课的。我不需要知道如何实现它,只需要知道它是否可能以及如何了解更多信息)

我想使用一个用户仅使用文件描述符而不使用 FILE* 的文件的级别缓冲区。我知道 stdio.h 至少为所有 *printf()*scanf() 函数提供 1 个 fifo 用户级缓冲区。

我的问题:

我被要求做一个使用 write() linux 函数的系统。该函数是系统调用。这里的目标是完成教授给我的一个额外的小挑战,即:

使用或创建用户级缓冲区来写入这些文件。

因为我使用函数 write 并且使用文件描述符,所以我无法使用任何 fprintf() 函数,因为有记录表明混合系统调用(write 和 *fprintf)会产生未定义的结果。

是否有另一种方法可以使用与 stdio 相同的缓冲区,或者是否有 linux 的库“提供”的等效缓冲区来写入该文件? (注意:我还需要知道如何刷新该缓冲区)

我在制作 fifo 缓冲区时没有遇到特殊问题,但如果我可以节省一些工作,那将很有用。

注意:我的代码适用于linux。如果有可能实现此目的的可移植代码,请告诉我有关可移植代码的信息。如果没有任何可移植的代码,那么请给我linux的方式。

(note: This question is for some schoolwork I'm doing. I don't need to know how to implement this, just need to know if it's possible and how can I learn more about it)

I'd like to use a user level buffer for a file using only a file descriptor and not using a FILE*. I know stdio.h has, at least, 1 fifo user level buffer for all *printf() and *scanf() functions.

My question:

I was asked to do a system that uses the write() linux function. That function is a system call. The objective here is to complete a small extra challenge I was given by the professor which is:

Use or make a user level buffer for the writting of those files.

Because I use the function write and I use file descriptors I cannot use any of the fprintf() functions because it is documented that mixing system calls (write and *fprintf) has an undefined result.

Is there an alternative way to use the same buffer as stdio or is there an equivalent buffer "offered" by linux's libraries to write to that file? (Note: I also need to know how to flush that buffer)

I don't have special problems making a fifo buffer, but If I can save some work, that'll be useful.

Note: My code is for linux. If there is portable code possible for this, please tell me about the portable code. If there isn't any portable code, then please give me linux's way.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风吹短裙飘 2024-12-22 17:08:20

当然。您可以在缓冲区中收集任何您想要的内容,并write()这个缓冲区。

尝试一下:

...
size_t cursize = 100;
char * buf = malloc(cursize);
char * cursor = buf;
char * end = buf + cursize;
int n = snprintf(cursor, end-cursor, "Hello! Here is the number %d.", 12);
// maybe do some checking of n here; ensure that it is below 100
cursor += n; // advance the write cursor
n = sprintf(cursor, end-cursor, " And this is data I would like to add.");
// dito
cursor += n;

// Now we have buffered enough data and can write out our stuff.
write(fd, buf, cursor-buf);
free(buf);

这样做可以让您灵活地在需要时立即扩展缓冲区并执行其他操作。

Of course. You can collect whatever you want in a buffer and write() this buffer.

Try this:

...
size_t cursize = 100;
char * buf = malloc(cursize);
char * cursor = buf;
char * end = buf + cursize;
int n = snprintf(cursor, end-cursor, "Hello! Here is the number %d.", 12);
// maybe do some checking of n here; ensure that it is below 100
cursor += n; // advance the write cursor
n = sprintf(cursor, end-cursor, " And this is data I would like to add.");
// dito
cursor += n;

// Now we have buffered enough data and can write out our stuff.
write(fd, buf, cursor-buf);
free(buf);

Doing so gives you the flexibility to extend your buffer as soon as needed, and doing other stuff.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文