Re: Reading Simple Flatfiles with a COBOL Mindset

From: Eric Sosman (Eric.Sosman_at_sun.com)
Date: 09/09/04


Date: Thu, 09 Sep 2004 16:41:53 -0400

KevinD wrote:
> assumption: I am new to C and old to COBOL
>
> I have been reading a lot (self teaching) but something is not sinking
> in with respect to reading a simple file - one record at a time.
> Using C, I am trying to read a flatfile. In COBOL, my simple file
> layout and READ statement would look like below.
>
> Question: what is the standard, simple coding convention for reading
> in a flatfile - one record at a time?? SCANF does not work because of
> spaces; I tried FGETS and STRUCT to emulate my COBOL perspective but
> that does not work (though I may have coding this wrong). C likes to
> deliver data in streams but FGETS is akin to reading a single record.
>
> I know I am missing something that is very simple but the examples
> that I have come across avoid this simple scenario. Please explain -
> an example would be great.

     C's I/O streams have no notion of "record," aside from
the fairly weak notion of "line" as expressed in fgets() and
a few other, relatively obscure parts of the library.

     But don't despair. Ask yourself "What does a record
look like, when thought of as an undifferentiated stream of
bytes?" Then read the appropriate number of bytes from the
stream and impose your interpretation on them: The first
five are alphabetic characters denoting a stock ticker
symbol, the next ten are decimal digits giving the last
trade price in millicents, the next twenty are alphabetics
giving the name of the latest executive to serve his
company from behind bars, and so on. Extract whatever's
needed from this layout, convert it to more convenient forms
if you like (e.g., the fields of decimal digits might become
`int' or `double' values), and away you go.

     C streams come in two principal flavors (three, really,
but I have a hunch you're not interested in wide characters
just yet): there are text streams and binary streams. If
your data file looks like a bunch of lines consisting of
textual characters, you should access it with a text stream:
use "r" as the second argument to fopen(). But if your
file contains "binary garbage" like numbers expressed in
binary or packed decimal format, use a binary stream: pass
"rb" as fopen()'s second argument.

     In the binary case, it *may* be that you can use fread()
to plop a fixed number of bytes from the file straight into
a properly-arranged struct, and be on your way without any
need for further interpretation. However, there are lots of
pitfalls in this approach, and I couldn't recommend it without
knowing a lot more about your situation than I'm likely to
have time to discover.

-- 
Eric.Sosman@sun.com


Relevant Pages

  • Re: OO Reuse
    ... >> when reading about software, you may find a reference to ... Does a true COBOL programmer need those CRC cards - I think not. ... > The point of mentioning Simon - young, bright and can absorb stuff like ... > famous English tea firms. ...
    (comp.lang.cobol)
  • Re: Reading from standard input
    ... I finally understood the concept of '-' stream. ... I am learning perl scripting and was reading an online tutorial where i ... Can somebody explain me why reading from the standard input ...
    (comp.lang.perl.misc)
  • StreamReader.Read blocking?
    ... spawn threads that handle the reading of the streams by placing the stream ... Debug.WriteLine("Started reading standard output."); ... characters are read, or the end of the file is reached. ... About ReadBlock(), MSDN says: The method blocks until either count ...
    (microsoft.public.dotnet.framework)
  • StreamReader.Read blocking?
    ... spawn threads that handle the reading of the streams by placing the stream ... Debug.WriteLine("Started reading standard output."); ... characters are read, or the end of the file is reached. ... About ReadBlock(), MSDN says: The method blocks until either count ...
    (microsoft.public.dotnet.framework)
  • StreamReader.Read blocking?
    ... spawn threads that handle the reading of the streams by placing the stream ... Debug.WriteLine("Started reading standard output."); ... characters are read, or the end of the file is reached. ... About ReadBlock(), MSDN says: The method blocks until either count ...
    (microsoft.public.dotnet.framework)

Loading