How to use index in the file name

匿名 (未验证) 提交于 2019-12-03 02:35:01

问题:

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; } 

回答1:

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"); 


回答2:

Use sprintf to generate the filename.

char buf[80]; sprintf(buf,"file%d", i); fopen(buf,"w"); 

Array syntax in C uses square brackets [].



回答3:

You can just build a string up with sprintf. Make sure your buffer is large enough:

char filename[20]; sprintf( filename, "file%d", i ); 

Then you can open it like this:

FILE *f = fopen( filename, "w"); ... fclose(f); 

No need to use an array (if that's what you were trying to do with f(i)), because you're only keeping one file open at a time.

If you want your files to be text-sortable, you might want file001, file002 etc... You can use %03d instead of %d to 0-pad to 3 digits.



回答4:

You could try this:

#include <stdio.h> #include <stdlib.h>  int main(void) {     int i = 0;    for (i = 0; i < 10; i++) {       char filename[64];       sprintf(filename, "file%d", i);           FILE *fp = fopen(filename, "w");       fprintf(fp, "%d\n", i);       fclose(fp);    }    return 0; } 

Your code is almost ok. Some observations:

  • Use sprintf to create the name of the file. In C there is not a concatenate operator of strings.
  • You don't need to create an array of file pointers.
  • And of course, this may be improved: handling the size of the filename, padding the numbers, etc.


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!