C Programming

By Guy Lecky-Thompson

Lesson 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 file

hFile = fopen(“test”,”r”); // Read only access fscanf(“%s %d %f”, szText, &nInt, &fFloat); fclose (hFile);

String handling is only a little different:

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…

Print this Page Print this page


Previous Page  1  2  3  4  5  6 


Lessons

Lesson 1: Introduction to Programming
Lesson 2: Introduction to C
Lesson 3: Decision Making in C
Lesson 4: Repetition in C
Lesson 6: User Defined Data
Lesson 7: Functions
Lesson 8: Common Standard Libraries