SIO

NAME
BUGS

NAME

Create, Open, Append, Close, Get, Put, GetLine, PutLine: Simple input/output package

This set of Go routines constitutes simple input and output routines to binary and text files.

File handling:

func Create(filename string) (fileptr *os.File, err error)

Create a new file "filename" for writing. The file pointer "fileptr" is returned along with the error flag "err". The "os" package file handling routines are used to accomplish this. In the special case in which the file name is "stdout" or "stderr", no new file pointer is returned. Instead, the pre-defined standard output or standard error file pointers are returned.

func Open(filename string) (fileptr *os.File, err error)

As in Create except an existing file is opened for reading. If the file name is "stdin", the pre-defined standard input pointer is returned.

func Append(filename string) (fileptr *os.File, err error)

As in Append except an existing file is opened for appending. If the file doesn’t exist, a new file with the specified name is created for writing. The filename should not point to a pipe, e.g., "stdout" or "stderr".

func Close(fileptr *os.File) (err error)

This routine closes the file represented by the file pointer "fileptr".

Binary reading and writing:

func Get(fileptr *os.File, nbytes int) (inbuff []byte, err error)

This routine reads up to "nbytes" bytes from the previously opened file associated with "fileptr". Less than "nbytes" are read if fewer remain to be read. The results are returned in a byte slice whose length equals that of the number of bytes read. If "nbytes" is less than or equal to zero, the entire file is read. An error is raised on an end of file.

func Put(fileptr *os.File, b []byte) (err error)

This routine writes the bytes in byte slice "b" to the open file represented by "fileptr". An error is returned if something goes wrong.

Text reading and writing:

func GetLine(fileptr *os.File) (s string, err error)

This routine returns a line of text from the previously opened file. The read is terminated when it encounters a newline character. This is the normal line separator for Linux. If a carriage return is encountered (as occurs before the newline in Windows text files), it is silently discarded. The string containing the text does not include the newline. If an end of file is encountered before a newline, an error is returned along with the text received up to that point.

func PutLine(fileptr *os.File, s string) (err error)

This routine writes a line of text represented by string "s" (without a terminating newline) to the previously opened file. An error is returned if a problem is encountered.

BUGS

If a text file contains a carriage return character unassociated with a Windows end-of-line, it is lost.