ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I am trying to generate files in fortran90 with ascending number in the filename. The fortran code I used is as blow:
Code:
program openfile
implicit none
integer::i, j
character (len=90) :: filename
fileLoop: do j = 31, 40
write (filename, '( "test-set",I2,".traj" )' ) (j-30)
OPEN(unit=j,status='unknown',file=filename)
close (j)
end do fileLoop
end
The problem I am finding here is the single digit number after the "set" word. The number is not like "set1", "set2" and I want to get until "set60". Instead there is an empty space if I use I2 in the code above. I want the file names to be:
Dear vjramana,
even if you achieve what you are aiming for, when viewed in a file explorer (eg Nautilus or Dolphin) or even under a command-line ls, they will not appear in the numerical order you expect: you will always get xx1, xx10, xx12, xx2, etc. If you expect a maximum of, say, a hundred files, the numerical string you want to append to xx should be padded with a single leading zero, so that you will get xx01, xx02, xx03 ... xx09, xx10, xx11 and so on.
If the expected number of files is up to a thousand, you would need to pad with two zeros, to get xx001, xx002, etc.
I don't recognise the language you are programming in, but usually there are provisions for doing this left-side padding with zeros for the counter (i or j) that you are using to iterate through the sequence of files.
The counter is an integer; to create a file name (which is a string), you have to convert the integer to a string, and usually the programming language allows you to format integers as strings of any chosen length (I presume that is what the I2 in your example is trying to do), but you may have to specify what the padding character is going to be. It seems from your results that your programming language has used a default of a space as padding character. Personally, I find that file names with spaces in them cause so many problems that I *never* use spaces in file names. You wouldn't find a single example in my entire workspace.
In the hope that this helps, may I wish you the best of luck?
Regards, Flos
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.