3.4 - Standard Input/Output
- OS uses 3 file descriptors to provide standard input output functionalities:
- 0 → stdin
- 1 → stdout
- 2 → stderr
- These are sometimes referred as “The Standard Streams”
- The IO access example could be:
- stdin → Keyboard, pipes
- stdout → Console, Files, Pipes
- stderr → Console, Files, Pipes
The header file
- You need to refer input/output library function
#include <stdio.h>
- When the reference is made with “<name>” the search for the files happen in standard path
- Internal binary representation of the data directly between memory and the file.
- Basic form of I/O, simple, efficient and compact.
- Unformatted I/O is not directly human readable, so you cannot type it out on a terminal screen or edit it with a text editor.
- getchar() and putchar() are two functions part of standard C library.
- Some
functions like getch(), getche(), putch() are defined in conio.h, which
is not a standard C library header and is not supported by the
compilers targeting Linux / Unix systems
- Data is formatted or transformed
- Converts the internal binary representation of the data to ASCII before being stored, manipulated or output
- Portable and human readable, but expensive because of the conversions to be done in between input and output
- The printf() and scanf() functions are examples of formatted output and input
Standard I/O – printf()
Prototypeint printf(char *format, arg1, arg2, ...);
or int printf(“format string”, [variables]);
where format string arguments can be%[flags][width][precision][length]specifier
%format_specifier is mandatory and others are optional
- Converts, formats, and prints its arguments on the standard output under control of the format
- Returns the number of characters printed
int printf(char *format, arg1, arg2, ...);
Standard I/O – printf() - Type SpecifiersStandard I/O – printf() - Type Length Specifiers
Standard I/O – sprintf() - Printing to string
Prototype
int sprintf(char *string, char *format, arg1, arg2, ...);
- Similar to printf() but prints to the buffer instead of stdout
- Formats the arguments in arg1, arg2, etc., according to format specifier.
- Buffer must be big enough to receive the result
#includeint main() { int num1 = 123; char ch = 'A'; float num2 = 12.345; char string1[] = “sprintf() Test”; char string2[100]; sprintf(string2, “%d %c %f %s\n”,num1 , ch, num2, string1); printf(“%s”, string2); return 0; }
Standard I/O – Formatted Input - scanf()
Prototype:
int scanf(char *format, ...);
or
int scanf(“string”, [variables]);
- Reads characters from the standard input, interprets them according to the format specifier, and stores the results through the remaining arguments.
- All most all the format specifiers are similar to printf() except changes in few.
- Each of the other argument must be a pointer.
- It returns as its value the number of successfully matched and assigned input items.
- On the end of file, EOF is returned. Note that this is different from 0, which means that the next input character does not match the first specification in the format string.
- The next call to scanf() resumes searching immediately after the last character already converted.
#includeint main() { int num1; char ch; float num2; char string[10]; scanf(“%d %c %f %s”, &num1 , &ch, &num2, string); printf(“%d %c %f %s\n”, num1 , ch, num2, string); return 0; }
sscanf() - Reading from string
Prototype
int sscanf(char *string, char *format, ...);
- Similar to sscanf() but read from string instead of stdin
- Formats the arguments in arg1, arg2, etc., according to format
#includeint main() { int age; char array_1[10]; char array_2[10]; // sscanf(“I am 30 years old”, “%s %s %d”, array_1, array2, &age); sscanf(“I am 30 years old”, “%*s %*s %d”, &age); printf(“OK you are %d years old\n”, age); return 0; }
- Buffer is a consecutive memory block used for temporary storage
- The standard I/O, except stderr, uses the buffer to store the data.
- The input would be stored in the user space before providing it to your process
- The main idea is to reduce the context switching between user and kernel space which leads to better I/O efficiency
- The output buffer get flushed out due to the following reasons:
- Normal Program Termination
- '\n' in a printf
- Read
- fflush call
- Buffer Full
- Refers to the technique of temporarily storing the results of an I/O operation in user-space before transmitting it to the kernel (in the case of writes) or before providing it to your process (in the case of reads)
- This technique minimizes the number of system calls (between user and kernel space) which may improve the performance of your application.
- For example, consider a process that writes one character at a time to a file. This is obviously inefficient: Each write operation corresponds to a write() system call
- Similarly, imagine a process that reads one character at a time from a file into memory!! This leads to read() system call
- I/O buffers are temporary memory area(s) to moderate the number of transfers in/out of memory by assembling data into batches.
...
Post a Comment