Most programs rely on input and output. The C language manage this data flow by channels to and from the program. Be default the C language provides three channels in each program:
These channnels are uni-directional, meaning that data can only flow in one direction. Unix/Linux/Macs allow for redirection of these standard channels or to link standard channels between two programs ("piping"), so the programmer should never assume that printing to the standard output appears on screen.
Programs can also creates new channels (e.g. to access files or to read the input from a mouse), which have the option to be bi-directional. Typically this is the default, because it is more economical to use only one channnel to read from and to write to a file. Once created the input and output is very similar to those of the standard channels.
The most common functions is printing with a defined format. The syntax is
printf( format-string , commma separated list of variabls to print );
printf is one of the few function which accept an arbitrary number of arguments, however a format string is always mandatory.
The format string is printed to the screen as it is except for character sequences starting with '%' or '\'. The backslash indicates non-printable or special characters it is either followed by a character or the ASCII code of that character. The most common special character is \n, which is the newline control character. So the command printf("Hello World\n");
prints out the string Hello World and then advance to the next line. If \n is omitted the next print command would start its output right after the 'd' of 'World'. Another special characters is \" and \\ which produces the double quotation mark and the backslash itself. A complete list of all special characters can be found here.
The percent symbol indicates a place holder in the format string for the variable, following the format string in the printf command. There must be always as many variables as there are place holder in the format string. The first place holder corresponds to the first variable in the list after the format string, the second place holder to the second variable, and so on.
A character right after the percent symbol identifies the basic format, whether to print the variable as a single character, a string, its ASCII value. Unfortunately allows the printf command some nasty casting regardless of the type (e.g equivalent to a =(int *) &b;
, where b is a float).
The symbol for printing integer is 'd'. Asuming that the integer variable a has the value 123, the format string "%d" (aka printf("%d",a);
) produces the output '123'. A number between the percent and the d-character overrides the default length to allow for additional spaces. The output is right adjust for a positive number and left for a negative. Example "%4d" and "%-4d" produces ' 123' and '123 ', respectively. With an addition 0 before the number the spaces are filled with zeros ("%05" gives '00123').
Characters and strings are marked with the character 'c' and 's', respectively. The character placeholder allows an addition integer number to print more spaces. Strings allow also fractional number the integer part serves the same purpose as the number for characters and integer. The fractional part is the precission, determining how much of the actual string is printted. Example: a string has the value "Hello World". The format "%s" would print the complete string. So does "%15s" but with preceeding spaces to give in total 15 characters. "%s10.5" prints 10 characters but only 5 from the string (only "Hello" would appear on screen").
Floating points behave similar to string, where the integer number indicates the total length to be printed and the fractional part the digits after the period. Note that the total length must include a possible negative sign or the exponent. Floating points have three indicates: 'e' for scientific notation, 'f' for floating point numbers and 'g' for either 'f' or 'e', whatever is shorter.
A complete set of output formater is given in the reference.
The corresponding function to printf is scanf for the input. The syntax is very similar, except that scanf only accept only the reference to variables and not the variables itself (after all scanf is just a function, which must obay the same rules of passing variables by value or reference. Because it is expected to get some return values, the variables have to be referenced in the function call.)
The format string is interpretated differently for the read procedure. It is there any character which is not a formating place holder (indicated by a percent symbol). It has to match the input, otherwise the function will report an input error. The format indicated are the same as for the output, but optional characters between the identifier and the percent symbol have now a different meaning. They are:
Example: The input string is
45 , 1.2 ignore this D input example
The commands to parse the string is
int a;
double b;
char c,*d,*e
scanf("%d , %lf %*s %*s %c %s %[^p]",&a,&b,&c,d,e);
The input is processed in this way:
The two functions sprintf and sscanf are similar to printf and scanf, but acts on a string instead of the standard input and output. They require a string as the first arguments of the function call, followed then by the format string and the variable list.
Before a file can be access the channnel has to be created with the fopen command. It returns a pointer to the derived data type FILE or a null pointer when the operation was not successful. It requires two string arguments: the file name and the access method. Allowed formats are: "r" to read a text file, "w" to write a text file, "a" to append to a text file, "rb" to read a binary file, "wb" to write a binary file and "wa" to append to a binary. Note that for reading and appending, the file must exist. A write operation might overwrite an existing file with the same name. An additional plus sign for the access methods (e.g. "w+") opens the file for both write and read access. A channel is released with the fclose command, with the file pointer as its argument.
Writing and reading is done with the commands fprintf and fscanf, which are similar to printf and scanf, except they require the file pointer as its first argument, followed by the format string and the variable list.
Example of a short program to write 'Hello World' to a file:
FILE *ip;
ip=fopen("helloworld.txt","w");
fprintf(ip,"Hello World!\n");
fclose(ip);
Other file operation are summerized in the following table:
Command | Syntax | Description |
fflush | fflush(ip) | Writes out the current buffer |
ftell | i=ftell(ip) | Return the current file position indicator as an integer. |
fseek | fseek(ip,offset,place) | Advance the file position indicator by offset bytes with respect to place (0: beginning of file, 1: current position, 2: end of file); |
rewind | rewind(ip) | Set file position indicator to beginning of file. |
tmpfile | ip=tmpfile() | Creates a temporary file with the access "wb+" |
fwrite | fwrite(data,size,elements,ip) | Copies the memory content at position data and length size*elements as binary data to file. |
fread | fread(data,size,elements,ip) | Copies binary content of length size*elements from file to memory position data. |
css_footer(); ?>