LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 04-30-2013, 08:21 PM   #1
vjramana
Member
 
Registered: Sep 2009
Posts: 89

Rep: Reputation: 0
open files with ascending numbers


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

I get the files with name as below:

Code:
test-set10.traj
test-set 1.traj
test-set 2.traj
test-set 3.traj
test-set 4.traj
test-set 5.traj
test-set 6.traj
test-set 7.traj
test-set 8.traj
test-set 9.traj
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:


Code:
test-set1.traj
test-set2.traj
test-set3.traj
test-set4.traj
test-set5.traj
test-set6.traj
test-set7.traj
test-set8.traj
test-set9.traj
test-set10.traj

Can anyone help me to fix this problem?

Many thanks in advance.

Regards
 
Old 04-30-2013, 09:40 PM   #2
flos
Member
 
Registered: Feb 2009
Location: Shropshire, UK
Distribution: Mint
Posts: 30

Rep: Reputation: 2
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
 
1 members found this post helpful.
Old 05-01-2013, 12:41 AM   #3
psionl0
Member
 
Registered: Jan 2011
Distribution: slackware_64 14.1
Posts: 722
Blog Entries: 2

Rep: Reputation: 124Reputation: 124
It's been a while since I used fortran but you could try the following code:
Code:
write(str1,I3) (j-30)
adjustl(str1)
str2 = '000'//str1
n = len_trim(str2)
filename = "test-set"//str2(n-2,n)//".traj"
I'm guessing it should generate the following filenames:
Code:
test-set001.traj
test-set002.traj
test-set003.traj
test-set004.traj
test-set005.traj
test-set006.traj
test-set007.traj
test-set008.traj
test-set009.traj
test-set010.traj

Last edited by psionl0; 05-01-2013 at 12:46 AM.
 
1 members found this post helpful.
  


Reply

Tags
fortran



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 ls files that start with numbers Drigo Linux - Newbie 9 11-14-2016 11:35 AM
[SOLVED] Sort numbers in ascending order using echo, and as an alias robfraser Linux - Newbie 2 04-18-2012 03:44 PM
[SOLVED] How to rename some files to new filenames in ascending format. e3399 Linux - Newbie 2 12-08-2010 07:07 PM
LXer: Open core by the numbers LXer Syndicated Linux News 0 11-09-2010 02:10 PM
Finding multiple files with ascending numerical files extensions unim21 Linux - Newbie 3 04-23-2009 07:50 PM

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

All times are GMT -5. The time now is 07:02 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