Note: This website is archived. For up-to-date information about D projects and development, please visit wiki.dlang.org.

Changes from Version 1 of ReplaceTextExample

Show
Ignore:
Author:
jcc7 (IP: 192.149.244.9)
Timestamp:
07/10/06 19:38:07 (18 years ago)
Comment:

created example page

Legend:

Unmodified
Added
Removed
Modified
  • ReplaceTextExample

    v0 v1  
     1= Replace Text Example = 
     2 
     3''Part of'' StandardLibraryCategory 
     4 
     5 
     6 
     7== Description == 
     8 
     9Take the input file and replace one string with another (repeatedly). 
     10 
     11 
     12 
     13== Example == 
     14 
     15{{{ 
     16#!d 
     17import std.stream; /* for File */ 
     18import std.string; /* for replace */ 
     19import std.stdio;  /* for writefln */ 
     20 
     21 
     22 
     23void main(char[][] args) 
     24{ 
     25        char[] fromFileStr; 
     26        char[] toFileStr; 
     27 
     28        char[] fromStr; 
     29        char[] toStr; 
     30 
     31        char[] fileStr; 
     32 
     33        char[] lnStr; 
     34 
     35        if(args.length < 5) 
     36        { 
     37                writefln(`Usage: replace_text.exe fileIn fileOut "from" "to"` \n); 
     38                return; 
     39        } 
     40 
     41        debug writefln("args.length: %s", args.length); 
     42        debug for(int i = 0; i < args.length; i++) writefln("args[%s]: %s", i, args[i]); 
     43 
     44        fromFileStr = args[1]; 
     45        toFileStr = args[2]; 
     46        fromStr = args[3]; 
     47        toStr = args[4]; 
     48 
     49    File inFile = new File(fromFileStr); 
     50    while (!inFile.eof()) 
     51    { 
     52        lnStr = inFile.readLine(); 
     53                debug writefln("%s", lnStr); 
     54                fileStr ~= lnStr ~ \n; 
     55    }     
     56    inFile.close();     
     57 
     58        fileStr = replace(fileStr, fromStr, toStr); 
     59 
     60    File outFile = new File(); 
     61    with(outFile) 
     62    { 
     63        outFile.create(toFileStr); 
     64        outFile.writeString(fileStr); 
     65        outFile.close();     
     66    } 
     67 
     68        return; 
     69} 
     70}}} 
     71 
     72 
     73 
     74== Batch File == 
     75 
     76{{{ 
     77@echo off 
     78dmd ReplaceTextExample.d 
     79ReplaceTextExample.exe 
     80ReplaceTextExample.exe "ReplaceTextExample.d" "ReplaceTextExampleX.d" "Str" "String" 
     81pause 
     82}}} 
     83 
     84 
     85 
     86== Compatibility Notes == 
     87 
     88Tested with Digital Mars D Compiler v0.162 on Windows 2000.