- Forward


Input/Output in C
An Introduction


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu

Print

Getting Started
Back SMYC Forward
  • The C input/output functions are declared in <stdio.h>
  • We need to consider files, ASCII I/O and binary I/O
Files and the Console
Back SMYC Forward
  • File Variables:
    • C uses the FILE type for I/O
    • You declare a file variable as follows:
      FILE *input_file;
  • The Console:
    • If you only need to use "console I/O" (i.e., "standard in", "standard out", and "standard error"), which Java calls System.in, System.out, and System.err, you can use the FILE variables stdin, stdout, and stderr
Opening/Closing Files
Back SMYC Forward
  • Background:
    • Files need to be opened before they can be used and closed when you are through with them
    • The standard files are opened and closed for you
  • The fopen and fclose Functions:
    • FILE* fopen(const char* name, char* mode)
    • int fclose(FILE f)
  • The Parameters:
    • name is (a pointer to) the name of the file
    • mode 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).
ASCII I/O
Back SMYC Forward
  • Formats:
    • Many applications have to generate output in a particular format and/or read input files that are formatted in a particular way
    • C uses the variadic printf and scanf functions (and variants discussed below) for this purpose
  • Syntax of the printf Function:
    • printf(format [, argument]...)
  • Syntax of the scanf Function:
    • [number=]scanf(format [, argument]...)
  • Parameters:
    • format denotes a format string
    • argument denotes the item to be input/output
    • number denotes the number of parameters successfully input
The Format String
Back SMYC Forward
  • Contents:
    • Literals
    • Conversion Specifications
  • Conversion Specifications:
    • %[flags] [width] [.precision] [length] specifier
  • Interpretation/Use:
    • flags - justification, leading characters, etc...
    • width - minimum number of characters to output
    • precision - number of decimal places or significant digits
    • length - specifies the size of the argument (e.g., l for long)
Some Flags
Back SMYC Forward
- Left justify
0 Pad with 0s instead of blanks
+ Prepend the sign
Some Conversion Specifiers
Back SMYC Forward
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
The Format String (cont.)
Back SMYC Forward

An Example

int n; printf("The answer is %d!", n);
ASCII I/O with a File
Back SMYC Forward
  • The fprintf Function:
    • fprintf(file, format [, argument]...);
  • The fscanf Function:
    • [number=]fscanf(file, format [, argument]...)
  • Parameters:
    • file denotes the FILE to use for input/output
Creating Formatted Strings
Back SMYC Forward
  • The sprintf function:
    • sprintf(string, format [, argument]...);
  • Behavior:
    • Just like printf except that it does not write to an output device; it stores what would be printed in the given string
  • The snprintf function:
    • Like sprintf except that the maximum number of characters is specified
Binary Input/Output
Back SMYC Forward
  • The fread Function:
    • [read_size = ] fread(data_pointer, 1,size, file);
  • The fwrite Function:
    • [write_size = ] fwrite(data_pointer, 1,size, file);
There's Always More to Learn
Back -