Interactive file naming for naming multiple files in fortran90

Click For Summary
SUMMARY

The discussion focuses on generating multiple files in Fortran90 with names based on a loop variable. The user seeks a method to create files named sequentially (e.g., name1.txt, name2.txt, ..., name11.txt) using a loop. A solution is provided that utilizes internal file writing to convert integers to strings, allowing dynamic file naming. The example code demonstrates how to implement this functionality effectively in Fortran90.

PREREQUISITES
  • Understanding of Fortran90 syntax and file handling
  • Familiarity with internal file operations in Fortran
  • Basic knowledge of string manipulation in programming
  • Experience with loops and control structures in Fortran
NEXT STEPS
  • Explore Fortran90 file I/O operations in detail
  • Learn about string manipulation techniques in Fortran90
  • Investigate the use of internal files for data conversion in Fortran
  • Review best practices for dynamic file naming in programming
USEFUL FOR

Fortran developers, educators teaching Fortran programming, and anyone looking to automate file creation based on variable values in Fortran90.

dealove
Messages
3
Reaction score
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:
Technology news on Phys.org
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.
 
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
 

Similar threads

Replies
6
Views
2K
  • · Replies 32 ·
2
Replies
32
Views
4K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 5 ·
Replies
5
Views
5K
Replies
4
Views
5K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 89 ·
3
Replies
89
Views
6K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 75 ·
3
Replies
75
Views
7K
  • · Replies 12 ·
Replies
12
Views
2K