7
\$\begingroup\$

This question already has an answer here:

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!

\$\endgroup\$

marked as duplicate by Bassdrop Cumberwubwubwub, Timtech, AdmBorkBork code-golf Apr 20 '17 at 13:04

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 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 Apr 20 '17 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 Apr 20 '17 at 8:35
  • \$\begingroup\$ @LuisMendo Probably fixed \$\endgroup\$ – Thunda Apr 20 '17 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 Apr 20 '17 at 15:17
  • 1
    \$\begingroup\$ @Notts90 Yes, as long as it works. Hell, put it in /bin/ if you like. \$\endgroup\$ – Thunda Apr 21 '17 at 4:26

10 Answers 10

10
\$\begingroup\$

Zsh, 3 bytes

>$0

Truncates the script to length 0.

Unlike its Bash counterpart, this handles filenames that contain whitespace.

Try it online!

\$\endgroup\$
7
\$\begingroup\$

C, 35 25 bytes

f(){fopen(__FILE__,"w");}

Just empties the source file.

Online test suite on TIO

\$\endgroup\$
  • \$\begingroup\$ but you can't delete the file itself. Clarification: the "file" in question is the source file. \$\endgroup\$ – Thunda Apr 20 '17 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 Apr 20 '17 at 10:02
  • \$\begingroup\$ I'd correct the "erase" term anyway. \$\endgroup\$ – KeyWeeUsr Apr 20 '17 at 11:06
  • \$\begingroup\$ Erase was a bit ambiguous there \$\endgroup\$ – Thunda Apr 20 '17 at 11:20
  • \$\begingroup\$ I agree - I wasn't quite awake. \$\endgroup\$ – betseg Apr 20 '17 at 11:21
5
\$\begingroup\$

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.

\$\endgroup\$
2
\$\begingroup\$

Python, 18 17 bytes

open(input(),"w")

-1 thanks to @EriktheOutgolfer

Opens itself in write mode, deleting everything in the file.

\$\endgroup\$
  • 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 Apr 20 '17 at 8:33
2
\$\begingroup\$

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.

\$\endgroup\$
2
\$\begingroup\$

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

\$\endgroup\$
1
\$\begingroup\$

Ruby, 10 bytes

open $0,?w

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

\$\endgroup\$
1
\$\begingroup\$

C, 27 bytes

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

Overwrites the contents of the file wtih the current directory.

\$\endgroup\$
0
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ . 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. \$\endgroup\$ – Neil Apr 20 '17 at 12:17
  • \$\begingroup\$ @Neil Indeed, thanks. I took another look and this might be the limit for Batch. \$\endgroup\$ – Engineer Toast Apr 20 '17 at 12:50
0
\$\begingroup\$

Node.js, 47 bytes

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

Erase the content of the source file

\$\endgroup\$

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