如何使用索引中的文件名 [英] How to use index in the file name

查看:179
本文介绍了如何使用索引中的文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是微不足道的问题。我不是一个专业的程序员,我宁愿是谁做C.使用一些数值实验,我想输出数学家我
将被写入用于参数的不同值的不同的文件的实验。 MWE应该做这样的事情。箱子被我索引的文件指针。打开一个文件,文件名为[1]。写
我到该文件,然后将其关闭。下面显然,code不能编译。就是这样一个结构甚至可能吗?

 #包括LT&;&stdio.h中GT;
INT I;INT
主要()
{
        对于(I = 0; I&小于10;我++){
                FILE * F(ⅰ);
                F(ⅰ)=的fopen(文件[I],在w);
                fprintf中(F(I),%d个\\ N,I);
                FCLOSE(F(I));
        }
    返回0;
}

编辑:我有几个像样的答案,但能有人帮助解决sprintf的问题。即在OpenBSD,我用sprintf不推荐使用。所以,我得到这个消息

  $ GCC test.c的
/tmp//ccN31aTv.o(.text+0x41):在函数'主':
:警告:的sprintf()常常被误用,请使用的snprintf()

当我与snprintf的sprintf的替代我得到的各种警告

  $ GCC test.c的
test.c的:在函数'主':
test.c的:9:警告:传递的snprintf的论点2使整数指针,未作投
test.c的:9:警告:传递的snprintf的说法3使指针从整数没有投

这看起来并不像一个伟大的品质code给我。

最终解决方案:我只是想记录最终的解决方案。 ProPolice和systrace很高兴与在OpenBSD此code。感谢大家谁帮助!

 #包括LT&;&stdio.h中GT;
INT I;焦炭BUF [20];INT
主要()
{
        对于(I = 0; I&小于10;我++){
                的snprintf(BUF,sizeof的(BUF),文件名%D,我);
                FILE * F = FOPEN(BUF,W);
                fprintf中(F,%d个\\ N,I);
                FCLOSE(F);
        }
返回0;
}


解决方案

在C,使用的snprintf:

 字符BUF [PATH_MAX];
的snprintf(BUF,sizeof的(BUF),文件%D,我);

如果你使用Linux操作系统,有一个有用的GNU扩展:

 字符*名称;
asprintf(安培;名称为文件%D,我);

您需要记住使用后释放(名称)。

请注意,你的语法 FILE * F(I); 是无效的,虽然

如果您需要声明10个元素的FILE *数组做的:

  FILE *阵列[10];

然后用它这样的:

 数组[我] = FOPEN(文件名,W);

This is probably trivial question. I am not a professional programmer, I am rather a mathematician who is doing some numerical experiment using C. I would like the output of my experiment to be written in different files for different values of a parameter. MWE should do something like this. Crate a file pointer indexed by i. Open a file named file[i]. Write i into that file and then close it. The code below obviously doesn't compile. Is such a construction even possible?

#include<stdio.h>
int i;

int 
main()
{
        for (i = 0; i < 10; i++){
                FILE *f(i);
                f(i)=fopen("file"[i],"w");
                fprintf(f(i),"%d \n", i);
                fclose(f(i));  
        }
    return 0;
}

Edit: I got several decent answers but can somebody help to fix the sprintf problem. Namely on OpenBSD which I use sprintf is not recommended. So I get this message

$ gcc test.c
/tmp//ccN31aTv.o(.text+0x41): In function `main':
: warning: sprintf() is often misused, please use snprintf()

When I replace sprintf with snprintf I get all sorts of warnings

$ gcc test.c
test.c: In function 'main':
test.c:9: warning: passing argument 2 of 'snprintf' makes integer from pointer without a cast
test.c:9: warning: passing argument 3 of 'snprintf' makes pointer from integer without a cast

That doesn't look like a great quality code to me.

Final Solution: I just want to document final solution. ProPolice and systrace are happy with this code on OpenBSD. Thanks to everyone who helped!

#include<stdio.h>
int i;

char buf[20];

int
main()
{
        for (i = 0; i < 10; i++){
                snprintf(buf, sizeof(buf), "filename%d", i);
                FILE *f = fopen( buf, "w");
                fprintf(f,"%d \n", i);
                fclose(f);
        }
return 0;
}

解决方案

In C, use snprintf:

char buf[PATH_MAX];
snprintf(buf, sizeof(buf), "file%d", i);

If you use linux, there is a useful GNU extension:

char *name;
asprintf(&name. "file%d", i);

You need to remember to free(name) after use.

Note that your syntax FILE *f(i); is not valid though.

If you need to declare an array of FILE * of 10 elements do:

FILE *array[10];

then use it like that:

array[i] = fopen(filename, "W");

这篇关于如何使用索引中的文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆