LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-29-2011, 11:09 AM   #1
parekhharsh_j
LQ Newbie
 
Registered: Jun 2011
Posts: 20

Rep: Reputation: Disabled
Fortran How to open a file in loop?


I have a 50 file name NSSAVE0001.vtk to NSSAVE0050.vtk.
Do I have to manually type individually command to open each file or can I have a loop to open file?
 
Old 06-29-2011, 11:46 AM   #2
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: Fedora
Posts: 4,147

Rep: Reputation: 1264Reputation: 1264Reputation: 1264Reputation: 1264Reputation: 1264Reputation: 1264Reputation: 1264Reputation: 1264Reputation: 1264
Quote:
Originally Posted by parekhharsh_j View Post
I have a 50 file name NSSAVE0001.vtk to NSSAVE0050.vtk.
Do I have to manually type individually command to open each file or can I have a loop to open file?
Pretty sure the file name can be in an array, so no reason not to use the loop.
 
Old 06-29-2011, 11:51 AM   #3
parekhharsh_j
LQ Newbie
 
Registered: Jun 2011
Posts: 20

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by smallpond View Post
Pretty sure the file name can be in an array, so no reason not to use the loop.
If it is possible then how?
How to write file name?

Code:
do n=1,50
nn=n+10
open(unit=nn,file=??)
enddo
 
Old 06-29-2011, 03:37 PM   #4
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 735

Rep: Reputation: 76
Hi.

Here is a driver for a sample Fortran code to create filenames as strings:
Code:
#!/usr/bin/env bash

# @(#) s1	Demonstrate creating of file names as character strings.

# Utility functions: print-as-echo, print-line-with-visual-space, debug.
# export PATH="/usr/local/bin:/usr/bin:/bin"
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C gfortran

FILE=${1-one.f90}
rm -f a.out nssave*

pl " Fortran source:"
cat $FILE

pl " Compile, link, execute:"
gfortran $FILE
./a.out

pl " Resultant files opened (created):"
ls -lgG nssave*
producing:
Code:
% ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian GNU/Linux 5.0.8 (lenny) 
GNU bash 3.2.39
gfortran GNU Fortran (Debian 4.3.2-1.1) 4.3.2

-----
 Fortran source:
     program f1

       character*20 :: my_unit

       do n=1,5
         write(my_unit,"(a,i4.4,a)") "nssave",n,".vtk"
         write(*,*) " Preparing to open file: ",my_unit
         open(unit=1,file=my_unit,err=100)
       end do
       stop "f1"

100    continue
       write(*,*) " Error in opening file: ", my_unit

     end

-----
 Compile, link, execute:
  Preparing to open file: nssave0001.vtk      
  Preparing to open file: nssave0002.vtk      
  Preparing to open file: nssave0003.vtk      
  Preparing to open file: nssave0004.vtk      
  Preparing to open file: nssave0005.vtk      
STOP f1

-----
 Resultant files opened (created):
-rw-r--r-- 1 0 Jun 29 15:35 nssave0001.vtk
-rw-r--r-- 1 0 Jun 29 15:35 nssave0002.vtk
-rw-r--r-- 1 0 Jun 29 15:35 nssave0003.vtk
-rw-r--r-- 1 0 Jun 29 15:35 nssave0004.vtk
-rw-r--r-- 1 0 Jun 29 15:35 nssave0005.vtk
Good luck ... cheers, makyo
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to loop over text file lines within bash script for loop? johnpaulodonnell Linux - Newbie 9 07-28-2015 03:49 PM
how to open *.vtk file in fortran parekhharsh_j Linux - Newbie 3 06-22-2011 12:01 PM
generating files with different names at each loop in fortran vjramana Programming 1 11-09-2010 12:14 PM
Save the results of a fortran loop in a text file simpleman0982 Programming 2 11-02-2010 11:40 PM
Geting error loop QFike:getch: File not open QFile:atEnd: this file is not open badgerbox76 Linux - Newbie 6 01-07-2006 05:30 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:12 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration