MathGroup Archive 1998

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

Search the Archive

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

  • To: mathgroup at smc.vnet.net
  • Subject: [mg13776] Problems with Read[stream,String] after Read[stream,Number]
  • From: "L. Dwynn Lafleur" <lafleur at usl.edu>
  • Date: Mon, 24 Aug 1998 05:07:25 -0400
  • Organization: University of Southwestern Louisiana
  • 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 the following contrived notebook fragment, I expected that no more
than four Read statements would be needed to import the four lines of
data, one at a time, no matter whether they are interpreted as Strings
or Numbers.  (This is in Mathematica 3.0.1 under Windows 95.)

---------------

In[2]:= !!test.dat

1
2
3
4

(***** Test 1  *****)

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];

---------------

Note that the first attempt to read Line 3 fails.  It is the only case
of a String line being read after a Number line.  A more thorough
investigation shows that the extra Read line returns one of the
characters (I have forgotten which) from the CR/LF line terminator used
in DOS/Windows.  

Note there is no problem when a Number line is reaed after a String
line.  And the next test shows that String-after-String and
Number-after-Number are okay.

(***** Test 2  *****)

In[10]:= strm=OpenRead["test.dat"];
In[11]:= Read[strm,String]    (*Read Line 1 as String *) Out[11]= 1
In[12]:= Read[strm,String]     (*Read Line 2 as String *) Out[12]= 2
In[13]:= Read[strm,Number]   (*Read Line 3 as Number *) Out[13]= 3
In[14]:= Read[strm,Number]  (* Read Line 4 as Number *) Out[14]= 4
In[15]:= Close[strm];


What am I missing here?  A search of the Mathematica Bible, other books,
and Deja News has only turned up examples of using Read and ReadList to
import Strings followed by Numbers.  That works the way I would have 
expected, as shown above.

Thanks for any explanations or pointers thereto.

-- 

L. Dwynn Lafleur
Professor of Physics
The University of Southwestern Louisiana Lafayette, Louisiana  (USA)
lafleur at usl.edu


  • Prev by Date: Re: Description of the function
  • Next by Date: Re: Matrix Multiplication
  • Previous by thread: Doing a table of contents ; also : footnotes ?
  • Next by thread: Re: Problems with Read[stream,String] after Read[stream,Number]