|
Input/Output in C
An Introduction |
|
Prof. David Bernstein |
| Computer Science Department |
| bernstdh@jmu.edu |
<stdio.h>
FILE type for I/O
FILE *input_file;
System.in, System.out,
and System.err, you can use the
FILE variables stdin,
stdout, and stderr
fopen and fclose Functions:
FILE* fopen(const char* name, char* mode)
int fclose(FILE f)
name is (a pointer to) the name of the filemode is used to control the properties
of the file. A mode of "w" indicates that the file should be
opened for writing (i.e., output) and a mode of "r" indicates that
it should be opened for reading (i.e., input).printf and scanf
functions (and variants discussed below) for this purposeprintf Function:
scanf Function:
l
for long)| - | Left justify |
| 0 | Pad with 0s instead of blanks |
| + | Prepend the sign |
| d, i | Signed integer |
| f | Floating point |
| u | Unsigned integer |
| s | String (i.e., pointer to an array of char) |
| c | Character |
| o | Unsiged octal |
| x | Unsigned hexadecimal |
| e | Floating point in scientific notation |
| p | Pointer (as a character sequence) |
| n | Write the number of characters written so far to the given address |
An Example
int n;
printf("The answer is %d!", n);
fprintf Function:
fscanf Function:
FILE to use for
input/outputsprintf function:
printf except that it does not
write to an output device; it stores what would be
printed in the given string
snprintf function:
sprintf except that the maximum number of
characters is specifiedfread Function:
fwrite Function: