Die Herausforderung:
Geben Sie Text aus einer Datei ein und geben Sie ihn in eine andere aus. Die Lösung sollte eine vollständige, funktionierende Funktion sein.
Hinweis: Dies ist eine Code-Trolling- Frage. Bitte nehmen Sie die Frage und / oder die Antworten nicht ernst. Mehr Infos hier .
popularity-contest
code-trolling
Gemeinschaft
quelle
quelle
Antworten:
C
Der springende Punkt bei der Verwendung mehrerer Programmiersprachen ist, dass Sie das richtige Werkzeug für den Job verwenden müssen.
In diesem Fall möchten wir Bytes von einer Datei in eine andere kopieren.
Während wir etwas Phantasievolles wie Bash oder Ruby gebrauchen oder die Kraft von ASM nutzen könnten, brauchen wir etwas Schnelles, Gutes mit Bits und Schnelles.
Die offensichtliche Wahl ist C.
Die EINFACHSTE Art, dies zu tun, wäre wie folgt:
Das ist böse, weil es Müll über Dinge wie Cygwin liest, für Anfänger komplett chinesisch ist, ihn erschreckt, wenn er merkt, dass es ein Hilferuf ist, und offensichtlich, weil C.
quelle
#define VERY filename1
#define NOT filename2
#define _ 1
, you can make(;_;)
with the for-loop, to make it look like a smiley.#define LOL fclose
made my day#define LOL fclose
, which is never used is my masterpiece though.sh
I like the simplicity of this one.
Really only works properly if
File1.txt
contains "File1.text"...quelle
File2.txt
, not the actual contents ofFile1.txt
AutoHotKey
You must first open the files in notepad or some other text editor that makes the window names start with the file names. Then it copies the contents of file1 to the clipboard (literally, using ctrl+c), and pastes it into file2, overwriting anything currently in it, and saves.
Solves the problem, but in a very inconvenient and pretty useless fashion. It would probably by easier to just copy and paste manually.
quelle
As everyone knows, perl is very good for manipulating files. This code will copy the contents of one file to another, and will do so with extra redundancy for good measure.
To be safe, test this out on a small file at first. Once you are convinced of its correctness you can put it into production for use on very large files without worry.
quelle
C#
In order to achieve this, you must make sure to do several things:
Here's the code:
quelle
In four simple steps:
lpr
command for this.quelle
Java
A lot of people find it useful to attempt to use regular expressions or to evaluate strings in order to copy them from one file to another, however that method of programming is sloppy. First we use Java because the OOP allows more transparency in our code, and secondly we use an interactive interface to receive the data from the first file and write it to the second.
In theory (I haven't tested it) this makes the user manually input the content of the first file (word by word) and then writes it to the second file. This response is a play on the ambiguity of the question as to what it means by "input text from one file," and the crazy variable names (generated randomly) were just for some extra fun.
quelle
sh
Like @Shingetsu pointed out, one has to use the right tool for the right job. Copying the content from one file to another is an old problem, and the best tool for such file-management tasks is using the shell.
One shell command that every programmer has to familiarise themself with is the common
tac
command, used to tack the content of files together, one after another. As a special case of this, when only one file is passed in it is simply spit out as-is again. We then simply redirect the output to the appropriate file:Simple and to-the-point, no tricks!
quelle
Perl
Antivirus included.
quelle
Windows Batch
(because you gotta have this supposedly "dead" language ;-)
You simply call this as
copy.bat file1.txt file2.txt
(or whatever files you want)If only it would keep line breaks...
quelle
setlocal enabledelayedexpansion
andset thisline=!thisline!%%I
works only in Windows CMD. In DOS must work simpleset thisline=%thisline%%%I
Linq
Efficiency is not important, correctness is. Writing in a functional style results in more clear and less error-prone code. If performance does turn out to be a problem, the last ToArray() line can be omitted. It's better to be lazy anyway.
quelle
Smalltalk
There is a library yet ported to several Smalltalk dialects (Visualworks, Gemstone Squeak/Pharo, ...) named Xtreams that makes this task more than easy.
FileStream would be as simple as
'foo' asFilename reading
and'bar' asFilename writing
for example in Visualworks, but are dialect specific.For this reason I demonstrate the algorithm with dialect neutral internal Streams instead.
A good idea could be to process each byte code in increasing order:
Of course, it is also possible to process in random order, but i'm afraid that it makes the code a bit too compact:
EDIT
Ah stupid me, I didn't saw the log2 solution:
quelle
BASH
on terminal #1 with the IP, 192.168.1.2
on terminal #2 with the IP, 192.168.1.3
This will encrypt and send
mydocument.docx
, usingnc
andgpg
, over to terminal #1 You will have to type the password on terminal #2, then on terminal #1quelle
C++
This is somewhat based on Shingetsu's answer, but I couldn't resist. It is fully functioning, but no student would submit it to their teacher (I hope). If they are willing to analyze the code, they will be able to solve their problem:
quelle
Python
Inputs text from file1.txt and outputs it to file2.txt
It's complete and "working". No one said anything about writing the input as it is. All input characters appear in the output. "getchar" is the trolling part.
quelle
Mathematica, 44 characters
Implementation
Execution
Check
quelle
CopyFile
function.DELPHI / PASCAL ( copy from f1.txt to f2.txt )
quelle
MASM
I'm certainly not an assembly expert, but below is my little snippet:
quelle
C++
Did you notice the "complete, working function" bit? Anyway, here is my answer:
quelle
Lua
Run with an input file containing
text from one file and output it to another. Solution should be a complete, working function.
.quelle
PHP
quelle
#!/bin/sh
Looks reasonable, but rather inefficient if the file is large.
Works fine on ASCII files except when the input file contains
-n
,-e
or-E
. (Because these are interpreted as arguments byecho
.)Does not produce the correct output for all (most) binary files.
(Using
printf "%s" "$contents" > output
under/bin/bash
works a little better, but that still drops NULL bytes.)Oh and of course it doesn't work for filenames_containing_spaces. But such files are illegal under UNIX%20policy anyway.
quelle