如何在 C 中生成数字文件名的字符串数组
How to generate a string array of numerical filenames in C
我有一个 C 程序,它生成一些我想存储在 CSV 文件中的矩阵,我稍后会制作图像,然后制作 GIF。该程序目前正在运行,但我定义文件名的方式很尴尬。目前我是这样硬编码的:
char filenames[5][10] = {
"0.csv",
"1.csv",
"2.csv",
"3.csv",
"4.csv",
"5.csv"
};
有没有办法以编程方式生成这样的数组,比如在 for 循环中?我有一种直觉,它看起来像这样:
int num_files=10;
char** filenames;
int i;
for(i=0;i<num_files;i++) {
filenames[i] = malloc(something) /*some sort of memory allocation*/
filenames[i] = format("%d.csv",i); /*some sort of string formatting process*/
}
感谢您的帮助。
char (*genArray1(size_t nfiles))[10]
/* can be also: void *genArray(size_t nfiles) */
{
char (*array)[10] = malloc(sizeof(*array)* nfiles);
if(array)
{
for(size_t fileno = 1; fileno <= nfiles; fileno++)
snprintf(array[fileno-1], sizeof(*array), "%zu.csv", fileno);
}
return array;
}
我想到了以下解决方案,在我的程序中,我不得不多次调用生成 csv 文件的函数。我根据需要在循环内定义了文件名。该解决方案基于我收到的关于此 post.
的评论
/*generate csv files*/
int i;
char* filename; /*define the filename string array we will write to*/
int filename_len; /*we will need this when allocating memory*/
for (i=0;i<num_files;i++) {
/*generate filename.
this will be specific to your own code, depending on what you
want to name the files. My filenames are i.pgm (i + 4 characters),
Remember you also need to malloc another for the null terminator
at the end of char array*/
filename_len = snprintf(NULL,0,"%d",i) + 5; /*calculate filename len*/
filename = malloc(filename_len * sizeof filename[0]); /*malloc memory*/
sprintf(filename, "%d.pgm",i); /*sprintf(destination, format string, variables)*/
myfunction(filename, parameters); /*use filename as needed*/
free(filename); /*free memory*/
}
我有一个 C 程序,它生成一些我想存储在 CSV 文件中的矩阵,我稍后会制作图像,然后制作 GIF。该程序目前正在运行,但我定义文件名的方式很尴尬。目前我是这样硬编码的:
char filenames[5][10] = {
"0.csv",
"1.csv",
"2.csv",
"3.csv",
"4.csv",
"5.csv"
};
有没有办法以编程方式生成这样的数组,比如在 for 循环中?我有一种直觉,它看起来像这样:
int num_files=10;
char** filenames;
int i;
for(i=0;i<num_files;i++) {
filenames[i] = malloc(something) /*some sort of memory allocation*/
filenames[i] = format("%d.csv",i); /*some sort of string formatting process*/
}
感谢您的帮助。
char (*genArray1(size_t nfiles))[10]
/* can be also: void *genArray(size_t nfiles) */
{
char (*array)[10] = malloc(sizeof(*array)* nfiles);
if(array)
{
for(size_t fileno = 1; fileno <= nfiles; fileno++)
snprintf(array[fileno-1], sizeof(*array), "%zu.csv", fileno);
}
return array;
}
我想到了以下解决方案,在我的程序中,我不得不多次调用生成 csv 文件的函数。我根据需要在循环内定义了文件名。该解决方案基于我收到的关于此 post.
的评论/*generate csv files*/
int i;
char* filename; /*define the filename string array we will write to*/
int filename_len; /*we will need this when allocating memory*/
for (i=0;i<num_files;i++) {
/*generate filename.
this will be specific to your own code, depending on what you
want to name the files. My filenames are i.pgm (i + 4 characters),
Remember you also need to malloc another for the null terminator
at the end of char array*/
filename_len = snprintf(NULL,0,"%d",i) + 5; /*calculate filename len*/
filename = malloc(filename_len * sizeof filename[0]); /*malloc memory*/
sprintf(filename, "%d.pgm",i); /*sprintf(destination, format string, variables)*/
myfunction(filename, parameters); /*use filename as needed*/
free(filename); /*free memory*/
}