I have a doubt about Fortran code. Is "A" a keyword? I found this character used in write and format commands but I can't find some specific documentation about it. Here are some examples:
CHARACTER *10 name
write(*,1) name
1 format (" Your name is ",A)
or
end = LNBLNK(string)
write(4,'(A)') string(1:end)
No, A is an data edit descriptor for character string output. For more read some textbook about Fortran I/O or a tutorial like https://www.cs.mtu.edu/~shene/COURSES/cs201/NOTES/format.html
And it the second code sample, A is not even in a position where a keyword would be used. '(A)' is a normal string, like 'Hello world' or " Your name is ", for example. But here the content of the string and where the string is used is what matters. It contains a descriptor and it is used as a format string in the write statement.
In the FORMAT statement it is not a normal string, but a special syntax, but it works the same.
Related
file content:
/function name: input\n\t\tworking: inputs details\n\t\t/
/*fuction name: datawrite\n\t\tworking: write the data into the file
*/
/function name: dataread\n\tworking: read the data in the file for
display/
/*funtion name: accsummary\n\t\tworking: display the content of the
file of specific person */
I want to extract only the "function names" and the "workings" from the file and store them into the array of string. "using c++"
that is, if i have declared an array of string function[10] then this should store "input,datawrite,dataread,accsummary" similarly from "working"
This sounds like a task for regular expression. In C++ there is support for regex especially regex_match.
I guess, this should get you started. But be warned, what you are trying to accomplish will not be solved by simple regex.
Your matching string might look something like this
/\/function name: ([^\\]*).*/
This will look for string "function name: " followed by any character other than \ . and then any character up to the end of the line. The second part will be remembered and can be accessed by regex_match.
Try it in online regex tester and modify it based on your specific needs. Just note that it takes regex without leading and ending /.
Oh, I noticed that you asked also for extracting workings, while my example extracts only function names. But you will get there when you get the concept.
You need to take a look at the std::stringstream class:
http://www.cplusplus.com/reference/sstream/stringstream/
Then you need to look at the substr method in std::string:
http://www.cplusplus.com/reference/string/string/substr/
I have a Fortran program in which I use a format staement for writing like
WRITE (12,'(A72,1X,A,1X,I6.6)') ALINE,SFFIX(1:NUM),LINENO
which works fine. However when I write a character string `fmtt' like
WRITE (fmtt,'(a)') trim(adjustl(sttr(2)))
where string 'sttr (2)is '(A72,1X,A,1X,I6.6) ' ` , which I confirm by printing fmtt like
WRITE(*,'(a)')fmtt
When I use string fmtt as format in a write statement like
WRITE (12, fmtt) ALINE,SFFIX(1:NUM),LINENO
I get error message
forrtl : info(58) format syntax error at or near '(A72,1X,A,1X,I6.6) '
Though I am not an expert I expected it to work as format is supposed to be a character string. Where am I wrong? I wanted to do this to make format dependent on user input. Thanking you.
Probably the single quotation marks ' ' are making problem. Following code is working fine.
implicit none
character(len=30) :: fmtt
fmtt = '(A72,1X,A,1X,I6.6)'
write(*,fmtt)"first_character","second_character",230
stop
end
Note that print*,fmtt give the output without single quotes. I follow the method given in http://www.cs.mtu.edu/~shene/COURSES/cs201/NOTES/chap05/format.html . Hopefully this will help you.
Suppose I have the following code
program fortran
open(900, FILE='SOMETHING')
write(900, *) '21'
end program fortran
The file form will be
21
that is, there is a space before the number. How to get rid of that space?
You can write it as a string:
PROGRAM fortran
OPEN(900,FILE='SOMETHING')
WRITE(900,'(a)') '21'
END PROGRAM FORTRAN
> cat SOMETHING
21
To respond to the comment:
The more explicit way of doing that would be to write the number into a string (you could also use list-directed I/O for this step), remove whitespaces from the string trim and finally output the left-adjusted adjustl:
program test
character(len=23) :: str
write(str,'(ES23.15 E3)') 1.23d0
write(*,'(a)') adjustl(trim(str))
write(str,'(ES14.7 E2)') 0.12e0
write(*,'(a)') adjustl(trim(str))
end program
> ./a.out
1.230000000000000E+000
1.2000000E-01
This solution is probably more complicated then necessary, but it is a very flexible approach that can be extended easily for arbitrary purposes and formats.
In list-directed format (the * in write(unit,*)) the compiler typically inserts a leading space. The first column used to be used to control line printers but that is now deleted from Fortran.
You can use any explicit format you want to get rid of the leading space. For example the general g0 one or the string specific a or the integer-specific i.
This program will write 'hello' at the specified position '9' of the file 'test.txt'
program test
open(31,file='test.txt',access='stream')
write(31,pos=9)'hello'
close(31)
end program
Can this position specifier be used to write on terminal?
I want to be able to do something like write(*,pos=9)'hello'
The standard output is pre-opened as a sequential access formatted file, therefore you cannot use pos.
Why you want this? You can just use an explicit format
write(*,'(t9,a)') 'hello'
Or you can print some spaces before hello. You can even concatenate the spaces and the string.
I am writing some simple output in fortran, but I want whitespace delimiters. If use the following statement, however:
format(A20,ES18.8,A12,ES18.8)
I get output like this:
p001t0000 3.49141273E+01obsgp_oden 1.00000000E+00
I would prefer this:
p001t0000 3.49141273E+01 obsgp_oden 1.00000000E+00
I tried using negative values for width (like in Python) but no dice. So, is there a way to left-justify the numbers?
Many thanks in advance!
There's not a particularly beautiful way. However, using an internal WRITE statement to convert the number to a text string (formerly done with an ENCODE statement), and then manipulating the text may do what you need.
Quoting http://rsusu1.rnd.runnet.ru/develop/fortran/prof77/node168.html
An internal file WRITE is typically
used to convert a numerical value to a
character string by using a suitable
format specification, for example:
CHARACTER*8 CVAL
RVALUE = 98.6
WRITE(CVAL, '(SP, F7.2)') RVALUE
The WRITE statement will fill the
character variable CVAL with the
characters ' +98.60 ' (note that there
is one blank at each end of the
number, the first because the number
is right-justified in the field of 7
characters, the second because the
record is padded out to the declared
length of 8 characters).
Once a number has been turned into a
character-string it can be processed
further in the various ways described
in section 7. This makes it possible,
for example, to write numbers
left-justified in a field, ...
This is easier with Fortran 95, but still not trivial. Write the number or other item to a string with a write statement (as in the first answer). Then use the Fortran 95 intrinsic "ADJUSTL" to left adjust the non-blank characters of the string.
And really un-elegant is my method (I program like a cave woman), after writing the simple Fortran write format (which is not LJ), I use a combination of Excel (csv) and ultraedit to remove the spaces effectively getting the desired LJ followed directly by commas (which I need for my specific import format to another software). BF
If what you really want is whitespace between output fields rather than left-justified numbers to leave whitespace you could simply use the X edit descriptor. For example
format(A20,4X,ES18.8,4X,A12,4X,ES18.8)
will insert 4 spaces between each field and the next. Note that the standard requires 1X for one space, some of the current compilers accept the non-standard X too.
!for left-justified float with 1 decimal.. the number to the right of the decimal is how many decimals are required. Write rounds to the desired decimal space automatically, rather than truncating.
write(*, ['(f0.1)']) RValue !or
write(*, '(f0.1)') RValue
!for left-justified integers..
write(*, ['(i0)']) intValue !or
write(*, '(i0)') RValue
*after feedback from Vladimir, retesting proved the command works with or without the array brackets