Fortran Interactive file naming for naming multiple files in fortran90

AI Thread Summary
The discussion centers on creating multiple files in Fortran, named according to a variable in a loop, similar to existing C code. The original poster seeks guidance on how to implement this in Fortran, specifically how to convert an integer to a string for file naming. A suggested solution involves using internal file writing to convert integers to strings. The provided Fortran code example demonstrates how to achieve this by writing the integer to a character variable and then using it to open files with names formatted as "foo1.dat", "foo2.dat", etc. The approach emphasizes the use of the `write` statement to handle string conversion and file creation efficiently.
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
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top