From: Jim Xia on
>
> 1. Read the whole line into a character buffer and parse the buffer
> (possibly with the help of internal reads).


This is the most common technique. When I program in C/C++, this is
also the approach I choose. Although munipulating the string can
sometimes tricky.



>
> 2. Use non-advancing I/O (or a nonstandard variant such as the $ or \
> edit descriptors).

This only works if you know the leading character string format, which
luckily is true in this case. But in general, if the first field is
for a string of variable length, which I've seen before, then you'll
find non-advancing IO helpless.


>
> 3. Use backspace
>


I'm not very encouraged to use backspaces.



> 4. (sort of - I almost forgot about this one until others mentioned
> simillar things) Assume that any error must be the one you might be
> expecting. That's pretty full of problems, though. I disrecommend it.
>


I'd be cautious on this too. Because if the file IO is in an error
state, then the file position can be indeterminant as explicitly
spelled out in the standard. You must either do a backspace or a
rewind to reset the file position.



> Note that you won't ever get an "end-of-line" (more properly
> end-of-record) at all from any of the codes shown. That condition
> applies *ONLY* to non-advancing I/O. If you find a compiler that returns
> an end-of-line condition for advancing I/O, file a bug report.
>
> Well, that's the case through f2003 at least. I haven't checked the
> f2008 FDIS on the matter. I hope it is still the case


I don't recall any changes in that area. In general, my impression on
Fortran IO is that its format is too rich to be understood by an
average user, while it's lacking some basic functionalities (e.g.
recursive IO).


Cheers,

Jim
From: glen herrmannsfeldt on
Jim Xia <jimxia(a)hotmail.com> wrote:

>> 1. Read the whole line into a character buffer and parse the buffer
>> (possibly with the help of internal reads).

> This is the most common technique. When I program in C/C++, this is
> also the approach I choose. Although munipulating the string can
> sometimes tricky.

Well, C doesn't have the record oriented I/O that Fortran has,
so if you want to process something a line at a time that is
pretty much the way. Otherwise, scanf() usually works for
something similar to list directer I/O, where record boundaries
are mostly ignored.

-- glen
From: Jim Xia on

> >> 1. Read the whole line into a character buffer and parse the buffer
> >> (possibly with the help of internal reads).
> > This is the most common technique.  When I program in C/C++, this is
> > also the approach I choose.  Although munipulating the string can
> > sometimes tricky.
>
> Well, C doesn't have the record oriented I/O that Fortran has,
> so if you want to process something a line at a time that is
> pretty much the way.  Otherwise, scanf() usually works for
> something similar to list directer I/O, where record boundaries
> are mostly ignored.
>


Well, you have choices of scanf() or getline() as two different
approaches in C although C is stream based IO. You also have an
option to overload the << operator in C++. I've used all these
methods, and I'd say getline() is the most straight forward approach
with the least amount of coding to handle most common errors in IO.
But if you want more robust IO handling, you probably end up
overloading <<, which could cost a lot more effort. I see similar
issues in Fortran.

Cheers,

Jim
From: robin on
"Richard Maine" <nospam(a)see.signature> wrote in message news:1jhm2us.zs8t7a108i3woN%nospam(a)see.signature...
| Dan Nagle <dannagle(a)verizon.net> wrote:
|
| > On 2010-04-27 10:52:36 -0400, Jim Xia <jimxia(a)hotmail.com> said:
| >
| > > "read (5,*) line" wouldn't work as the input data look like "A 1. 2.
| > > 3.". Only "A" will be read in. What you want is
| > > "read (5, '(a)') line"
| >
| > I think there should be an advance= 'no' or a backspace( 5)
| > in there somewhere.
|
| Those are alternative approaches. While they can also work ok in some
| cases, you don't need them *ALSO*; you could use them instead.
|
| The basic approaches commonly seen are
|
| 1. Read the whole line into a character buffer and parse the buffer
| (possibly with the help of internal reads).
|
| 2. Use non-advancing I/O (or a nonstandard variant such as the $ or \
| edit descriptors).
|
| 3. Use backspace
|
| 4. (sort of - I almost forgot about this one until others mentioned
| simillar things) Assume that any error must be the one you might be
| expecting. That's pretty full of problems, though. I disrecommend it.

All of those problems were resolved on other languages,
notably PL/I, as much as 40 years ago.

In PL/I, it's possible to read a file using any of the three forms
(formatted, not formatted, and named I/O) interchangeably.

That is equivalent to using Fortran's formatted, list-directed,
and NAMELIST I/O intermixed on the same file.
That can be done without re-reading records, or backspacing.