Q: simple example of read file->convert char(s)->write file needed 
Author Message
 Q: simple example of read file->convert char(s)->write file needed

 Could someone give me a simple example of how to read a file
then convert one (or more) character to other character(s) and
then write file back to disk.
 Any help appreciated.

 Jari Sepp?l?
 TUT / ACI

--
*** Jari Sepp?l?                                                     ***
*** Tampere University of Technology Finland / ACI                     ***



Mon, 23 Nov 1998 03:00:00 GMT  
 Q: simple example of read file->convert char(s)->write file needed



:>
:> Could someone give me a simple example of how to read a file
:>then convert one (or more) character to other character(s) and
:>then write file back to disk.
:> Any help appreciated.
:>

Jari Sepp?l?

Here a simple one for you.

 /* Start of REXX */
 infile = 'c:\config.sys'
 otfile = 'c:\test.txt'
 fsize = stream(infile,'C','QUERY SIZE')
 wstr  = charin(infile,1,fsize)
 call stream(infile,'C','CLOSE')
 newdata = changestr('C',wstr,'z')
 call charout(otfile,newdata,0)
 call stream(otfile,'C','CLOSE')
 RETURN
 /* End of REXX */

DG

The above rexx script read in C:\CONFIG.SYS in one big gulp.
Replaces all 'C' with 'z'



Mon, 23 Nov 1998 03:00:00 GMT  
 Q: simple example of read file->convert char(s)->write file needed



:>
:> Could someone give me a simple example of how to read a file
:>then convert one (or more) character to other character(s) and
:>then write file back to disk.
:> Any help appreciated.

Reading the file is quite easy with LineIn while writing it can be achieved
with LineOut.
If your translation is one for one then the translate function can be used.
Alas, if you want to replace one character by more than one, then there is no
Rexx function to achieve that and you will have to do it yourself.

The skeletons I think about can be :

/* 1 : one to one conversion */
Arg FileIn FileOut
tableIn  = 'abc'   /* The characters to be changed */
TableOut = '123'   /* a -> 1, b -> 2, c-3 */

Do While Lines(FileIn) > 0
  Call LineOut FileOut,Translate(LineIn(FileIn),TableOut,TableIn)
End

Call Stream FileIn,'C','CLOSE'
Call Stream FileOut,'C','CLOSE'
Exit

/* 2 : one to n conversion */
Arg FileIn FileOut
char. = ''         /* default value is nothing */
char.61 = '1a'     /* 61 is the hexcode for a, 1a is what I want to */
                   /* use for replacing any a found in my file      */
char.62 = '2b'     /* idem for b -> 2b */
char.63 = '3c'    
Do While Lines(FileIn) > 0
  In = LineIn(FileIn)
  out = ''
  Do While In <> ''
    Parse Var In c +1 In  /* Extract one char in c and remove it from in */
    xc = x2c(c)
    If char.xc <> '' Then  Out = Out || Char.xc
                     Else  Out = Out || c
  End
  Call LineOut FileOut,Out
End

Call Stream ... etc

If You need also to remove one character, you cannot use the default value
and you'll have to initialize the whole char. stem
do i = 0 to 256
  xc = d2x(i)
  Char.xc = x2c(xc)
End
and then change the values you want to. In the working loop, always change
the character with the corresponding char. element.

WARNING: This code has been just written here and not tested in any way.
One feature to add is obviously to delete the outfile at the very beginning,
perhaps asking the user if this is acceptable aso.

Hope this helps, Write me if you think you need more info.

Jean-Pierre



Tue, 24 Nov 1998 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. >>>>>How to read a IMAGE file?(BMP, GIF)|||||||||

2. .cob files >>> .exe files

3. error reading <<file>>, aborting

4. HELP>>>>>>>Fortran-Pascal Linking

5. >>>>>>>FROM SMTK TO C++

6. Please help Visual Basic Programer with simple read/write text file routine---Real Basic 2.1

7. ><><><><>Heeeeeeeeeeeeeeelp on INT 14!><><><><><>

8. <<<<<YOU MUST CHECK THIS OUT >>>>>>>>>> 2103

9. C->Ada: Converting char* to unsigned long

10. How to convert byte -> char

11. Reading from input file writing to output file

12. Reading a binary file / writing binary data to a file

 

 
Powered by phpBB® Forum Software