D Programming Language 2.0

- Comments
- D
- Search
- Home

Last update Sun Jan 20 11:39:28 2008

# std.stdio

Standard I/O functions that extend std.c.stdio. std.c.stdio is publically imported when importing std.stdio.

class StdioException: object.Exception;
  Thrown if I/O errors happen.
void write(T...)(T args);
  If the first argument args[0] is a FILE*, for each argument arg in args[1..$], format the argument (as per to!(string)(arg)) and write the resulting string to args[0]. If args[0] is not a FILE*, the call is equivalent to write(stdout, args).
  A call without any arguments will fail to compile. In the exceedingly rare case you'd want to print a FILE* to stdout as a hex pointer, write("", myFilePtr) will do the trick.
  In case of an I/O error, throws an StdioException.
void writeln(T...)(T args);
  Equivalent to write(args, '\n'). Calling writeln without arguments is valid and just prints a newline to the standard output.
void writef(T...)(T args);
  If the first argument args[0] is a FILE*, use the format specifier in args[1] to control the formatting of args[2..$], and write the resulting string to args[0]. If arg[0] is not a FILE*, the call is equivalent to writef(stdout, args).
  IMPORTANT:
  New behavior starting with D 2.006: unlike previous versions, writef (and also writefln) only scans its first string argument for format specifiers, but not subsequent string arguments. This decision was made because the old behavior made it unduly hard to simply print string variables that occasionally embedded percent signs.
  Also new starting with 2.006 is support for positional parameters with POSIX syntax.
  Example:

      writef("Date: %2$s %1$s", "October", 5); // "Date: 5 October"

  The positional and non-positional styles can be mixed in the same format string. (POSIX leaves this behavior undefined.) The internal counter for non-positional parameters tracks the next parameter after the largest positional parameter already used.
  New starting with 2.008: raw format specifiers. Using the "%r" specifier makes writef simply write the binary representation of the argument. Use "%-r" to write numbers in little endian format, "%+r" to write numbers in big endian format, and "%r" to write numbers in platform-native format.
void writefln(T...)(T args);
  Equivalent to writef(args, '\n').
void fwritef(_iobuf* fp,...);
  Kept for backward compatibility. Use writef instead.
void fwritefln(_iobuf* fp,...);
  Kept for backward compatibility. Use writefln instead.
string readln(_iobuf* fp = stdin, dchar terminator = '\x0a');
  Read line from stream fp.
  Returns:
  null for end of file, char[] for line read from fp, including terminating character
  Params:

  Throws:
  StdioException on error
  Example:
  Reads stdin and writes it to stdout.

      import std.stdio;

      int main()
      {
          char[] buf;
          while ((buf = readln()) != null)
      	write(buf);
          return 0;
      }

size_t readln(_iobuf* fp, ref char[] buf, dchar terminator = '\x0a');
size_t readln(ref char[] buf, dchar terminator = '\x0a');
size_t readln(_iobuf* f, ref wchar[] buf, dchar terminator = '\x0a');
size_t readln(_iobuf* f, ref dchar[] buf, dchar terminator = '\x0a');
  Read line from stream fp and write it to buf[], including terminating character.
  This is often faster than readln(FILE*) because the buffer is reused each call. Note that reusing the buffer means that the previous contents of it need to be copied if needed.
  Params:

  Returns:
  0 for end of file, otherwise number of characters read
  Throws:
  StdioException on error
  Example:
  Reads stdin and writes it to stdout.

      import std.stdio;

      int main()
      {
          char[] buf;
          while (readln(stdin, buf))
      	write(buf);
          return 0;
      }

  This method is more efficient than the one in the previous example because readln(stdin, buf) reuses (if possible) memory allocated by buf, whereas buf = readln() makes a new memory allocation with every line.
_iobuf* fopen(string name, string mode = "r");
  Convenience function that forwards to std.c.stdio.fopen with appropriately-constructed C-style strings.
_iobuf* popen(string name, string mode);
  Convenience function that forwards to std.c.stdio.popen with appropriately-constructed C-style strings.
struct lines;
  Iterates through the lines of a file by using foreach.
  Example:

      void main()
      {
        foreach (string line; lines(stdin))
        {
          ... use line ...
        }
      }

  The line terminator ('\n' by default) is part of the string read (it could be missing in the last line of the file). Several types are supported for line, and the behavior of lines changes accordingly:
  1. If line has type string, wstring, or dstring, a new string of the respective type is allocated every read.
  2. If line has type char[], wchar[], dchar[], the line's content will be reused (overwritten) across reads.
  3. If line has type invariant(ubyte)[], the behavior is similar to case (1), except that no UTF checking is attempted upon input.
  4. If line has type ubyte[], the behavior is similar to case (2), except that no UTF checking is attempted upon input.
  In all cases, a two-symbols versions is also accepted, in which case the first symbol (of integral type, e.g. ulong or uint) tracks the zero-based number of the current line.
  Example:

        foreach (ulong i, string line; lines(stdin))
        {
          ... use line ...
        }

  In case of an I/O error, an StdioException is thrown.
struct chunks;
  Iterates through a file a chunk at a time by using foreach.
  Example:

      void main()
      {
        foreach (ubyte[] buffer; chunks(stdin, 4096))
        {
          ... use buffer ...
        }
      }

  The content of buffer is reused across calls. In the example above, buffer.length is 4096 for all iterations, except for the last one, in which case buffer.length may be less than 4096 (but always greater than zero).
  In case of an I/O error, an StdioException is thrown.

Copyright © 1999-2008 by Digital Mars, All Rights Reserved | Page generated by Ddoc.