Thread overview
Any additions for write-to-file short program
Nov 18, 2021
pascal111
Nov 18, 2021
H. S. Teoh
Nov 19, 2021
pascal111
Nov 19, 2021
Imperatorn
Nov 19, 2021
H. S. Teoh
Nov 19, 2021
pascal111
Nov 18, 2021
Stanislav Blinov
November 18, 2021

In next program that rewrites original written texts into new files, I see that it may need some additions or we can accept it like this because it's just a simple program that achieve its task and doesn't need any philosophical additions.

Example:

Original text:

Learning C doesn't impact bad on C++. In theoretical speaking we think that C++ is C with some new features, but these features maybe tends to some high level, and working with C is more difficult than direct learning of C++, because last one provides features or tools make us pass some low level ways we have to use in C to do same tasks, so, the work with C impact good on learning C++ because we see everything with its real nature in C.

New produced text in new file:

Learning C doesn't impact bad on C++. In theoretical speaking we think that C++ is C with some new features, but these features maybe tends to some high level, and working with C is more difficult than direct learning of C++, because last one provides features or tools make us pass some low level ways we have to use in C to do same tasks, so, the work with C impact good on learning C++ because we see everything with its real nature in C.

Code:

// D programming language

import std.stdio;
import std.string;

int main()
{

string s;
char[] f;

try{
write("Enter file name and path: ");
readln(f);
f=strip(f);}

catch(Exception err){
stderr.writefln!"Warning! %s"(err.msg);}

File file = File(f, "r");
File file2 = File("output_x.txt", "w");

while (!file.eof()) {
      s = chomp(file.readln());
      file2.writeln(s);
   }

file.close();
file2.close();

return 0;

}

November 18, 2021
On Thu, Nov 18, 2021 at 10:20:48PM +0000, pascal111 via Digitalmars-d-learn wrote:
> In next program that rewrites original written texts into new files, I see that it may need some additions or we can accept it like this because it's just a simple program that achieve its task and doesn't need any philosophical additions.

I assume there's a reason you're reading the input file by line instead of just copying it using larger fixed-size blocks?  Perhaps you have in mind some kind of line-based filtering or processing eventually?

Because for copying a file, using a large, fixed-size block will work much faster. Not to mention the code will be simpler. For example:

	auto inputFile = File(inputFilename, "r");
	auto outputFile = File(outputFilename, "w");

	enum bufferSize = 8194;
	inputFile.byChunk(bufferSize)	// read input in blocks of 8194 bytes
		.copy(outputFile.lockingBinaryWriter); // copy each block into output file


T

-- 
Javascript is what you use to allow third party programs you don't know anything about and doing you know not what to run on your computer. -- Charles Hixson
November 18, 2021

On Thursday, 18 November 2021 at 22:20:48 UTC, pascal111 wrote:

>

In next program that rewrites original written texts into new files, I see that it may need some additions or we can accept it like this because it's just a simple program that achieve its task and doesn't need any philosophical additions.

If "the task" is to copy a file, then this program, as presented, may fail at it. If you're going to do any exception handling at all, a question must be answered: what to do in case the algorithm is interrupted before completing? I.e. if the while loop throws an exception, which it can do on any line except for braces. Is it acceptable to leave the output file containing only part of input data, perhaps even no data at all? Or should you catch the exception and delete the output file? Will that even be possible?..

November 19, 2021
On Thursday, 18 November 2021 at 23:09:28 UTC, H. S. Teoh wrote:
> On Thu, Nov 18, 2021 at 10:20:48PM +0000, pascal111 via Digitalmars-d-learn wrote:
>> In next program that rewrites original written texts into new files, I see that it may need some additions or we can accept it like this because it's just a simple program that achieve its task and doesn't need any philosophical additions.
>
> I assume there's a reason you're reading the input file by line instead of just copying it using larger fixed-size blocks?  Perhaps you have in mind some kind of line-based filtering or processing eventually?
>
> Because for copying a file, using a large, fixed-size block will work much faster. Not to mention the code will be simpler. For example:
>
> 	auto inputFile = File(inputFilename, "r");
> 	auto outputFile = File(outputFilename, "w");
>
> 	enum bufferSize = 8194;
> 	inputFile.byChunk(bufferSize)	// read input in blocks of 8194 bytes
> 		.copy(outputFile.lockingBinaryWriter); // copy each block into output file
>
>
> T


When I compiled the code after adding yours, I found this error message:

"untitled20.d:24:8: error: no property 'copy' for type 'ByChunk'
   24 |        .copy(outputFile.lockingBinaryWriter); // copy each block into output file
      |        ^
"



November 19, 2021
On Friday, 19 November 2021 at 22:21:52 UTC, pascal111 wrote:
> On Thursday, 18 November 2021 at 23:09:28 UTC, H. S. Teoh wrote:
>> [...]
>
>
> When I compiled the code after adding yours, I found this error message:
>
> "untitled20.d:24:8: error: no property 'copy' for type 'ByChunk'
>    24 |        .copy(outputFile.lockingBinaryWriter); // copy each block into output file
>       |        ^
> "

Did you import std.algorithm?
November 19, 2021
On Fri, Nov 19, 2021 at 10:21:52PM +0000, pascal111 via Digitalmars-d-learn wrote: [...]
> When I compiled the code after adding yours, I found this error message:
> 
> "untitled20.d:24:8: error: no property 'copy' for type 'ByChunk'
>    24 |        .copy(outputFile.lockingBinaryWriter); // copy each block
> into output file
>       |        ^
> "

import std.algorithm;


T

-- 
It is impossible to make anything foolproof because fools are so ingenious. -- Sammy
November 19, 2021
On Friday, 19 November 2021 at 23:05:03 UTC, H. S. Teoh wrote:
> On Fri, Nov 19, 2021 at 10:21:52PM +0000, pascal111 via Digitalmars-d-learn wrote: [...]
>> When I compiled the code after adding yours, I found this error message:
>> 
>> "untitled20.d:24:8: error: no property 'copy' for type 'ByChunk'
>>    24 |        .copy(outputFile.lockingBinaryWriter); // copy each block
>> into output file
>>       |        ^
>> "
>
> import std.algorithm;
>
>
> T

Thanks! it works now.

code:

// D programming language

import std.stdio;
import std.string;
import std.algorithm;

int main()
{

string s;
char[] f;


try{
write("Enter file name and path: ");
readln(f);
f=strip(f);


File inputFile = File(f, "r");
File outputFile = File("output_x", "w");

enum bufferSize = 8194;
inputFile.byChunk(bufferSize)	// read input in blocks of 8194 bytes
       .copy(outputFile.lockingBinaryWriter); // copy each block into output file


inputFile.close();
outputFile.close();
}


catch(Exception err){
stderr.writefln!"Warning! %s"(err.msg);
return 1;
}



return 0;

}