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
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...
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...
Back
Top