Create a file: Difference between revisions
(→[[File_Creation#ALGOL 68]]: fix some obvious syntax errors) |
|||
Line 5: | Line 5: | ||
=={{header|ALGOL 68}}== |
=={{header|ALGOL 68}}== |
||
Note: file names are Operating System dependent. |
Note: file names are Operating System dependent. |
||
* [[ALGOL 68G]] does not support pages, and ''"set"'' procedure only has 2 arguments. |
|||
* [[ELLA ALGOL 68]] also encounters problems with ''"set"'' page on [[linux]]. |
|||
{{works with|ALGOL 68|Standard - no extensions to language used}} |
|||
It may be best to to use an operating system provided library. |
It may be best to to use an operating system provided library. |
||
<pre> |
<pre> |
||
main:( |
main:( |
||
⚫ | |||
INT errno; |
|||
⚫ | |||
BEGIN |
BEGIN |
||
FILE actual file; |
FILE actual file; |
||
INT errno = open(actual file, file name, stand out channel); |
INT errno := open(actual file, file name, stand out channel); |
||
IF errno NE 0 THEN stop touch FI; |
IF errno NE 0 THEN GO TO stop touch FI; |
||
close(actual file); # detach the book and keep it # |
close(actual file); # detach the book and keep it # |
||
errno |
errno |
||
Line 21: | Line 28: | ||
END; |
END; |
||
touch("input.txt"); |
errno := touch("input.txt"); |
||
touch("/input.txt"); |
errno := touch("/input.txt"); |
||
# ALGOL 68 has no concept of directories, |
# ALGOL 68 has no concept of directories, |
||
however a file can have multiple pages, |
however a file can have multiple pages, |
||
the pages are identified by page number only |
the pages are identified by page number only # |
||
PROC mkpage(STRING file name, INT page x)INT: |
PROC mkpage = (STRING file name, INT page x)INT: |
||
BEGIN |
BEGIN |
||
FILE actual file; |
FILE actual file; |
||
INT errno = open(actual file, file name, stand out channel); |
INT errno := open(actual file, file name, stand out channel); |
||
IF errno NE 0 THEN stop |
IF errno NE 0 THEN GO TO stop mkpage FI; |
||
set(actual file,page x,1,1); # skip to page x, line 1, character 1 # |
set(actual file,page x,1,1); # skip to page x, line 1, character 1 # |
||
close(actual file); # detach the new page and keep it # |
close(actual file); # detach the new page and keep it # |
||
Line 41: | Line 48: | ||
END; |
END; |
||
mkpage("input.txt",2); |
errno := mkpage("input.txt",2); |
||
) |
) |
||
</pre> |
</pre> |
Revision as of 12:22, 14 January 2009
You are encouraged to solve this task according to the task description, using any language you may know.
In this task, the job is to create a new empty file called "output.txt" of size 0 byte and an empty directory called "docs". This should be done twice: once "here", i.e. in the current working directory and once in the filesystem root.
ALGOL 68
Note: file names are Operating System dependent.
- ALGOL 68G does not support pages, and "set" procedure only has 2 arguments.
- ELLA ALGOL 68 also encounters problems with "set" page on linux.
It may be best to to use an operating system provided library.
main:( INT errno; PROC touch = (STRING file name)INT: BEGIN FILE actual file; INT errno := open(actual file, file name, stand out channel); IF errno NE 0 THEN GO TO stop touch FI; close(actual file); # detach the book and keep it # errno EXIT stop touch: errno END; errno := touch("input.txt"); errno := touch("/input.txt"); # ALGOL 68 has no concept of directories, however a file can have multiple pages, the pages are identified by page number only # PROC mkpage = (STRING file name, INT page x)INT: BEGIN FILE actual file; INT errno := open(actual file, file name, stand out channel); IF errno NE 0 THEN GO TO stop mkpage FI; set(actual file,page x,1,1); # skip to page x, line 1, character 1 # close(actual file); # detach the new page and keep it # errno EXIT stop mkpage: errno END; errno := mkpage("input.txt",2); )
D
For file creation, std.file.write function & std.stream.file class are used.
For dir creation, std.file.mkdir is used.
module fileio ; import std.stdio ; import std.path ; import std.file ; import std.stream ; string[] genName(string name){ string cwd = curdir ~ sep ; // on current directory string root = sep ; // on root name = std.path.getBaseName(name) ; return [cwd ~ name, root ~ name] ; } void Remove(string target){ if(exists(target)){ if (isfile(target)) std.file.remove(target); else std.file.rmdir(target) ; } } void testCreate(string filename, string dirname){ // files: foreach(fn ; genName(filename)) try{ writefln("file to be created : %s", fn) ; std.file.write(fn, cast(void[])null) ; writefln("\tsuccess by std.file.write") ; Remove(fn) ; (new std.stream.File(fn, FileMode.OutNew)).close() ; writefln("\tsuccess by std.stream") ; Remove(fn) ; } catch(Exception e) { writefln(e.msg) ; } // dirs: foreach(dn ; genName(dirname)) try{ writefln("dir to be created : %s", dn) ; std.file.mkdir(dn) ; writefln("\tsuccess by std.file.mkdir") ; Remove(dn) ; } catch(Exception e) { writefln(e.msg) ; } } void main(){ writefln("== test: File & Dir Creation ==") ; testCreate("output.txt", "docs") ; }
DOS Batch File
md docs md \docs
Forth
There is no means to create directories in ANS Forth.
s" output.txt" w/o create-file throw ( fileid) drop s" /output.txt" w/o create-file throw ( fileid) drop
Fortran
Don't know a way of creating directories in Fortran
OPEN (UNIT=5, FILE="output.txt", STATUS="NEW") ! Current directory CLOSE (UNIT=5) OPEN (UNIT=5, FILE="/output.txt", STATUS="NEW") ! Root directory CLOSE (UNIT=5)
Haskell
import System.IO import System.Directory createFile name = do h <- openFile name WriteMode hClose h main = do createFile "output.txt" createDirectory "docs" createFile "/output.txt" createDirectory "/docs"
Java
import java.util.File; public class CreateFileTest { public static String createNewFile(String filename) { try { // Create file if it does not exist boolean success = new File(filename).createNewFile(); if (success) { return " did not exist and was created successfully."; } else { return " already exists."; } } catch (IOException e) { return " could not be created."; } } public static void test(String type, String filename) { System.out.println("The following " + type + " called " + filename + createNewFile(filename) ); } public static void main(String args[]) { test("file", "output.txt"); test("file", File.seperator + "output.txt"); test("directory", "docs"); test("directory", File.seperator + "docs" + File.seperator); } }
MAXScript
-- Here f = createFile "output.txt" close f makeDir (sysInfo.currentDir + "\docs") -- System root f = createFile "\output.txt" close f makeDir ("c:\docs")
OCaml
<ocaml># let oc = open_out "output.txt" in
close_out oc;;
- : unit = ()
- Unix.mkdir "docs" 0o750 ;; (* rigths 0o750 for rwxr-x--- *)
- : unit = ()</ocaml>
(for creation in the filesystem root, replace the filenames by "/output.txt" and "/docs")
Perl
use File::Spec::Functions qw(catfile rootdir); { # here open my $fh, '>', 'output.txt'; mkdir 'docs'; }; { # root dir open my $fh, '>', catfile rootdir, 'output.txt'; mkdir catfile rootdir, 'docs'; };
Without Perl Modules
Current directory
perl -e 'qx(touch output.txt)' perl -e 'mkdir docs'
Root directory
perl -e 'qx(touch /output.txt)' perl -e 'mkdir "/docs"'
Python
Current directory
<python>
import os f = open("output.txt", "w") f.close() os.mkdir("docs")
</python>
Root directory
<python>
f = open("/output.txt", "w") f.close() os.mkdir("/docs")
</python>
Exception-safe way to create file:
<python>
from __future__ import with_statement import os def create(dir): with open(os.path.join(dir, "output.txt"), "w"): pass os.mkdir(os.path.join(dir, "docs")) create(".") # current directory create("/") # root directory
</python>
Raven
"" as str str 'output.txt' write str '/output.txt' write 'docs' mkdir '/docs' mkdir
Smalltalk
Squeak has no notion of 'current directory' because it isn't tied to the shell that created it.
(FileDirectory on: 'c:\') newFileNamed: 'output.txt'; createDirectory: 'docs'.
In GNU Smalltalk you can do instead:
ws := (File name: 'output.txt') writeStream. ws close. Directory create: 'docs'. ws := (File name: '/output.txt') writeStream. ws close. Directory create: '/docs'.
Tcl
Assuming that we're supposed to create two files and two directories (one each here and one each in the file system root) and further assuming that the code is supposed to be portable, i.e. work on win, linux, MacOS (the task is really not clear):
close [open output.txt w] close [open [file nativename /output.txt] w] file mkdir docs file mkdir [file nativename /docs]
Toka
needs shell " output.txt" "W" file.open file.close " /output.txt" "W" file.open file.close ( Create the directories with permissions set to 777) " docs" &777 mkdir " /docs" &777 mkdir
Visual Basic .NET
Platform: .NET
'Current Directory IO.Directory.CreateDirectory("docs") IO.File.Create("output.txt").Close() 'Root IO.Directory.CreateDirectory("\docs") IO.File.Create("\output.txt").Close() 'Root, platform independent IO.Directory.CreateDirectory(IO.Path.DirectorySeparatorChar & "docs") IO.File.Create(IO.Path.DirectorySeparatorChar & "output.txt").Close()
UNIX Shell
touch output.txt touch /output.txt mkdir docs mkdir /docs