1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Development Fortran, READ()

Discussion in 'Software' started by 731|\|37, 14 Feb 2007.

  1. 731|\|37

    731|\|37 ESD Engineer in Training

    Joined:
    5 Sep 2004
    Posts:
    1,047
    Likes Received:
    0
    I'm have a question about reading a file with Fortran.

    This line:
    Code:
    READ(UNIT = 10, FMT = *, IOSTAT = openFileIoStat) fileBuffer
    
    Will read the file opened under the 10 identifier and store it in filBuffer. The problem is that it will read data from the file up until it encounters a space. Is there any way I can force it to read until it finds a newline?

    -Thanks
     
  2. crazybob

    crazybob Voice of Reason

    Joined:
    21 Oct 2004
    Posts:
    1,123
    Likes Received:
    6
    I've always opened any files I needed before they were used, with:
    Code:
    open(unit=1,file='../dat/input.dat')
    and then read them with
    Code:
    read(1,*) somevariable
    I just tested it and it skips over blank lines with no problem. This is using FORTRAN 90 and the Intel FORTRAN Compiler. I also tried your method and couldn't get it to compile, let alone skip blanks, so it seems likely we're doing something else very different.

    The only downside to my method, which might not matter in yours (not familiar with it), is that my read statement needs to be in a loop, and in order to set the loop index you need to know the number of lines in the input file. Still, hope it helps.
     
    Last edited: 15 Feb 2007
  3. 731|\|37

    731|\|37 ESD Engineer in Training

    Joined:
    5 Sep 2004
    Posts:
    1,047
    Likes Received:
    0
    Thats not my only statement. I have open statements too but they are inside a mess of user interface do-if constructs to handle the UI portion of the file opening.

    I can't remember exactly but when I tried simplifying the read statement (because it hit a compile-time error the first time I wrote it) I had something similar to yours and the compiler gave me crap about something, I don't remember the error message.

    I am running F90, but I'm using the Lahey compiler, which seems to read until it hits the space (which is really helpful if you want to parse test.. NOT). I'll see if I can get a hold of another compiler and see if it will run there.

    -Thanks
     
  4. Chrisy

    Chrisy What's a Dremel?

    Joined:
    27 Jul 2001
    Posts:
    196
    Likes Received:
    0
    Hi there,

    Just to confirm that you want to read in a character variable "xx xx" say. If this is the case then with the auto formatting read which is implied by the fmt=* you will have problems. As long as the string is a known length you can read it in using the read(x,'(a80)') statement where 80 is the length on the string you want to read in.

    This is the way I do things but your situation may be different.

    If you give me a few more details I may be able to help more.

    Chris
     
  5. 731|\|37

    731|\|37 ESD Engineer in Training

    Joined:
    5 Sep 2004
    Posts:
    1,047
    Likes Received:
    0
    Excelent, it works like a charm. I couldn't tell you why because I haven't handled formatting much, but using:

    Code:
    READ(UNIT = 10, FMT = 99, IOSTAT = openFileIoStat) fileBuffer
    99 FORMAT (a80)
    
    Will read till an end-line. Thanks a lot, I realy didn't want to have to dump this project because the language couldn't do what I wanted it to do.

    EDIT: One other question.

    Can I use INDEX() to find the end of line character so when I output the read lines I don't write a solid 80 characters that end in 40 spaces? :duh:
     
    Last edited: 15 Feb 2007
  6. Chrisy

    Chrisy What's a Dremel?

    Joined:
    27 Jul 2001
    Posts:
    196
    Likes Received:
    0
    Well first of all let me explain that formatting.

    What you have done is ask it to read in the next 80 characters as fileBuffer

    If you know what the length of the input will be the you can just shorten it to, for example a20.

    To trim blank space from the string use the command trim(fileBuffer) to do the job.

    Just in case you need this info you can also join character strings together like this.

    fullname=trim(path)//trim(filename)

    You should be able to get Fortran to do most things although its manipulation of character strings is poor.

    Chris
     
  7. 731|\|37

    731|\|37 ESD Engineer in Training

    Joined:
    5 Sep 2004
    Posts:
    1,047
    Likes Received:
    0
    I didn't know about trim. I've been indexing for spaces -1 and then printing string segments (e.g. for displaying the name of the file that the user just specified).

    Now my question is how do I get fortran to write an escape character. will it let me enter it in hex, decimal, or ^[? The documentation is kind of sketchy about how to display special characters, but it isn't short on ways of notating them.

    -Thanks
     
  8. Chrisy

    Chrisy What's a Dremel?

    Joined:
    27 Jul 2001
    Posts:
    196
    Likes Received:
    0
    For this you want to look at the function ACHAR() which "I<128 result is the Ith character of the ASCII collating sequence".

    I have to say I haven't done much with this function but seems to match your needs.

    Chris
     
  9. 731|\|37

    731|\|37 ESD Engineer in Training

    Joined:
    5 Sep 2004
    Posts:
    1,047
    Likes Received:
    0
    Achar works well, but I have to define the achar and then the rest of the ANSI code and then concatenate the two strings. A little irritating that the compiler won't let me define a portion of the origional string with the achar function, but it works well. (Takes the decimal value of the characer you want to display by the way)

    Now one last question I think. Is there a way I can search strings for a set of parameters that I define? I need to find a reoccurring sequence of characters that keeps a basic format, but will change slightly from time to time. Any ideas?

    -Thanks
     
  10. Chrisy

    Chrisy What's a Dremel?

    Joined:
    27 Jul 2001
    Posts:
    196
    Likes Received:
    0
    Hi there again,

    Now you are really pushing both my knowledge and the tricky character handling of Fortran. As far as I am aware there is no function that can search a character string for a sub-string. There are functions which will tell you where the first instance of a particular character is in a string. I'm not sure if this can be extended to multiple characters though.

    The function SCAN(string,set,back) does this. "string" is the string to be searched, set is the thing to be looked for and back is a logical (True: search from the left, False: search from the right).

    You could try having set as the thing you are searching for but the documentation I am looking at implies that it can only be a single character.

    Another option would be to call a C function to do the job for you. I have very limited experience of this calling FFTW (a C based FFT) but other than knowing it can be done I am not going to be much use.

    Most of the stuff I do is highly mathematical and the only dealings with character strings I have is file name manipulation.

    Out of interest what is it you are trying to achieve.

    Chris
     

Share This Page