- #1
dealove
- 3
- 0
I am looking for a way to create the files with names name according to value of variable in a loop.
For example (not actual code):
do i=1,11
open(10,file=name 'i'//".txt")
...
...
enddo
where 11 files with names: name1.txt, name2.txt...name11.txt will be created with the content.
Do you have any suggestion/code? I wrote it in C but, could not find some functions needed in fortran. Here is my C code:
#include
#include
#include
void int2str(int test, char *temp);
void main()
{
int i;
char str[15] = "temp\\name";
char filename[20], temp[10];
FILE *fptr;
int test = 355;
//clrscr();
for(i=1;i<11;i++)
{
strcpy(filename,str);
int2str(i, temp);
strcat(filename,temp);
strcat(filename,".txt");
fptr = fopen(filename,"w");
fprintf(fptr,"%s",filename);
fclose(fptr);
}
}void int2str(int test, char temp[10])
{
int k,i=0;
while(test > 0)
{
k = test % 10;
test = test/10;
temp = (char)(k + 48);
i++;
}
temp = '\0';
strrev(temp);
}
For example (not actual code):
do i=1,11
open(10,file=name 'i'//".txt")
...
...
enddo
where 11 files with names: name1.txt, name2.txt...name11.txt will be created with the content.
Do you have any suggestion/code? I wrote it in C but, could not find some functions needed in fortran. Here is my C code:
#include
#include
#include
void int2str(int test, char *temp);
void main()
{
int i;
char str[15] = "temp\\name";
char filename[20], temp[10];
FILE *fptr;
int test = 355;
//clrscr();
for(i=1;i<11;i++)
{
strcpy(filename,str);
int2str(i, temp);
strcat(filename,temp);
strcat(filename,".txt");
fptr = fopen(filename,"w");
fprintf(fptr,"%s",filename);
fclose(fptr);
}
}void int2str(int test, char temp[10])
{
int k,i=0;
while(test > 0)
{
k = test % 10;
test = test/10;
temp = (char)(k + 48);
i++;
}
temp = '\0';
strrev(temp);
}
Last edited: