11
\$\begingroup\$

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 , so shortest program in bytes wins.

  • Follow the spirit, not the letter, of the rules.

\$\endgroup\$
  • \$\begingroup\$ Is trailing newline okay? \$\endgroup\$ – Winny Aug 1 '16 at 9:03
  • \$\begingroup\$ @Winny yes, it is ok \$\endgroup\$ – anna328p Aug 1 '16 at 20:23
  • \$\begingroup\$ Is a filename part of the contents of a file? \$\endgroup\$ – CousinCocaine Aug 12 '16 at 20:36

50 Answers 50

1
2
1
\$\begingroup\$

HTML, 83 bytes

This code was tested in Chrome. It relies on the image failing to load, then it clicks itself, triggering the anchor tag which is a download link.

<a href="data:text/plain,Hello World" download><img src onerror="this.click()"></a>
| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

C#, 118 112 98 bytes


Golfed

void M(){var f=new FileInfo("_");var s=f.CreateText();s.Write("Hello World");s.Flush();s.Close();}

Ungolfed

public void M() {
    var f = new FileInfo( "_" );
    var s = f.CreateText();

    s.Write( "Hello World" );
    s.Flush();
    s.Close();
}

Full explicit code

using System.IO;

namespace N {
    class C {
        public void M() {
            // A file name is required
            FileInfo f = new FileInfo( "_" );
            
            // Obtain the Stream to later write on it
            StreamWriter s = f.CreateText();

            // Write 'Hello World' to the Stream
            s.Write( "Hello World" );
            
            // Flush it to the file
            s.Flush();
            
            // Close the Stream, for security and integrity reasons.
            s.Close();
        }
    }
}

Releases

  • v1.2 - -14 bytes - Changed to vars. Shoutout to Jean Lourenço.
  • v1.1 -  -6 bytes - Simplified names.
  • v1.0 - 118 bytes - Initial solution.



I'll leave a version down below that only uses the Console to write on th file. Just because I can.


C# Console Version, 39 bytes


Golfed

void M(){Console.Write("Hello World");}

Ungolfed

public void M() {
    Console.Write("Hello World");
}

Full code

using System;

namespace N {
    class C {
        public void M() {
            // Write 'Hello World' to the output handler
            Console.Write("Hello World");
        }
    }
}

Usage

<Exe. file name>.exe > FileToOutput.txt

Releases

  • v1.0 - 39 bytes - Initial solution.
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ You can use 'var' instead of FileInfo and StreamWriter to save some bytes \$\endgroup\$ – Jean Lourenço Aug 2 '16 at 16:37
  • \$\begingroup\$ Right! Completely forgot the vars thing... Thanks @JeanLourenço \$\endgroup\$ – auhmaan Aug 2 '16 at 16:56
1
\$\begingroup\$

Java

public class H
{
    public static void main(String[] a)
        throws IOException
    {
        FileWriter o = new FileWriter("x");
        o.write("Hello World");
        o.close();
    }
}

Simple I/O with no exception handling.

Corrected

Corrected the original code to write the correct string (Hello World).

| improve this answer | |
\$\endgroup\$
  • 2
    \$\begingroup\$ Your answer would have been shorter if you had actually wrote the correct string.. \$\endgroup\$ – Shaun Wild Aug 2 '16 at 9:11
  • \$\begingroup\$ @SeanBean: Huh, force of habit, I guess, that I wrote Hello, world. I corrected the code. \$\endgroup\$ – David R Tribble Aug 4 '16 at 23:07
  • \$\begingroup\$ @DavidRTribble You can change public class H to class H and IOException to Exception. This will save you 9 bytes. Also, you should include the import java.io.*; \$\endgroup\$ – marcelovca90 Aug 5 '16 at 15:12
1
\$\begingroup\$

Tcl, 28 bytes

puts [open T w] Hello\ World
| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

AutoIT, 43 40 bytes

FileWrite(FileOpen("a",1),"Hello World")


Explanation

FileOpen takes two parameters and returns the handle of the file. Mode must be 1 for writing (appending) text.

FileOpen ( "filename" [, mode = 0] )

FileWrite takes two parameters, a handle and the text to write

FileWrite ( "filehandle/filename", "text/data" )

If a file does not exist, AutoIT will create it. Note that in my example it will not have any file extension associated (filename: a), but can still be opened with any text editor.

AutoIT is a Windows only scripting language.

Script-Example.com has an online AutoIT compiler that can verify the solution.

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

Php, 38 bytes

file_put_contents('a', 'Hello World');

| improve this answer | |
\$\endgroup\$
  • 1
    \$\begingroup\$ You can save 15 bytes by calling the execution operator. `echo Hello\ World > a` \$\endgroup\$ – ʰᵈˑ Aug 1 '16 at 13:26
  • 2
    \$\begingroup\$ Heh heh that's not really PHP anymore. \$\endgroup\$ – Alex Howansky Aug 1 '16 at 16:24
  • 1
    \$\begingroup\$ 36 bytes: fputs(fopen('a','c'),'Hello World'); \$\endgroup\$ – Alex Howansky Aug 1 '16 at 16:28
  • \$\begingroup\$ @AlexHowansky You can do fputs(fopen(a,c),'Hello World'); and save 4 bytes. \$\endgroup\$ – Ismael Miguel Aug 2 '16 at 13:32
0
\$\begingroup\$

Mathematica, 27 bytes

".txt"~Export~"Hello World"
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Like in the answer using R... isn't a single character enough for the file name? \$\endgroup\$ – PEAR Aug 4 '16 at 10:06
  • \$\begingroup\$ @PEAR Mathematica can't infer the file format without a file extension. \$\endgroup\$ – alephalpha Aug 4 '16 at 10:20
  • \$\begingroup\$ Oh, ok... maybe something shorter encoded in the same way?... .c for example? \$\endgroup\$ – PEAR Aug 4 '16 at 10:22
0
\$\begingroup\$

Perl, 20 bytes

`echo Hello World>o`

Perl will execute anything in backticks (`) as a system command.

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

ListSharp, 37 bytes

OUTP="Hello World" HERE[<here>+"\\a"]

simply writes Hello World to a file called "a"(not even a text file) in the script execution environment,

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

PHP, 34 bytes

fputs(fopen(7,'c'),'Hello World');
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ @Lukabot I reverted your edit because it should have been a comment, and it is completely different enough to be its own answer (the same language can be used multiple times if the approach is different) \$\endgroup\$ – NinjaBearMonkey Aug 2 '16 at 2:09
0
\$\begingroup\$

PHP, 27 bytes

exec('echo Hello World>o');

PHP, 20 bytes

`echo Hello World>o`
| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

Nim, 28 26 bytes

"o".writeFile"Hello World"

Yes, Nim has a function in the system module for opening a file, writing a string to it, then closing it again.

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

C#, 50 bytes

s=>System.IO.File.WriteAllText("f","Hello World");

An anonymous function that takes in an unused argument and outputs the file.

Alternatively and cheating slightly this one:

s=>System.IO.File.WriteAllText("f",s);

That takes in a string and works assuming the string contains the text: Hello World, for 38 bytes.

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

Common Lisp, 69 bytes

Lauded for being incredibly expressive, it's not the best golfing language...

(with-open-file (f "f" :direction :output) (format f "Hello, world"))

See it work, with permission denied.

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

Bash script, 20 18 bytes

echo Hello World>o

Save this as a file write.sh and run it: `bash o.sh

| improve this answer | |
\$\endgroup\$
  • 5
    \$\begingroup\$ Why did you repost a deleted answer? \$\endgroup\$ – Dennis Aug 1 '16 at 6:17
  • 2
    \$\begingroup\$ Surely the quotes are unnecessary? \$\endgroup\$ – Neil Aug 1 '16 at 9:19
  • 1
    \$\begingroup\$ You can save a byte by escaping the space instead of using quotes: Hello\ World \$\endgroup\$ – MTCoster Aug 1 '16 at 14:13
  • \$\begingroup\$ @MTCoster, why? help echo says: “Display the ARGs, separated by a single space character and followed by a newline, on the standard output.” \$\endgroup\$ – manatwork Aug 1 '16 at 16:52
  • \$\begingroup\$ @manatwork You're absolutely right, I was unaware of this! \$\endgroup\$ – MTCoster Aug 1 '16 at 16:53
0
\$\begingroup\$

Awk, 29 characters

BEGIN{print"Hello World">"o"}

(No, not another output redirection. Just Awk's syntax is similar.)

Sample run:

bash-4.3$ awk 'BEGIN{print"Hello World">"o"}'

bash-4.3$ cat o
Hello World
| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

Groovy, 28 characters

new File("o")<<"Hello World"

Sample run:

bash-4.3$ groovy -e 'new File("o")<<"Hello World"'

bash-4.3$ cat o
Hello World
| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

Sed, 20 characters

(19 characters code + 1 character dummy input.)

s/^/Hello World/
wo

Seems the side effect of also displaying the string to STDOUT after the file was created is not prohibited.

Requires dummy input to work as per Are languages like sed exempt from “no input” rules?

Sample run:

bash-4.3$ sed $'s/^/Hello World/;wo' <<< ''
Hello World

bash-4.3$ cat o
Hello World
| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

BASH, 13 bytes

When we asume that the filename is part of the contents of a file:

>Hello\ World
| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

QBasic, 39 bytes

OPEN"f"FOR OUTPUT AS#1
?#1,"Hello World

Opens a file named f (creating it if it doesn't exist, wiping it if it does) and gives it the file handle #1. Then prints (?) to file #1 the string Hello World (with a trailing newline).

| improve this answer | |
\$\endgroup\$
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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