How do I iteratively replace lines in a file?

Andrej Mitrovic none at none.none
Sat Mar 19 16:51:19 PDT 2011


I'm trying to do something like the following:

File inputfile;
foreach (string name; dirEntries(r".\subdir\", SpanMode.shallow))
{
    if (!(isFile(name) && getExt(name) == "d"))
    {
        continue;
    }
    
    inputfile = File(name, "a+");
    
    foreach (line; inputfile.byLine)
    {
        if (line == "import foo.d")
        {
            inputfile.write("import bar.d");  // or ideally `line = "import bar.d"`
        }
    }
}

That obviously won't work. I think I might need to use the `fseek` function to keep track of where I am in the file, or something like that. File I/O in D is no fun..


More information about the Digitalmars-d-learn mailing list