# Challenge

Create a program that, when run, changes the contents of the source file. This means that you can delete/add/change characters in the source file, but you can't delete the (source) file itself (deletion is when the source file does not exist after the program is run).

In the case of pre-compiled languages such as C++, your program can change the source file only, such that when compiled, either fails or does not produce the same result.

Your program must not return any errors, such as access violations (e.g. opening a file that is already open).

• This is an advantage to pre-compiled languages, as the source file does not have to be in use to run it.

## Input

You may take an optional path to the source file, but nothing more.

## Output

You may output anything.

## Scoring

This is , shortest answer wins!

• The title and the clarification for compiled languages suggest that the modification should stop the program from working afterwards, but the actual rules in your first paragraph make no mention of that. – Martin Ender Apr 20 '17 at 8:20
• @MartinEnder, the second paragraph suggests that the program must fail or the output must be different (which can be considered breaking it, since it no longer produces the output it used to). – Stewie Griffin Apr 20 '17 at 8:35
• @LuisMendo Probably fixed – Thunda Apr 20 '17 at 11:21
• @AdmBorkBork This question is about changing the source file. The dup is about damaging the program itself or the environment. That is not a duplicate. – 2501 Apr 20 '17 at 15:17
• @Notts90 Yes, as long as it works. Hell, put it in /bin/ if you like. – Thunda Apr 21 '17 at 4:26

>$0  Truncates the script to length 0. Unlike its Bash counterpart, this handles filenames that contain whitespace. Try it online! # C, 35 25 bytes f(){fopen(__FILE__,"w");}  Just empties the source file. Online test suite on TIO • but you can't delete the file itself. Clarification: the "file" in question is the source file. – Thunda Apr 20 '17 at 9:59 • @Thunda source file doesn't get deleted, it just gets emptied so it can't be compiled again. – betseg Apr 20 '17 at 10:02 • I'd correct the "erase" term anyway. – KeyWeeUsr Apr 20 '17 at 11:06 • Erase was a bit ambiguous there – Thunda Apr 20 '17 at 11:20 • I agree - I wasn't quite awake. – betseg Apr 20 '17 at 11:21 # Octave, 27 bytes fwrite(fopen('f','w'),'')  Explanation: fopen('f','w') opens the file f (itself) with writing privileges (the file has no file extension (it's not f.m)). fwrite writes an empty string '' to that file, overwriting the content of it. Run it from the GUI / CLI using: run f. PS! Note: You should close the file again afterwards using fclose(3), but this is not necessary for it to work. # Python, 18 17 bytes open(input(),"w")  -1 thanks to @EriktheOutgolfer Opens itself in write mode, deleting everything in the file. • You can use input() getting quoted input (Python 2) or non-quoted input (Python 3) to the file path, because it's allowed by the rules. That saves 1 byte. – Erik the Outgolfer Apr 20 '17 at 8:33 # Jelly, 17 bytes “µ÷ⱮÑċẊṚ⁾Ṁ)ɦGl»ŒV  Never fails, but produces different outputs. Takes relative path through STDIN (unquoted). Explanation: “µ÷ⱮÑċẊṚ⁾Ṁ)ɦGl»ŒV Main link. Arguments: 0 “µ÷ⱮÑċẊṚ⁾Ṁ)ɦGl» Literal "open(input(),'w')" ŒV Python eval and listify  Open a file in write mode in Python overwrites it. # PHP, 18 fopen(__FILE__,w);  From the manual: Open for writing only; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.  I guess that should also work in most other languages # Ruby, 10 bytes open$0,?w


Not very original, almost everybody is doing it the same way nowadays.

## C, 27 bytes

f(){system("cd>"__FILE__);}


Overwrites the contents of the file wtih the current directory.

# Batch, 7 4 bytes

Thanks to Neil for saving 3 bytes

.>%0


> overwrites the contents of a file and %0 returns the current batch file's path.
Result: Blank batch file.

• . is shorter than FC and still seems to work. I like the ~f but I would have thought you're allowed to require that the batch file is invoked including extension. – Neil Apr 20 '17 at 12:17
• @Neil Indeed, thanks. I took another look and this might be the limit for Batch. – Engineer Toast Apr 20 '17 at 12:50

# Node.js, 47 bytes

require('fs').writeFileSync(process.argv[1],'')


Erase the content of the source file