Title: Standard InputOutput
1Standard Input/Output
- Actually the C programming language has no input
or output operations defined as part of the
language, they must be user defined. - Print out the file named stdio.h and spend some
time studying it. The name stdio.h is sort of
cryptic for "standard input/output header", - The function getchar() reads a single character
from the standard input device, the keyboard, and
assigns it to the variable named c. The next
function putchar(), uses the standard output
device, the video monitor, and outputs the
character contained in the variable named c.
2getchar()
- When data is read from the keyboard, under
control of the operating system, the characters
are stored in a buffer (stdin) until a carriage
return is entered at which time the entire string
of characters is given to the program. When the
characters are being typed, however, the
characters are displayed one at a time on the
monitor. This is called echo, and happens in many
of the applications you run.
3- include ltstdio.hgt / header for input/output /
- void main()
-
- char c
- printf("Enter any characters, X halt
program.\n") - do
-
- c getchar() / get a single character from
the kb / - putchar(c) / display the character on
the monitor / - while (c ! 'X') / until an X is hit /
- printf("\nEnd of program.\n")
-
- / Result of execution Enter any characters, X
halt program. (The output depends on what you
type in.) /
4Now To Read In Some Integers
- Instead of reading in a character at a time, as
we have in the last three example programs, we
read in an entire integer value with one call
using the function named scanf(). This function
is very similar to the printf() that you have
been using except that it is used for input
instead of output. Examine the line with the
scanf() and you will notice that it does not ask
for the variable valin directly, but gives the
address of the variable since it expects to have
a value returned from the function. Failing to
supply a pointer to the parameter in the scanf()
function is the most common problem encountered
in using this function. - The function scanf() scans the input line until
it finds the first data field. It ignores leading
blanks and in this case, it reads numeric
characters until it finds a blank or an invalid
numeric character, at which time it stops reading
and returns the integer value.
5include ltstdio.hgt void main() int
valin printf("Input a number, stop with
100.\n") do scanf("d",
valin) / read a single integer value in
/ printf("The value is d\n", valin)
while (valin ! 100) printf("End of
program\n")
6Character String Input
- This program is identical to the last one except
that instead of an integer variable, we have
defined a string variable with an upper limit of
24 characters (remember that a string variable
must have a null character at the end). The
variable in the scanf() does not need an
because big is an array variable and by
definition it can be interpreted as a pointer.
This program should require no additional
explanation. - You will probably get a surprise when you run it
because it separates your sentence into separate
words. When used in the string mode of input,
scanf() reads characters into the string until it
comes to either the end of a line or a space
character. Therefore, it reads a word, finds the
space following it, and displays the result.
Since we are in a loop, this program continues to
read words until it exhausts the input buffer. We
have written this program to stop whenever it
finds a capital X in column 1, but since the
sentence is split up into individual words, it
will stop anytime a word begins with capital X.
7include ltstdio.hgt void main() char
big25 printf("Input a character string,
up to 25 characters.\n") printf("An X in
column 1 causes the program to stop.\n") do
scanf("s", big)
printf("The string is s\n", big) while
(big0 ! 'X') printf("End of
program.\n") / Result of execution Input a
character string, up to 25 characters. An X in
column 1 causes the program to stop. (The output
depends on what you type in.) /
8String input using gets()
- To input a complete string including spaces we
use the gets() function. - This copies all input up to, but not including,
the enter key. - The entered string should be stored in an array
of char as usual. - Remember to allow one extra char space in the
array for the null character
9include ltstdio.hgt void main() char
big25 printf("Input a character string,
up to 25 characters.\n") printf("An X in
column 1 causes the program to stop.\n") do
gets(big)
printf("The string is s\n", big) while
(big0 ! 'X') printf("End of
program.\n") / Result of execution Input a
character string, up to 25 characters. An X in
column 1 causes the program to stop. (The output
depends on what you type in.) /
10Standard I/O Summary
- getchar(), gets(),scanf()
- putchar(), printf(), puts()
- fflush() empties an i/o buffer
- flush(stdin) empties keyboard input buffer
- flush(stdout) forces any data in output buffer
to be output immediately. - See stdio.h for all I/O functions
11Files
- Two types
- Binary
- Text
- Binary data stored using binary format. E.g.
the 32-bit integer value 29458 would be stored as
00000000000000000111001100010010 in 4 byte
locations- 0x22, 0x73, 0x00, 0x00. Note low byte
first
12Text Files
- Data converted and store as ASCII characters
- Text files can be viewed printed
- The 32-bit integer 29458 would be stored in 5
byte locations-0x32, 0x39, 0x34, 0x35, 0x38 (
ASCII codes for digits 29458)
13Text files in C how to read from and write to
text files
- All these functions are in ltstdio.hgt
- fopen - Opens a text file.
- fclose - Closes a text file.
- feof - Detects end-of-file marker in a file.
- fprintf - Prints formatted output to a file.
- fscanf - Reads formatted input from a file.
- fputs - Prints a string to a file.
- fgets - Reads a string from a file.
- fputc - Prints a character to a file.
- fgetc - Reads a character from a file.
14f fopen("filename", "mode")
- We use fopen() to open a file.
- This function requires two parameters, the name
of the file and the "mode" we want to use to
open this file. The three main modes are "r", "w"
and "a" -- read, write and append. - f fopen("filename", "r") // open for reading
- f fopen("filename", "w") // open for writing
- f fopen("filename", "a") // open for appending
- The function returns a FILE pointer we can then
use this to access the file, in this case f.
15Example of writing data to a file
- include ltstdio.hgt
- include ltstdlib.hgt
- void main()
-
- FILE fp
- int num
- fp fopen("a\\data.txt", "w")
- if(fp0)
-
- printf("Error unable to open file\n")
- exit(-1)
-
- for(num 1 num lt 11 num)
-
- fprintf(fp, "d\n", num)
-
- fclose(fp)
16Example of reading data from a file using gets()
- include ltstdio.hgt
- include ltstdlib.hgt
- void main()
-
- FILE fp
- char string1000
- fp fopen("a\\data.txt", "r")
- if(fp0)
-
- printf("Couldn't open data.txt\n")
- exit(-1)
-
- while(fgets(string, 1000, fp)) // read as
characters -
- printf("s", string)
-
- fclose(fp)
17Example of reading data from a file as integers
- include ltstdio.hgt
- include ltstdlib.hgt
- void main()
-
- FILE fp
- int x
- fp fopen("a\\data.txt", "r")
- if(fp0)
-
- printf("Couldn't open file.txt\n")
- exit(-1)
-
- while( feof(fp) 0 )
-
- fscanf(fp, "d\n", x)
- printf("d ", x)
-
18Notes
- fgets(fp, n, str) - The fgets function reads a
string from the file pointer fp argument and
stores it in str. fgets reads characters from the
current stream position to and including the
first newline character, to the end of the
stream, or until the number of characters read is
equal to n 1, whichever comes first. The result
stored in string is appended with a null
character. The newline character, if read, is
retained and included in the string. Unlike
gets() function which replaces the newline
character with a null character ('\0') before
returning the line.
19feof()
- The feof function returns a nonzero value after
the first read operation that attempts to read
past the end of the file. It returns 0 if the
current position is not end of file.