Challenge
Create a new file and write the string Hello World to it.
Restrictions
Your challenge must write to a file on disk, in the file system.
The file may not be a log file generated during normal operation of the interpreter.
The file must contain only the string
Hello World. It is allowed to contain a trailing newline or minimal whitespace. No other content.No command-line flags/pipes (etc) allowed, except when necessary to run the program. (e.g.
perl -p)
Notes
This is code-golf, so shortest program in bytes wins.
Follow the spirit, not the letter, of the rules.
-
\$\begingroup\$ Is trailing newline okay? \$\endgroup\$Winny– Winny2016年08月01日 09:03:23 +00:00Commented Aug 1, 2016 at 9:03
-
\$\begingroup\$ @Winny yes, it is ok \$\endgroup\$anna328p– anna328p2016年08月01日 20:23:21 +00:00Commented Aug 1, 2016 at 20:23
-
\$\begingroup\$ Is a filename part of the contents of a file? \$\endgroup\$CousinCocaine– CousinCocaine2016年08月12日 20:36:54 +00:00Commented Aug 12, 2016 at 20:36
-
\$\begingroup\$ Could I also write to a magnetic tape, or must it be a disk? If so, are you including 3.5" and 5.25" disks? Or do you mean a "hard disk" or solid state disk? \$\endgroup\$Shaun Bebbers– Shaun Bebbers2021年11月26日 16:06:35 +00:00Commented Nov 26, 2021 at 16:06
-
\$\begingroup\$ @ShaunBebbers Those are acceptable too \$\endgroup\$anna328p– anna328p2021年11月27日 07:00:01 +00:00Commented Nov 27, 2021 at 7:00
53 Answers 53
-
1\$\begingroup\$ Woah. Explanation? \$\endgroup\$DJMcMayhem– DJMcMayhem2016年08月01日 06:22:49 +00:00Commented Aug 1, 2016 at 6:22
-
1\$\begingroup\$ @DrGreenEggsandIronMan it looks like the main trick is how
*can split an iterable into individual arguments in a function, soopen(*"ww")becomesopen("w","w"). \$\endgroup\$Value Ink– Value Ink2016年08月01日 09:09:27 +00:00Commented Aug 1, 2016 at 9:09 -
7\$\begingroup\$ @ValueInk Actually the main trick is the use of the extended print statement which is rarely seen in the wild. \$\endgroup\$xsot– xsot2016年08月01日 10:20:37 +00:00Commented Aug 1, 2016 at 10:20
-
1\$\begingroup\$ That is indeed a cool trick, but if it weren't for your
*"ww"trick you'd've tied with the other Python answer here, which usesopen(...).write(...)instead for the same byte count \$\endgroup\$Value Ink– Value Ink2016年08月01日 10:23:33 +00:00Commented Aug 1, 2016 at 10:23 -
\$\begingroup\$ Oops, I didn't even realise that. \$\endgroup\$xsot– xsot2016年08月01日 10:26:55 +00:00Commented Aug 1, 2016 at 10:26
Haskell, 25 bytes
writeFile"o""Hello World"
zsh, 17 bytes
<<<Hello\ World>x
Outputs to a file called x.
-
\$\begingroup\$ Can you interpreted the
>as a pipe? \$\endgroup\$CousinCocaine– CousinCocaine2016年08月12日 20:34:00 +00:00Commented Aug 12, 2016 at 20:34 -
\$\begingroup\$ What about
>Hello\ World\$\endgroup\$CousinCocaine– CousinCocaine2016年08月12日 20:41:40 +00:00Commented Aug 12, 2016 at 20:41
Vyxal 2.5.3, 32 bytes
kh→@|;@+open(*"ww").write(VAR_)#
Vyxal can't really do anything with external files, which would normally render this challenge impossible. However, in Vyxal 2.5.3 and prior, there was an ACE exploit allowing for arbitrary Python execution. Yep, I found another one.
The ACE:
Vyxal is a transpiled language, so every Vyxal command is translated to some Python code, and then all of the translated commands are executed. Previously, I found that you could escape a string to insert any Python commands you wanted into the transpiled code. It turns out that you can do something similar with function names.
When defining a function in Vyxal, the resulting python looks something like this:
def FN_func(parameter_stack, arity=None):
...setup stuff...
...commands...
...finishing stuff...
return stack
When calling a function in Vyxal, the resulting Python code looks something like this:
stack += FN_func(stack)
The thing is, when parsing the name of a function, the strategy was to just read characters until reaching a :, |, or ;, which are the delimiters of different parts of the function declaration/call. This meant that you could put any characters you wanted into the name, and it would attempt to transpile it the same way.
In this case, I used a + in the function name when I called it. That results in this transpiled code:
stack += FN_+open(*"ww").write(VAR_)#(stack)
This code will error when ran, because it tries to add together a function and the None that is returned from the open command. However, even though it errors, the open command still runs, meaning that we have executed arbitrary Python. If you wanted to write longer sections of Python code, you could replace the + with ([]) and add a newline after it.
The program:
In this program, the payload (the arbitrary Python code) is the following:
open(*"ww").write(VAR_)
The basis of this code is the open().write() command, which creates a file named w if it doesn't exist, then writes the contents of VAR_ to it.
The VAR_ variable is set at the beginning of the Vyxal program with kh→. This pushes the builtin Hello World and saves it to the nameless variable. All variable names are prepended with VAR_ internally, so the variable VAR_ is created and contains Hello World, which is written to the newly created file. Even though this has the overhead of the ACE setup, it ends up being shorter than the Python solutions due to the builtin.
This ACE has not been fixed in the repo, but it had a patch applied server-side for the online interpreter. It was also fixed in the rewrite and subsequent 2.6 release. If you want to try out this program for yourself, you can download the 2.5.3 version here.
Ruby, 26 bytes
Writes to file f.
open(?f,?w)<<"Hello World"
Batch, 18 bytes
echo Hello World>f
-
\$\begingroup\$ I think you need an
@, or re-title this as (console). \$\endgroup\$Erik the Outgolfer– Erik the Outgolfer2016年10月02日 18:13:33 +00:00Commented Oct 2, 2016 at 18:13 -
\$\begingroup\$ The
@is not necessary. \$\endgroup\$Shaun Wild– Shaun Wild2016年10月03日 08:07:34 +00:00Commented Oct 3, 2016 at 8:07 -
\$\begingroup\$ Try saving it in a batch file and run it as such (not directly type the command). You will then see that you need the
@, because the command is otherwise printed too. That means you have two options: 1) Delete this answer because it will be a dupe 2) Relabel as (console). \$\endgroup\$Erik the Outgolfer– Erik the Outgolfer2016年10月03日 10:35:55 +00:00Commented Oct 3, 2016 at 10:35
Batch, 19 bytes
@echo Hello World>o
-
\$\begingroup\$ Not sure if it counts as wrong, but it outputs "Hello World" and a newline to the file, rather than simply "Hello World" \$\endgroup\$brianush1– brianush12016年08月01日 12:18:59 +00:00Commented Aug 1, 2016 at 12:18
-
\$\begingroup\$ What is the '@' good for? \$\endgroup\$PEAR– PEAR2016年08月01日 13:03:12 +00:00Commented Aug 1, 2016 at 13:03
-
\$\begingroup\$ @PEAR It prevents the command from being echoed to STDOUT. \$\endgroup\$Neil– Neil2016年08月01日 13:18:43 +00:00Commented Aug 1, 2016 at 13:18
-
\$\begingroup\$ @brianush1 It doesn't count as wrong, apparently. \$\endgroup\$Erik the Outgolfer– Erik the Outgolfer2016年10月02日 18:14:09 +00:00Commented Oct 2, 2016 at 18:14
Vim, 15 + 2 == 17 bytes
iHello World<esc>ZZ
+2 bytes for launching this with vim f instead of vim. Additionally, this version works to:
iHello World<C-o>ZZ
If launching vim like this is not allowed, there is also:
Vim, 18 bytes
iHello World<esc>:w f<cr>
Side note: this is a polyglot. The same thing works in V, except that it is one byte shorter (since the <cr> at the end is implicit.)
-
\$\begingroup\$ What's the ` f<cr>` at the end for? \$\endgroup\$Zwei– Zwei2016年08月01日 06:05:31 +00:00Commented Aug 1, 2016 at 6:05
-
\$\begingroup\$ @Zwei 'f' is the name of the file, and the
<cr>(which is a mnemonic for "enter", a single byte) is necessary to run the command. Commands that start with a colon are like a mini shell inside of vim, and the command:wis the command for writing to a file. \$\endgroup\$DJMcMayhem– DJMcMayhem2016年08月01日 06:08:47 +00:00Commented Aug 1, 2016 at 6:08 -
\$\begingroup\$ Should
<esc>not be counted as 5 bytes and<cr>as 4, since they have to be fully typed in? \$\endgroup\$Bart van Nierop– Bart van Nierop2016年08月03日 05:38:18 +00:00Commented Aug 3, 2016 at 5:38 -
\$\begingroup\$ @BartvanNierop No,
<esc>is just notation for "The escape key", which is0x1B, and<cr>is notation for "The Carriage Return key" which is0x0B\$\endgroup\$DJMcMayhem– DJMcMayhem2016年08月03日 05:39:42 +00:00Commented Aug 3, 2016 at 5:39 -
1\$\begingroup\$ Well first off, there is a precedent to score vim with keystrokes == bytes, but second off, there are several ways to write this in vim "code" without using the vim-key notation. For example, if you use
<C-v>to insert a literal escape character and a literal carriage return, then you can assign all of these strokes to a macro, and run it that way. You could also do it in vimscript withnormal 'iHello World^[:w f^Mwhich is how vim displays it, not how you enter it.^[and^Mare both one byte. \$\endgroup\$DJMcMayhem– DJMcMayhem2016年08月03日 05:50:39 +00:00Commented Aug 3, 2016 at 5:50
C, 44 bytes
main(){fputs("Hello World",fopen("o","w"));}
-
1\$\begingroup\$ This will segfault on some systems without
#include <stdio.h>\$\endgroup\$Zombo– Zombo2016年08月01日 12:23:40 +00:00Commented Aug 1, 2016 at 12:23 -
\$\begingroup\$ @StevenPenny As long as there's a system/compiler where it's guaranteed not to segfault, that's fine. Answers generally don't have to be portable. \$\endgroup\$Martin Ender– Martin Ender2016年10月02日 20:57:41 +00:00Commented Oct 2, 2016 at 20:57
-
\$\begingroup\$ @StevenPenny And now it has. \$\endgroup\$orlp– orlp2016年10月03日 05:54:05 +00:00Commented Oct 3, 2016 at 5:54
PowerShell, 15 bytes
"Hello World">o
> redirects the string to a file called o in the current directory.
Node.js, 42 bytes
require("fs").writeFile('o','Hello World')
i don't think this needs explanation
Node.js REPL, 31 bytes
fs.writeFile('o','Hello World')
for some reason in repl you dont need to include fs
-
\$\begingroup\$ I don't think this works. In order for it to work, you'd need
require("fs").writeFile("o","Hello World"). Otherwise, fs is not included. \$\endgroup\$Conor O'Brien– Conor O'Brien2016年08月01日 19:45:37 +00:00Commented Aug 1, 2016 at 19:45 -
\$\begingroup\$ @CᴏɴᴏʀO'Bʀɪᴇɴ huh ok. works in REPL for some reason \$\endgroup\$Downgoat– Downgoat2016年08月01日 19:49:05 +00:00Commented Aug 1, 2016 at 19:49
-
\$\begingroup\$ I beat you with 1 byte :P \$\endgroup\$Alex bries– Alex bries2021年11月27日 15:40:16 +00:00Commented Nov 27, 2021 at 15:40
Bash, 18 bytes
echo Hello World>a
Pyth, 14 bytes
.w"Hello World
Outputs to a file called o.txt.
ed, 19 characters
i
Hello World
.
w o
Sample run:
bash-4.3$ ed <<< $'i\nHello World\n.\nw o'
12
bash-4.3$ cat o
Hello World
K, 20 Bytes
`:f 0:,"Hello World"
`:f
Confirmation;
mmm@chromozorz:~/q$ cat f.txt
Hello World
-
\$\begingroup\$ Since nobody said it yet, welcome to Programming Puzzles & Code Golf. \$\endgroup\$Erik the Outgolfer– Erik the Outgolfer2016年10月02日 18:19:03 +00:00Commented Oct 2, 2016 at 18:19
-
\$\begingroup\$ Much appreciated! \$\endgroup\$Chromozorz– Chromozorz2016年10月05日 19:54:02 +00:00Commented Oct 5, 2016 at 19:54
-
\$\begingroup\$ OK. I also think that you have extra leading spaces though. I will edit the spaces out if you want (I don't know if they are intentionally placed, but I think it was unintentional.) \$\endgroup\$Erik the Outgolfer– Erik the Outgolfer2016年10月06日 05:22:08 +00:00Commented Oct 6, 2016 at 5:22
Clojure, 23 bytes
#(spit"x""Hello World")
Anonymous function which creates file called x and writes Hello World there.
C#, (削除) 93 (削除ここまで) (削除) 77 (削除ここまで) 76 bytes
(削除) using System.IO;namespace N{class C{static void M(){File.WriteAllText("f", "Hello World");}}} (削除ここまで)
(削除) class C{static void Main(){System.IO.File.WriteAllText("f", "Hello World");}} (削除ここまで)
class C{static void Main(){System.IO.File.WriteAllText("f","Hello World");}}
See it work, with an exception for unauthorized file access.
Changelog
Rev2
- Removed unnecessary namespace
- Changed function name to Main (because otherwise it won't be detected as main function)
- Removed
usingdirective (thanks Jean Lourenço)
Rev3
- Removed space that sneaked in.
C# (without boilerplate), 47 bytes
void M(){File.WriteAllText("f","Hello World");}
-
\$\begingroup\$ You can save some bytes by removing the using and appending it directly to the method: System.IO.File.WriteAllText[...] \$\endgroup\$Jean Lourenço– Jean Lourenço2016年08月02日 11:26:28 +00:00Commented Aug 2, 2016 at 11:26
-
\$\begingroup\$ @JeanLourenço Thanks. I had that originally and then changed it for reasons unknown. \$\endgroup\$Bart van Nierop– Bart van Nierop2016年08月02日 14:15:11 +00:00Commented Aug 2, 2016 at 14:15
R, (削除) 38 (削除ここまで) (削除) 36 (削除ここまで) 35 bytes
sink(" ");cat("Hello World");sink()
I like how the created file has no name ! It's just (削除) anything, in fact !.txt (削除ここまで)
-2 bytes thanks to @PEAR remark !
-1 bytes thanks to @BartvanNierop !
This code will produce a file with no name.
-
1\$\begingroup\$ Is the '.txt' really necessary? File endings are just for the user. A single character might me enough. \$\endgroup\$PEAR– PEAR2016年08月01日 13:02:21 +00:00Commented Aug 1, 2016 at 13:02
-
1\$\begingroup\$ I don't know R, but could you not shave off another byte, as by @PEAR's suggestion, by simply naming the file
"a"? \$\endgroup\$Bart van Nierop– Bart van Nierop2016年08月03日 05:35:29 +00:00Commented Aug 3, 2016 at 5:35
Python, 34 bytes
open("h","w").write("Hello World")
Outputs to a file called h.
Gema, 28 characters
\A=@write{o;Hello World}@end
Sample run:
bash-4.3$ gema '\A=@write{o;Hello World}@end'
bash-4.3$ cat o
Hello World
Racket, 43 bytes
(display"Hello World"(open-output-file"f"))
Julia, 47 bytes
f=open("o","w");write(f,"Hello World");close(f)
I tried using writedlm, but it didn't work out.
C, 37 bytes
main(){system("echo Hello World>o");}
Perl 6, (削除) 27 (削除ここまで) 23 bytes
(削除) 'o'.IO.spurt: 'Hello World' (削除ここまで)
spurt 'o','Hello World'
Java 7, (削除) 100 (削除ここまで) 95 bytes
void f()throws Exception{java.io.Writer p=new java.io.PrintWriter("x");p.print("Hello World");}
Or if you want to close the writer after using it (101 bytes):
void f()throws Exception{try(java.io.Writer p=new java.io.PrintWriter("x")){p.print("Hello World");}}
Ungolfed:
class Main{
static void f() throws Exception{
try(java.io.Writer p = new java.io.PrintWriter("x")){
p.print("Hello World");
}
}
public static void main(String[] a){
try{
f();
} catch(Exception ex){
}
}
}
Usage:
java -jar Main.jar
-
\$\begingroup\$ Java is my favorite language but jesus christ it's hilarious how bad a golfing language it is haha \$\endgroup\$Shaun Wild– Shaun Wild2016年08月01日 11:44:23 +00:00Commented Aug 1, 2016 at 11:44
-
4\$\begingroup\$ @AlanTuning Indeed. xD I work with Java at work, and it's fun to code-golf in Java. You will NEVER be able to win a code-golf challenge here using Java, but it's still fun to write the Java code as short as possible. \$\endgroup\$Kevin Cruijssen– Kevin Cruijssen2016年08月01日 12:02:13 +00:00Commented Aug 1, 2016 at 12:02
-
2\$\begingroup\$ well, you can just
void f()throws Exception{new java.io.PrintWriter("x").print("Hello World");}\$\endgroup\$cliffroot– cliffroot2016年08月01日 15:13:04 +00:00Commented Aug 1, 2016 at 15:13 -
\$\begingroup\$
java.io.Writer p=new java.io.PrintWritersaves you some more characters. \$\endgroup\$Frozn– Frozn2016年08月01日 18:39:36 +00:00Commented Aug 1, 2016 at 18:39 -
\$\begingroup\$ @cliffroot Hmm, I tried that initially, but it didn't seem to write anything to disk. Will try again tomorrow, currently I don't have an IDE and file I/O doesn't work in ideone. \$\endgroup\$Kevin Cruijssen– Kevin Cruijssen2016年08月01日 19:26:55 +00:00Commented Aug 1, 2016 at 19:26
eacal, 26 bytes
write a string Hello World
This writes a string Hello World to file a, creating it if not present. Basically:
write <fileName> <exec>
and
string <params to convert to string>
Run the program as:
λ node eacal.js writeFile
J, 21 bytes
'Hello World'1!:3<'o'
This writes to a file o in the current directory, or, if not called from a file, in your j64-804 file. 1!:3 is the write foreign, and <'o' is the boxed filename (filenames need to be boxed). The LHS is the string to write.
Explore related questions
See similar questions with these tags.