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 code-golf, shortest answer wins!
-
1\$\begingroup\$ 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. \$\endgroup\$Martin Ender– Martin Ender2017年04月20日 08:20:29 +00:00Commented Apr 20, 2017 at 8:20
-
\$\begingroup\$ @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). \$\endgroup\$Stewie Griffin– Stewie Griffin2017年04月20日 08:35:31 +00:00Commented Apr 20, 2017 at 8:35
-
\$\begingroup\$ @LuisMendo Probably fixed \$\endgroup\$Thunda– Thunda2017年04月20日 11:21:58 +00:00Commented Apr 20, 2017 at 11:21
-
2\$\begingroup\$ @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. \$\endgroup\$2501– 25012017年04月20日 15:17:01 +00:00Commented Apr 20, 2017 at 15:17
-
1\$\begingroup\$ @Notts90 Yes, as long as it works. Hell, put it in /bin/ if you like. \$\endgroup\$Thunda– Thunda2017年04月21日 04:26:36 +00:00Commented Apr 21, 2017 at 4:26
10 Answers 10
Zsh, 3 bytes
>0ドル
Truncates the script to length 0.
Unlike its Bash counterpart, this handles filenames that contain whitespace.
C, (削除) 35 (削除ここまで) 25 bytes
f(){fopen(__FILE__,"w");}
Just empties the source file.
-
\$\begingroup\$
but you can't delete the file itself.
Clarification: the "file" in question is the source file. \$\endgroup\$Thunda– Thunda2017年04月20日 09:59:37 +00:00Commented Apr 20, 2017 at 9:59 -
4\$\begingroup\$ @Thunda source file doesn't get deleted, it just gets emptied so it can't be compiled again. \$\endgroup\$betseg– betseg2017年04月20日 10:02:35 +00:00Commented Apr 20, 2017 at 10:02
-
\$\begingroup\$ I'd correct the "erase" term anyway. \$\endgroup\$KeyWeeUsr– KeyWeeUsr2017年04月20日 11:06:20 +00:00Commented Apr 20, 2017 at 11:06
-
\$\begingroup\$ Erase was a bit ambiguous there \$\endgroup\$Thunda– Thunda2017年04月20日 11:20:33 +00:00Commented Apr 20, 2017 at 11:20
-
\$\begingroup\$ I agree - I wasn't quite awake. \$\endgroup\$betseg– betseg2017年04月20日 11:21:44 +00:00Commented Apr 20, 2017 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.
-
1\$\begingroup\$ 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. \$\endgroup\$Erik the Outgolfer– Erik the Outgolfer2017年04月20日 08:33:49 +00:00Commented Apr 20, 2017 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
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.
-
\$\begingroup\$
.
is shorter thanFC
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. \$\endgroup\$Neil– Neil2017年04月20日 12:17:41 +00:00Commented Apr 20, 2017 at 12:17 -
\$\begingroup\$ @Neil Indeed, thanks. I took another look and this might be the limit for Batch. \$\endgroup\$Engineer Toast– Engineer Toast2017年04月20日 12:50:41 +00:00Commented Apr 20, 2017 at 12:50
Node.js, 47 bytes
require('fs').writeFileSync(process.argv[1],'')
Erase the content of the source file