silveira13
04-25-2008, 09:50 AM
Hi,
I'd like to search for a CHARACTER variable, 'b' for example, in a input file and the
algorithm below doesn't get it! Someone can help me?
Thanks for answer,
Filipe Silveira
program test
implicit integer (i-n)
CHARACTER :: a,b
open (unit=1,file="testing.dat",status="old")
read(1,*)a
do i=1,10
if (a.EQ.b) then
write(*,*)'a.EQ.b'
else
write(*,*)'a.NE.b'
endif
enddo
close (1)
end program
davekw7x
04-26-2008, 01:26 AM
the algorithm below doesn't get it...
Well, I gotta tell you: I don't get it either. What, exactly, did you expect from this?
Here's the action that I see in your codelet:
1. Try to open a file
2. Try to read a single character at the beginning of the file into variable a
3. Perform the following steps ten times:
Compare the value of the variable a with the value of b, but b was never given a value, so it is comparing the value of the first character of the file with nothing in particular.
Depending on the results of the comparison, write one message or another.
Now, the results of the comparison are the same every time through the loop, so the exact same thing is printed all ten times, regardless of what is in the file.
Or am I missing something?
Here's the thing: There are a number of ways of finding whether a particular character is in a file, but before writing a program, maybe it's best to define what the heck you are going to if and when you find it. I'll get to that in a minute.
It should be pretty obvious that if you are going to read past the first character, the read statement should be in a loop of some kind. Furthermore, a read statement like the one you used will read as much as it can from a line (exactly one character in your case, since that is what was on the list) and then advance to the next line. Bottom line: that read statement in a loop might be used to read the first character on each line, but I'm guessing that's not what you had in mind.
Let's create an example program that actually does something related to file contents and searching for a particular character. Since I don't know what you really need to do, I'll make up my own program specification.
So: Suppose we know that we have a text file (a file organized as a bunch of lines), and each line is no more than 80 characters long. Suppose that we don't know how many lines (or how many total characters) there are in the file. Suppose that we want to find all occurrences of the character 'b'. We are required to print out the line number and the place on the line where they appear.
You could do something like
PROGRAM test_lines
IMPLICIT NONE
INTEGER i, line_number
CHARACTER line*80
CHARACTER search_char
search_char = 'b'
line_number = 0
open (unit=1,file="testing.dat",status="old")
do
read(1,'(A)', END=1000) line
line_number = line_number + 1
do i=1,80
if (line(i:i) .EQ. search_char) then
write(*, &
'("Line ",I2,", character number ",I2," is a match.")')&
line_number, i
end if
end do
end do
1000 close(1)
END PROGRAM test_lines
Here's what I did:
1. I compiled the above program. (I used GNU gfortran, but as far as I know, any Fortran 90 compiler will work.)
2. I copied the above source file to a file named "testing.dat"
3. I executed the program.
Here's the output I got:
Line 4, character number 26 is a match.
Line 8, character number 22 is a match.
Line 9, character number 15 is a match.
Line 15, character number 18 is a match.
Line 15, character number 32 is a match.
Line 19, character number 46 is a match.
Line 20, character number 25 is a match.
Now, if you really wanted to do something else (or if you had a different kind of file in mind), you should tell us exactly what the assignment is.
Regards,
Dave
Footnote: Is there any connection or possible future connection to Numerical Recipes? I mean I showed generic Fortran data types and code rather than the explicit types defined in nrtype.f90. Did you want more specific information?
I respectfully suggest the following;
1. Get a book or other Fortran reference material (class notes, on-line tutorial or whatever).
2. Read up on Fortran I/O with lines of text and character variables. Maybe a character array would be more to your liking than the construct that I showed. (Either can work.)