C语言 如何在循环写入时动态更改文件名?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4232842/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 07:07:13  来源:igfitidea点击:

How to dynamically change filename while writing in a loop?

c

提问by Richard Knop

I would like to do something like this: In a loop, first iteration write some content into a file named file0.txt, second iteration file1.txt and so on, just increase the number.

我想做这样的事情:在一个循环中,第一次迭代将一些内容写入名为file0.txt的文件,第二次迭代file1.txt等,只需增加数量。

FILE *img;
int k = 0;
while (true)
{
            // here we get some data into variable data

    file = fopen("file.txt", "wb");
    fwrite (data, 1, strlen(data) , file);
    fclose(file );

    k++;

            // here we check some condition so we can return from the loop
}

回答by Jookia

int k = 0;
while (true)
{
    char buffer[32]; // The filename buffer.
    // Put "file" then k then ".txt" in to filename.
    snprintf(buffer, sizeof(char) * 32, "file%i.txt", k);

    // here we get some data into variable data

    file = fopen(buffer, "wb");
    fwrite (data, 1, strlen(data) , file);
    fclose(file );

    k++;

    // here we check some condition so we can return from the loop
}

回答by PeteUK

A different way to do it in C++:

在 C++ 中执行此操作的另一种方法:

#include <iostream>
#include <fstream>
#include <sstream>

int main()
{
    std::string someData = "this is some data that'll get written to each file";
    int k = 0;
    while(true)
    {
        // Formulate the filename
        std::ostringstream fn;
        fn << "file" << k << ".txt";

        // Open and write to the file
        std::ofstream out(fn.str().c_str(),std::ios_base::binary);
        out.write(&someData[0],someData.size());

        ++k;
    }
}

回答by Nils Pipenbrinck

FILE *img;
int k = 0;
while (true)
{
    // here we get some data into variable data
    char filename[64];
    sprintf (filename, "file%d.txt", k);

    file = fopen(filename, "wb");
    fwrite (data, 1, strlen(data) , file);
    fclose(file );
    k++;

            // here we check some condition so we can return from the loop
}

回答by AndersK

so create the filename using sprintf:

所以使用 sprintf 创建文件名:

char filename[16]; 
sprintf( filename, "file%d.txt", k );  
file = fopen( filename, "wb" ); ...

(although that is a C solution so the tag is not correct)

(虽然这是一个 C 解决方案,所以标签不正确)

回答by Josh Wieder

I accomplished this in the manner below. Note that unlike a few of the other examples, this will actually compile and function as intended by without any modification beside preprocessor includes. The solution below iterates fifty filenames.

我以下面的方式完成了这项工作。请注意,与其他一些示例不同,这实际上将按预期编译和运行,除了预处理器包含外,无需任何修改。下面的解决方案迭代五十个文件名。

int main(void)
{
    for (int k = 0; k < 50; k++)
    {
        char title[8];
        sprintf(title, "%d.txt", k);
        FILE* img = fopen(title, "a");
        char* data = "Write this down";
        fwrite (data, 1, strlen(data) , img);
        fclose(img);
    }
}