Slimelite BBS

Re: C File I/O - Basics

Back to R25C

From Ian James Abbott · 1996-04-09 07:19 · #75276 in R25C

In reply to C File I/O - Basics by Matthew Probert

From: jambo@spuddy.mew.co.uk (Ian James Abbott)

Yo Matthew!
>Additionally the transfer mode may be defined by appending 'b' or 't' to the
>access mode string. A value of 'b' will cause the stream to be opened in
>binary mode, and a value of 't' will cause the stream to be opened in text
>mode. To open and create a new file in binary mode (no translation) with
>read/write access you would use:

Actually, the effect of appending 't' is not defined by the Standard.
The Standard allows extra characters such as that to appear in the mode
string, but any meaning attached to such characters is implementation-
defined. However 'b' is defined. Also, the 'b' is allowed to appear
before the '+'. If the mode string is one of those defined by the
Standard and it doesn't contain 'b'. then it indicates text mode.

>                FILE *fopen(const char *s,const char *access);

Your identifiers for the parameters here are not very meaningful. How
about:

    FILE *fopen(const char *filename, const char *mode);

>fgets() stops reading either when numbytes - 1 bytes have been read, or a
>line feed character is read in. A null terminating byte is appended to the
>read string, s. If an error occurs, fgets() returns NULL.

It also stops reading when the end of file is encountered of course!

>fputs() writes a null terminated string to a previously opened stream. It

But it only writes up to, not including the null terminator.

>takes two parameters; a character pointer to the string to be written and a
>FILE pointer to the open stream. The prototype for fputs() looks like this:
>
>                         int fputs(char *s, FILE *fp);
>
>If an error occurs in writing to the stream, fputs() returns a value of EOF
>which is defined in stdio.h.

Otherwise it returns a non-negative value.

>Formatted I/O may be achieved with fprintf() and fscanf(). These are standard
>library C functions related to printf() and scanf() except that they take an
>additional parameter, an open stream. The prototype for fprintf() is:
>
>          int fprintf(FILE *fp, const char *format[,argument, ...]);

The actual prototype is:

    int fprintf(FILE *fp, const char *format, ...);

>>fprintf() attempts to write to the specified stream. If an error occurs, it
>returns the value EOF.

Not necessarily EOF. It is only guarenteed that a negative value is
returned on error. If no error occured, the number of characters
transmitted (before stream type conversion?) is returned.

>fscanf() has the prototype:
>
>           int fscanf(FILE *fp, const char *format[,address, ...]);

    int fscanf(FILE *fp, const char *format, ...);

>fscanf() attempts to read from the specified stream a series of input fields.
>fscanf() returns the number of input fields read and stored.

Or EOF if an input failure occurs before any conversion.

-- 
  * Meeeow! Call Spuddy on +44 1268 515441 for FREE mail & Usenet access. *
--- ifmail v.2.8c
 * Origin: Spud's Public Usenet Domain (2:257/108.12@fidonet)