C Lecture 6
[Previous Lecture]
[Lecture Index]
[Next Lecture]
File Input/Output
- FILE *fopen(char *file, char *mode)
opens the named file; mode is typically
"r" or "w".
- int fprintf(FILE *fp, char *fmt, ...)
used for generating formatted output.
- int fscanf(FILE *fp, char *fmt, ...)
used for parsing formatted input.
- int fgetc(FILE *fp)
read a single character.
- int fputc(c, FILE *fp)
write a single character.
- long ftell(FILE *fp)
returns current file position
- long fseek(FILE *fp, long offset, int whence)
sets current file position.
- int fread(char *buf, int size, int nelem, FILE *fp)
reads unformatted input
- int fwrite(char *buf, int size, int nelem, FILE *fp)
writes unformatted output
- int fflush(FILE *fp)
flushes buffer.
- int fclose(FILE *fp)
closes a file (flushes buffers, releases resources).
Parameter passing:
- function arguments always passed by value:
copy of value is placed on the stack
- the value passed can be the address of something:
Accomplished by
- using the name of an array
(address of array will be passed)
- applying the & operator to a variable
(address of where variable is stored is passed)
This may be done for any of the following reasons:
- function is to modify caller's variable
- when arrays are passed
(contents of arrays can't be passed as values)
- to avoid copying large structures
{...} block variables:
- start with random values
- only available in block
- memory shouldn't be used after leaving block
- multiple versions of variable can be in use at once
(recursion: each call has own value)
static {...} block variables:
- start with 0 value
- only available in block
- value preserved between block exit/entry
- in recursion, all calls share same value
global variables:
- start with 0 value
- available anywhere in program
static global variables:
- start with 0 value
- only available in the file
- functions can be declared static
strtok.c
char *
strtok(char *str, char *delims)
{
static char *pos = (char *) 0;
char *start = (char *) 0;
if (str) /* Start a new string? */
pos = str;
if (pos) {
/* Skip delimiters */
while (*pos && strchr(delims, *pos))
pos++;
if (*pos) {
start = pos;
/* Skip non-delimiters */
while (*pos
&& !strchr(delims, *pos))
pos++;
if (*pos)
*pos++ = '\0';
}
}
return start;
}
Storage classes:
- static:
- blocks variables: persistent across calls.
- global variables: only accessible within file.
- auto: used with block variables. Useless.
- register: parameter and block variables. Nearly useless.
- extern: declares a variable or function that is defined elsewhere.
- the variable or function must be global
- can't be used to declare static objects
Qualifiers:
- const: value can't be changed. Uses:
- specification of function interface
- ensure correct implementation
- allows compiler to optimize
- volatile: tells compiler contents may change asynchronously.
Symbolic constants
Two methods of defining constants:
- use #define name value
(handled by the C preprocessor)
- use enumerations
(handled by the C compiler)
#define
- can be used for any type of constant
(strings, real numbers, integers and more)
- compiler and debugger have no knowledge of them
Enumerations
enum enum-name {
ident-name , ... };
enum fruit { apple, orange, pear };
- used to create set of constants
(but only integer constants)
- type of constant is int
(can be used anywhere an int can be used)
- don't have to specify value explicitly
(but you can if you need to)
- compiler and debugger know about them
[Previous Lecture]
[Lecture Index]
[Next Lecture]