如何用 C 语言读取多个文件?

发布于 2024-09-30 23:40:31 字数 124 浏览 1 评论 0原文

我正在开发一个必须从 txt 文件读取的程序。 我知道有一个名为 fopen("myfile.txt","rt") 的函数,但是如果我有 10 个文件怎么办?我需要调用该函数 10 次(每个文件调用一次)吗?

I am working on a program which have to read from txt files.
I know that there's a function called fopen("myfile.txt","rt"), but what if I have 10 files? Do i need to call the function 10 times (a call for every file)?

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

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

发布评论

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

评论(2

一瞬间的火花 2024-10-07 23:40:31

是的。但是,如果您对每个函数执行相同的例程,请将该行为抽象为接受文件名的函数。现在调用该函数 10 次,每个文件名调用一次。

void read_from_text_file(char const *filepath);

read_from_text_file("myfile.txt");
read_from_text_file("myfile2.txt");
...

这是计算机科学的一个核心概念。流行语包括“抽象”、“常规”、“可重用性”等。

Yes. But if you perform the same routines on each of those functions, abstract that behaviour into a function that accepts the name of a file. Now call that function 10 times, once with each file name.

void read_from_text_file(char const *filepath);

read_from_text_file("myfile.txt");
read_from_text_file("myfile2.txt");
...

This is a core concept in computer science. Buzzwords include "abstraction", "routine", "reusability", etc.

零度° 2024-10-07 23:40:31

是的。每个文件需要一次函数调用。

Yes. One function call per file is needed.

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