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")
    .........
    .........
    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: 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

    n=2

    do j = 1,n

    write(numchr,*) j

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

    enddo

    end
     
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
Loading...