Interactive file naming for naming multiple files in fortran90

In summary, the person is looking for a way to create files with dynamic names based on the value of a variable in a loop. They provide an example in C but are looking for a solution in Fortran. Another person suggests using internal files to convert a number to a string in Fortran, which the person confirms works with some modifications.
  • #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);

}
 
Last edited:
Technology news on Phys.org
  • #2
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
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
 

What is interactive file naming?

Interactive file naming is a method used in Fortran90 programming to name multiple files through user input. It allows the user to specify a base name for the files and then add a unique identifier, such as a number or letter, to each file name.

Why is interactive file naming important in Fortran90?

In Fortran90, each file must have a unique name in order to be accessed and manipulated by the program. Interactive file naming allows the user to easily generate multiple unique file names, making it an essential tool for managing large amounts of data.

How does interactive file naming work in Fortran90?

In Fortran90, interactive file naming is achieved through the use of a DO loop. The user specifies the base file name and the number of files to be created, and then uses the DO loop to add a unique identifier to each file name. This process can also be automated through the use of variables and arrays.

What are the benefits of using interactive file naming in Fortran90?

Interactive file naming in Fortran90 allows for efficient and organized management of data. It also reduces the likelihood of naming errors and makes it easier to access specific files within a larger set of data.

Are there any limitations to using interactive file naming in Fortran90?

One limitation of interactive file naming in Fortran90 is that it requires the user to have a solid understanding of the DO loop and how to use arrays and variables. Additionally, the number of files that can be generated using this method is limited by the size of the arrays used, so it may not be suitable for very large data sets.

Similar threads

  • Programming and Computer Science
Replies
6
Views
896
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
32
Views
2K
  • Programming and Computer Science
Replies
4
Views
3K
  • Programming and Computer Science
3
Replies
89
Views
4K
  • Programming and Computer Science
3
Replies
75
Views
4K
  • Programming and Computer Science
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
846
  • Programming and Computer Science
2
Replies
55
Views
4K
  • Programming and Computer Science
Replies
2
Views
3K
Back
Top