Mathematica 9 is now available
Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2006
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2006

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

Search the Archive

Re: Input, output troubles me ...

  • To: mathgroup at smc.vnet.net
  • Subject: [mg70061] Re: Input, output troubles me ...
  • From: Peter Pein <petsie at dordos.net>
  • Date: Sun, 1 Oct 2006 04:10:43 -0400 (EDT)
  • References: <efldl4$d6s$1@smc.vnet.net>

luigirmailbox-mathematica at yahoo.it schrieb:
> Dear Friends
> 
> I have been using Mathematica for text file processing for a long time.
> I found it's a valuable tool for text complex manipulations.
> 
> The strategy I was used to apply is well condensed in this childish
> example:
> 
> ---------------------------------
> (*loading the content*)
> dummy = Import["c:\\myDir\\inputFile.txt", "Lines"];
> (*processing it*)
> dummy = Select[dummy, (# != "a") &];
> (*exporting it*)
> Export["c:\\myDir\\outpuFile.txt", dummy, "Lines"];
> ---------------------------------
> 
> Now I have to face huge file (up to 5,6 Gb sized) and the previous
> technique mostly crashes my systems.
> 
> I would like to process a single line, rather than the whole load,
> doing something similar what a classical programmig language does:
> 
> ---------------------------------
> Open "c:\\myDir\\inputFile.txt" for Input As #1
> Open "c:\\myDir\\outputFile.txt" for Input As #2
> 
> Do While Not Eof(#1)
>    Line Input #1, dummy$
>    If dummy$ <> "a" Then Print #1, dummy$
> Loop
> 
> Close #1
> Close #2
> ---------------------------------
> 
> I tried this (and many others!) trick:
> 
> ---------------------------------
> stream1 = OpenRead["c:\\myDir\\inputFile.txt"];
> stream2 = OpenWrite["c:\\myDir\\outputFile.txt"];
> While[
>    dummy != EndOfFile,
>    dummy = Read[stream1, "String"];
>    If[dummy != "a", Write[stream2, dummy]];
> ]
> Close[stream1];
> Close[stream2];
> ---------------------------------
> 
> But it doesn't work: when inputFile is
> 
> a
> b
> c
> d
> 
> outputFile is void, not
> 
> b
> c
> d
> 
> as expected.
> 
> Plaese can someone help me?
> 
> Thanks for your attention.
> Goobye.
> 

Hi,

I guess you tried this some times without clearing the value of dummy, 
which was EndOfFile from the last attempt? The following works:

In[1]:=
SetDirectory[$HomeDirectory];
In[2]:=
!!"inputFile.txt"
 From In[2]:=
a
b
c
d
In[3]:=
Module[{instream = OpenRead["inputFile.txt"],
         outstream = OpenWrite["outputFile.txt"],
         dummy},
   While[(dummy = Read[instream, "String"]) =!= EndOfFile,
     If[dummy =!= "a",
       Write[outstream, dummy] ]
   ];
   Close /@ {outstream, instream}
];
In[4]:=
!!"outputFile.txt"
 From In[4]:=
"b"
"c"
"d"
In[5]:=
ResetDirectory[];

Peter


  • Prev by Date: Re: distance function
  • Next by Date: Re: HoldPattern question
  • Previous by thread: Re: Input, output troubles me ...
  • Next by thread: Re: Input, output troubles me ...