Interactive file naming for naming multiple files in fortran90

  • Fortran
  • Thread starter dealove
  • Start date
  • #1
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);

}
 
Last edited:

Answers and Replies

  • #2
268
72
I think your problem is how to convert a number to a string in fortran. It can be done by using
internal files:
write(str,fmt=*) i

where i is the number and str the string.
I hope this can help you further.
 
  • #3
3
0
that works, with little modification.

program int2str



character(40) :: numchr
integer j,n

n=2

do j = 1,n

write(numchr,*) j

open (10, file = "foo" // trim(adjustl(numchr)) // ".dat")

enddo

end
 

Related Threads on Interactive file naming for naming multiple files in fortran90

  • Last Post
Replies
5
Views
7K
  • Last Post
Replies
7
Views
40K
  • Last Post
Replies
3
Views
2K
Replies
11
Views
6K
  • Last Post
Replies
4
Views
5K
Replies
8
Views
15K
Replies
6
Views
3K
Replies
3
Views
2K
Replies
5
Views
2K
Top