...

/

Solution: Write the Contents of a File to Another File

Solution: Write the Contents of a File to Another File

This lesson provides a solution to the challenge given in the previous lesson.

We'll cover the following...

Solution #

Here is the code that will write the contents of a file to another file.

Press + to interact
import std.stdio;
import std.string;
void main() {
string inFileName = "foo.txt";
File inFile = File(inFileName, "r");
string outFileName = inFileName ~ ".out";
File outFile = File(outFileName, "w");
while (!inFile.eof()) {
string line = strip(inFile.readln());
if (line.length != 0) {
outFile.writeln(line);
}
}
writeln(outFileName, " has been created.");
inFile = File(outFileName, "r");
while (!inFile.eof()) {
string line = strip(inFile.readln());
writeln(line);
}
}

Solution explanation #

...