C Programming
By Guy Lecky-ThompsonLesson 5: I/O & Data Types
Summary
This simple use of input and output of information is vital to any program that is going to perform any function that offers interactivity to the user. Plus, as an added bonus, the stdio.h library also includes functions for file input and output, as well as handling strings of formatted information.
These are prefixed by an s or f, for string or file. Any good reference can be used to find out more about file handling, but here is a taste:
FILE * hFile; int nInt; float fFloat; hFile = fopen(“test”,”w”); // Write access fprintf(hFile, ”Test 1 2.3”); fclose(hFile); // Close the fileString handling is only a little different:hFile = fopen(“test”,”r”); // Read only access fscanf(“%s %d %f”, szText, &nInt, &fFloat); fclose (hFile);
sprintf(szOut, “Text %d”, &nInt);For a more complete look at files, Chapters 28 & 29 of 'Absolute Beginners Guide to C', has an easy to read approach to discussing advanced file handling. For the scientific view, check out Chapter 12 of 'C Primer Plus'.
Now that the student knows enough to write interesting programs which fulfill specific functions, they might like to consider writing a program which allows the user to specify how many bottles there are at the start of the rhyme, and count down from there…