1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Interactive file naming for naming multiple files in fortran90

  1. Jun 20, 2008 #1
    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")

    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:


    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;


    int2str(i, temp);
    fptr = fopen(filename,"w");



    void int2str(int test, char temp[10])

    int k,i=0;
    while(test > 0)
    k = test % 10;
    test = test/10;
    temp = (char)(k + 48);

    temp = '\0';

    Last edited: Jun 20, 2008
  2. jcsd
  3. Jun 20, 2008 #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.
  4. Jun 20, 2008 #3
    that works, with little modification.

    program int2str

    character(40) :: numchr
    integer j,n


    do j = 1,n

    write(numchr,*) j

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


Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?

Similar Discussions: Interactive file naming for naming multiple files in fortran90