简体   繁体   English

fopen 用于读取/写入文件夹中的多个文件 C

[英]fopen for reading/writing multiple files in a folder C

I have a question about how to use fopen() to read/write multiple files of one folder.我有一个关于如何使用fopen()读取/写入一个文件夹的多个文件的问题。 I have 100 text files in a folder and I want to use a loop to open all the files (one by one) and write something in them.我在一个文件夹中有 100 个文本文件,我想使用循环打开所有文件(一个一个)并在其中写入一些内容。 For example something like this:例如这样的:

for(i = 0; i < 100; i++)
{
  f = fopen("files","a");
  fwrite("hello");
  fclose(f);
}

So for example if i have 100 txt files in the folder "C:\Users\Desktop\examples\txts" I want to open all of them and write for example the same word "hello" to all of them.因此,例如,如果我在文件夹“C:\Users\Desktop\examples\txts”中有 100 个 txt 文件,我想打开所有这些文件,并为所有这些文件写上相同的单词“hello”。

If I use it like: "C:\Users\Desktop\examples\txts*.txt" (with the star: *) it doesn't work.如果我像这样使用它:“C:\Users\Desktop\examples\txts*.txt”(带有星号:*)它不起作用。 Any idas?有伊达吗?

You could use readdir, as in this example:您可以使用 readdir,如下例所示:

http://www.gnu.org/s/hello/manual/libc/Simple-Directory-Lister.html http://www.gnu.org/s/hello/manual/libc/Simple-Directory-Lister.html

You can use the FindFirstFile/FindNextFile api functions on windows, not sure if there's a built in function to do this in C (it's been 20 years (almost) since I wrote real world code in C) You can use the FindFirstFile/FindNextFile api functions on windows, not sure if there's a built in function to do this in C (it's been 20 years (almost) since I wrote real world code in C)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM