Take the tour ×
Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It's 100% free, no registration required.

The challenge:

Input text from one file and output it to another. Solution should be a complete, working function.

Note: This is a question. Please do not take the question and/or answers seriously. More information here.

share|improve this question
add comment

10 Answers

up vote 30 down vote accepted

C

The whole point of having multiple programming languages, is that you need to use the right tool for the job.
In this case, we want to copy bytes from one file to another.
While we could use something fancy like bash or ruby, or fall to the power of ASM, we need something quick, good with bits and fast.
The obvious choice is C.
The SIMPLEST way of doing it would be like so:

#include <stdio.h>
#define THEORY FILE
#define AND *
#define PRACTICE myfile1
#define SOLUTIONS myfile2
#define ARE fopen
#define IMPORTANT "r"
#define BAD "w"
#define LOL fclose
#define PLEASE fgetc
#define HELP fputc
#define ME return
#define NEVER for
#define SURRENDER !=
#define VERY filename1
#define NOT filename2

int copyFile(char* filename1, char* filename2)
{
  THEORY AND PRACTICE = ARE (VERY, IMPORTANT);
  THEORY AND SOLUTIONS = ARE (NOT, BAD);
  NEVER (;;)
  {
    HELP(PLEASE(PRACTICE),SOLUTIONS);
  }
  ME 1 SURRENDER 1;
}

This is evil because it will read garbage on things like cygwin, is completely chinese for a beginner, will scare him when he realizes it's a call for help and, obviously, because C.

share|improve this answer
7  
#define VERY filename1 #define NOT filename2 –  Joe Z. Dec 28 at 5:01
 
Great job! You made me laugh with those names! –  user2509848 Dec 28 at 5:01
1  
Changed those defs for you. I kept them the same on the function declaration for MORE confusion! –  Shingetsu Dec 28 at 5:04
8  
Also, if you #define _ 1, you can make (;_;) with the for-loop, to make it look like a smiley. –  Joe Z. Dec 28 at 5:14
4  
#define LOL fclose made my day –  tomwilde 2 days ago
show 3 more comments

sh

I like the simplicity of this one.

echo File1.txt > File2.txt

Really only works properly if File1.txt contains "File1.text"...

share|improve this answer
 
What is wrong with it? –  user2509848 Dec 28 at 4:25
4  
@user2509848 It puts the text "File1.txt" into File2.txt, not the actual contents of File1.txt –  syb0rg Dec 28 at 4:35
 
Thanks for the info. –  user2509848 Dec 28 at 4:36
 
But this doesn't actually cheat the question... –  Jeremy Dec 28 at 4:49
1  
Could just as well be sh as there isn't anything bash specific. –  Kaya Dec 28 at 4:58
show 7 more comments

AutoHotKey

WinActivate, file1.txt
SendInput ^a^c
WinActivate, file2.txt
SendInput ^a^v^s

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.

share|improve this answer
 
Excellent! I wonder what the teacher would think? –  user2509848 2 days ago
add comment

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.

#Good perl programs always start thusly
use strict;
use warnings;

my $input_file  = 'File1.txt';
my $output_file = 'File2.txt';
open(my $in, '<', $input_file) or die "Couldn't open $input_file $!";

my $full_line = "";
while (my $line = <$in>) {
    #Get each letter one at a time for safety, 
    foreach my $letter (split //, $line) {
        #You could and should add validity checks here
        $full_line .= $letter;

        #Get rid of output file if it exists!  You are replacing it with
        # new content so it's just wasting space.
        unlink $output_file if (-e $output_file);

        #Write data to new file
        open(my $out, '>', $output_file) or die "Couldn't open $output_file $!";
        print {$out} $full_line;
        close($out);
    }
}

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.

share|improve this answer
 
Does this code output it once, destroy the file, then try to output it again from the first output file? –  user2509848 Dec 28 at 6:31
2  
The first pass through it writes the first character of the old file to the new file. The second time through it deletes the new file and writes the first two characters of the old file to the new file. The third time, three characters, etc. etc. Not only is it redundant, but if your computer crashes you will (possibly) have a partial file! –  dms Dec 28 at 7:06
add comment

C#

In order to achieve this, you must make sure to do several things:

  1. Read string from file
  2. Download Microsoft File IO and String Extension for Visual Studio
  3. Remove viruses from string ("sanitize")
  4. Write file to path
  5. Delete and reload cache

Here's the code:

static void CopyTextFile(string path1, string path2)
    {
        try
        {
            FileStream fs = new FileStream(path1, FileMode.OpenOrCreate); //open the FTP connection to file
            byte[] file = new byte[fs.Length];
            fs.Read(file, 0, file.Length);
            string makeFileSafe = Encoding.UTF32.GetString(file);

            using (var cli = new WebClient())
            {
                cli.DownloadData(new Uri("Microsoft .NET File IO and String Extension Package")); //get MS package
            }

            fs.Dispose();

            File.Create(path2);
            StreamReader read = new StreamReader(path2);
            read.Dispose(); //create and read file for good luck

            var sb = new StringBuilder();
            foreach (char c in path1.ToCharArray())
            {
                sb.Append(c);
            }

            string virusFreeString = sb.ToString(); //remove viruses from string

            File.WriteAllText(path2, virusFreeString);
            File.Delete(path1);
            File.WriteAllText(path1, virusFreeString); //refresh cache
        }
        catch
        { 
            //Don't worry, exceptions can be safely ignored
        }
    }
share|improve this answer
add comment

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.

import java.util.*;
import java.io.*;

public class WritingFiles{



 public static void main(String []args){
    File tJIAgeOhbWbVYdo = new File("File1.txt");
    Scanner jLLPsdluuuXYKWO = new Scanner(tJIAgeOhbWbVYdo);
    while(jLLPsdluuuXYKWO.hasNext())
    {
        MyCSHomework.fzPouRoBCHjsMrR();
        jLLPsdluuuXYKWO.next();
    }
 }
}

class MyCSHomework{
    Scanner jsvLfexmmYeqdUB = new Scanner(System.in);
    Writer kJPlXlLZvJdjAOs = new BufferedWriter(new OutputStreamWriter( new  FileOutputStream("File2.txt"), "utf-8"));
    String quhJAyWHGEFQLGW = "plea";
   String LHpkhqOtkEAxrlN = "f the file";
   String SLGXUfzgLtaJcZe = "e nex";
   String HKSaPJlOlUCKLun = "se";
   String VWUNvlwAWvghVpR = " ";
   String cBFJgwxycJiIrby = "input th";
   String ukRIWQrTPfqAbYd = "t word o";
   String  CMGlDwZOdgWZNTN =   quhJAyWHGEFQLGW+HKSaPJlOlUCKLun+VWUNvlwAWvghVpR+cBFJgwxycJiIrby+SLGXUfzgLtaJcZe+ukRIWQrTPfqAbYd+LHpkhqOtkEAxrlN;
    public static void fzPouRoBCHjsMrR(){
        System.out.println(CMGlDwZOdgWZNTN);
        kJPlXlLZvJdjAOs.write(jsvLfexmmYeqdUB.next());
    }



}

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.

share|improve this answer
2  
I was thinking about making something similar to this... not misinterpreting the part input text frome one file, but following a mathematician's approach saying I've reduced the problem to an already solved one. => handling screen input. Didn't come up with a good solution though... –  Derija93 2 days ago
add comment

In four simple steps:

  1. Print the file. You can use the lpr command for this.
  2. Mail the printouts to a scanning service. (You will need postage for this).
  3. The scanning service will scan the printouts and do OCR for you.
  4. Download to the appropriate file location.
share|improve this answer
 
Good job creating a useless answer! –  user2509848 2 days ago
add comment

Perl

Antivirus included.

#!/usr/bin/perl -w
use strict;
use warnings;

print "Copy the text of the file here: (warning: I can't copy files with newlines):\n";
my $content = <STDIN>;

print "\n\nPlease wait, removing viruses...";
my @array = split(//,"$content");
my @tarray;

foreach my $item (@array)
{
    if ($item ne "V" && $item ne "I" && $item ne "R" && $item ne "U" && $item ne "S")
    {
        push(@tarray, $item);
    }

}

print "\n\nNow copy this into the target file:\n";

foreach my $item (@tarray){ print "$item"};
share|improve this answer
 
Very interesting, does it just pretend to check for viruses, or does it really do it? –  user2509848 2 days ago
1  
No, it only removes the letters V, I, R, U, S from the file. –  craftext 2 days ago
 
Oh. Since you say "removing viruses", maybe you should remove 'E' too. –  user2509848 2 days ago
1  
That's true. But if you want to really remove viruses, there is a CPAN module to do that: search.cpan.org/~converter/Mail-ClamAV-0.29. –  craftext 2 days ago
add comment

BASH

on terminal #1 with the IP, 192.168.1.2

nc -l 8080 | gpg -d > mydocument.docx

on terminal #2 with the IP, 192.168.1.3

gpg -c mydocument.docx | nc 192.168.1.2 8080

This will encrypt and send mydocument.docx, using nc and gpg, over to terminal #1 You will have to type the password on terminal #2, then on terminal #1

share|improve this answer
 
Can you mark the code? –  user2509848 2 days ago
 
im a newbie, but here goes, nc -l 8080 tells netcat to listen on port 8080. anything sent to 192.168.1.2:8080 will show up on terminal #1 on terminal #2, gpg encrypts mydocument.docx, and pipes that to netcat. nc 192.168.1.2 8080 sends the encrypted mydocument.docx over to terminal #1 when #1 recieves it, it unencrypts it with gpg -d , and saves i –  Fews1932 2 days ago
add comment

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:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
#define SOLVE ifs
#define IT >>
#define YOURSELF ofs

ifstream& operator IT(ifstream& ifs, ofstream& ofs) {
  string s;
  SOLVE IT s;
  YOURSELF << s;
  return ifs;
}

void output(ofstream& ofs, ifstream& ifs) {
  while (ifs) {
    SOLVE IT YOURSELF;
    ofs << ' ';
  }
}

int main() try {

  ofstream ofs("out.txt");
  ifstream ifs("in.txt");

  output(ofs, ifs);

  return 0;
}
catch (exception& e) {
  cerr << "Error: " << e.what() << endl;
  return 1;
}
catch (...) {
  cerr << "Unknown error.\n";
  return 2;
}
share|improve this answer
3  
-1 (not really), not enough JQuery. Also too much error checking. I think you even closed the file! Gawd. –  Shingetsu 2 days ago
 
I could have known JQuery was coming up! –  user2509848 yesterday
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.