I/O Functions
See the list of functions on this reference website.
Standard input/output
We must #include <stdio.h>
to use this library. The standard
(built-in) streams are defined there:
stdin
: keyboard or input redirect inputstdout
: standard output (to console by default)stderr
: standard error output (to console by default)
Output in C
Prerequisite: #include <stdio.h>
in header to access library functions; Reference for stdio.h
-
putchar('d')
to output characters -
puts("some string")
to output strings -
printf("format string", args...)
for fancier output - formats:
%i %d %f %c %s %n %ld %lf
for basic types
%o
octal printing of ints
%x
hex printing of ints - field widths
%cols.decf
- eg.%.2f
,%.6f
right justified by default, use%-
to left justify
-
and cols works for %d and %s also - See Kernighan Appendix B or this reference for details
Input in C
Character-based Input
-
stdio
functiongetchar()
reads one character from a file - Returns
EOF
if at end of file - Value of
EOF
is -1
getchar()
returns anint
type for this reason (automatically cast tochar
if needed) - Type ctrl-d in a Unix system to produce an
EOF
from keyboard (ctrl-Z (Enter) on a PC)
Formatted Input
- The
scanf
function works similarly to the printf output function for reading formatted input. - We use a format
string followed by the
memory locations (addresses) we are reading into. - We indicate this for most variables by preceding the
variable name with an ampersand character (
&
) to indicate "address of" the variable. - An exception is strings because they are arrays, and as we'll learn soon, array names are the memory address where the array starts. </p>
%d integer %ld long %c char %s string %f float (real number type) %lf double, Lf long doubleThere are also many fancy input format options, see scanf documentation for details:
- Scan set notation:
%[a-zA-Z]
- inputs as long as chars in set,[^0-9]
is "all except" inverse - Use character literals to requre but skip over them, eg
%d-%d-%d
would successfully read2-3-4
- Use
%*c
to skip any character (*
skips assigning any value)
Here is a short example: ```c #include <stdio.h> int main() { int number; float x; double y; char word[20]; scanf("%d", &number); scanf("%f %lf", &x, &y); scanf("%s", word); } ```
Note that we don't need &
in front of word because
as an array it is an address already. We also don't use a
subscript since we're reading into the whole array. When
reading a string, scanf only reads up to first whitespace
(space, tab, ret).
Sequential Files
- Declare as:
FILE *fptr;
(FILE
is the type of files, defined in<stdio.h>
) - Must open file for use, giving it pointer, name, and mode
fptr = fopen("name.txt", "w")
(returns NULL if error) - types of file openings:
-
"r"
- reading -
"w"
- create or overwrite file for writing -
"a"
- open or create file for appending to end -
"r+"
- open for update (read and/or write) -
"w+"
- create (overwrite) file for read &/or write -
"a+"
- open for read or write to end
-
- Close a file when done:
fclose(fptr);
- very important to permanently save file! - Test for end of file:
feof(fptr)
returns 1 if end, 0 otherwise fflush(fptr)
- flush the buffer - usually for outputrewind(fptr)
- setfptr
to the beginning of file- The I/O functions above have more general versions for working with files, not just
stdin/stdout
: put anf
prefix on the name and addfileptr
parameter:-
fprintf(fileptr, formatstring, ...);
fscanf(fileptr, formatstring, ...);
fgetc(fileptr)
; notefgetc(stdin) == getchar()
fputc('c',fileptr)
; notefputc('c', stdout) == putchar('c')
fputs(str,maxlength,fileptr)
; notemaxlength
is there to prevent buffer overrun holesfgets(str,maxlength,fileptr)
-