MathGroup Archive 1998

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Problems with Read[stream,String] after Read[stream,Number]

  • To: mathgroup at smc.vnet.net
  • Subject: [mg13791] Re: [mg13776] Problems with Read[stream,String] after Read[stream,Number]
  • From: David Withoff <withoff>
  • Date: Fri, 28 Aug 1998 04:18:16 -0400
  • Sender: owner-wri-mathgroup at wolfram.com

> I have noticed what seems an inconsistency in the way Mathematica
> imports data from a file.  The strange (to me, at least) behavior
> appears when one attempts to read a line with String data immediately
> after reading a line with Number data.
>
> In[2]:= !!test.dat
>
> 1
> 2
> 3
> 4
>
> In[3]:= strm=OpenRead["test.dat"];
> In[4]:= Read[strm,String]    (*Read Line 1 as String *) Out[4]= 1
> In[5]:= Read[strm,Number]     (*Read Line 2 as Number *) Out[5]= 2
> In[6]:= Read[strm,String]  (* Attempt to read Line 3 as String *)
> Out[6]=
> In[7]:= Read[strm,String]  (* Second attempt to read Line 3 as String *)
> Out[7]= 3
> In[8]:= Read[strm,Number]  (* Read Line 4 as Number *) Out[8]= 4 In[9]:=
> Close[strm];
>
> What am I missing here?  
>
> Thanks for any explanations or pointers thereto.

String format reads everything up to the next newline character and
leaves the stream pointer after the newline character.

Number format reads the next number and leaves the stream pointer after
the number.

This means that, after reading Number format from a number followed by a
newline character, the stream pointer will be left before the newline
character.  Reading String format when the stream pointer occurs
immediately before a newline character returns an empty string.

Skip[strm, String] is often used in situations like this where the
purpose is to flush any remaining characters on a line and start with a
new line.

Dave Withoff
Wolfram Research


  • Prev by Date: Re: Disappearing variables
  • Next by Date: mathlm question
  • Previous by thread: Problems with Read[stream,String] after Read[stream,Number]
  • Next by thread: Re: Matrix Multiplication