[comp.os.msdos.programmer] Quick Stream Field hack

ctl8588@rigel.tamu.edu (LAUGHLIN, CHET) (10/20/90)

Maybe someof you already know this, but there is a quick way to read
in string fields from a stream.  Simple use fscanf with the [^XYZ]
format (look it up..d:)  where X, Y and Z are end-of-field characters.
I hope this saves someone a sleepless night.  I can now do in two lines
what use to take me 10 or so....and it only took me 5 years to stumble
apon it while reading the run-time library manuals (what a dedicated
guy! d:)

For example, one can use the following to 'type' a DOS file...

while(!feof(fh))
{
   fscanf(fh,"[^\n] ",tempstr);
   printf("%s\n",tempstr);
}

WARNING!!! It is important to have the white space after the end bracket.
If your end-of-field is not a white space I'm not sure what you would
have to replace the space with......

+---------------------------------------------------------------+
| Chet Laughlin                  CTL8588@RIGEL.TAMU.EDU         |
|   "I have no opinions as I          (128.194.4.4)             |
|    do not exist, my lawyers                                   |
|    told me so."                                               |
+---------------------------------------------------------------+

so@brownie.cs.wisc.edu (Bryan So) (10/25/90)

In article <9355@helios.TAMU.EDU> ctl8588@rigel.tamu.edu writes:
= Maybe someof you already know this, but there is a quick way to read
= in string fields from a stream.  Simple use fscanf with the [^XYZ]
= format (look it up..d:)  where X, Y and Z are end-of-field characters.
= I hope this saves someone a sleepless night.  I can now do in two lines
= what use to take me 10 or so....and it only took me 5 years to stumble
= apon it while reading the run-time library manuals (what a dedicated
= guy! d:)
= 
= For example, one can use the following to 'type' a DOS file...
= 
= while(!feof(fh))
= {
=    fscanf(fh,"[^\n] ",tempstr);
=    printf("%s\n",tempstr);
= }

  I think you need a "percent" in front of [^\n].

  
= WARNING!!! It is important to have the white space after the end bracket.
= If your end-of-field is not a white space I'm not sure what you would
= have to replace the space with......

  You can use the following:

     fscanf(fh, "%[^\n]%*c", tempstr);

  %c will get any character from the stream.  The * in between tells
  fscanf to get rid of any character from the stream.


Bryan So