twitter
    Find out what I'm doing, Follow Me :)

Outputting A Single Character At A Time

Load the next example file, charout.c, and display it on your monitor. This program will illustrate how to output a single character at a time.

#include "/sys/stdio.h"
main()
{
FILE *point;
char others[35];
int indexer,count;
strcpy(others,"Additional lines.");
point = fopen("tenlines.txt","a"); /* open for appending */
for (count = 1;count <= 10;count++) {
for (indexer = 0;others[indexer];indexer++)
putc(others[indexer],point); /* output a single character */
putc(’\n’,point); /* output a linefeed */
}
fclose(point);
}



The program begins with the "include" statement, then defines some variables including a file pointer. We have called the file pointer "point" this time, but we could have used any other valid variable name. We then define a string of characters to use in the output function using a "strcpy" function. We are ready to open the file for appending and we do so in the "fopen" function, except this time we use the lower cases for the filename. This is done simply to illustrate thatDOSdoesn’t care about the case of the filename. Notice that the file will be opened for appending so we will add to the lines inserted during the last program.

The program is actually two nested "for" loops. The outer loop is simply a count to ten so that we will go through the inner loop ten times. The inner loop calls the function "putc" repeatedly until a character in "others" is detected to be a zero.