C_Structs_Enums_Files
Home Up ComputingConcepts Definitions Characters & Strings C_Formatted IO C_Structs_Enums_Files FunctionsAndMenus Arrays and Cmd Line Args Arrays & Structures Structs, Enums, Ptr Variables Ptrs. to External Files Miscellaneous Random Files

 

 Home Up

C Programming


Structures, Enumerations,
 and
File Processing

This page will contain information about Structures, Enumerations, and File Processing with the C language. 

There are many more subjects that could be covered, but due to lack of time and space they are not included in this web site.

 

STRUCTURES

 
Structures are collections of related variables, sometimes referred to as aggregates, under one name.
Structures can contain variables of different data types.
The keyword struct begins every structure definition. Within the braces of the structure definition are the structure member declarations.
Members of the same structure must have unique names.
A structure definition creates a new data type that can be used to declare variables.
There are two methods for declaring structure variables. The first method is to declare the variables in a declaration as is done with variables of other data types using struct tagName as the type.  The second method is to include the variables between the closing brace of the structure definition and the semi-colon that ends the structure definition.
The tag name of the structure is optional. If the structure is defined without a tag name, the variables of the derived data type must be declared in the structure definition, and no other variables of the new structure type can be declared.
A structure can be initialized with an initializer list by following the variable name in the structure declaration with an equal sign and a comma-separated list of initializers enclosed in braces. If there are fewer initializers in the list than members in the structure the remaining members are automatically initialized to zero (or null if the member is a pointer).
Entire structures may be assigned to structure variables of the same type.
A structure variable may be initialized with a structure variable of the same type.
The structure member operator ( . ) --also called the dot operator-- is used when accessing a member of a structure via the structure variable name.
The structure pointer operator ( -> )-- also called the arrow operator -- is used when accessing a member of a structure via a pointer to the structure.
Structures and individual members of structures are passed to functions call by value.
To pass a structure call by reference, pass the address of the structure variable.
An array of structures is automatically passed call by reference.
To pass an array call by value, create a structure with the array as a member.
Creating a new name with typedef does not create a new type; it creates a name that is synonymous with a previously defined type.

 

Enumerations  

An enumeration.designated by the keyword enum, is a set of integers that are represented by identifiers. The values in an enum start with 0 unless specified otherwise, and are always incremented by 1.
An example of a statement of this type follows:
enum months (JAN = 1, FEB, MAR, APR, MAY, JUN);

FILE PROCESSING
All data items processed by a computer are reduced to combinations of zeros and ones.
The smallest data item in a computer (a bit) can assume the value 0 or the value 1.
Digits, letters, and special symbols are referred to as characters. Together they are called the computer's character set.
A field is a group of characters that conveys meaning.
A record is a group of related fields.
At least one field in each record is normally chosen as a record key. The record key identifies a record as belonging to a particular person or entity.
One type of organization for records in a file is called a sequential access file in which records are accessed consecutively until the desired data are located.
A group of related files is sometimes called a database.
A collection of programs designed to create and manage databases is called a database management system (DBMS).
C views each file simply as a sequential stream of bytes.
C automatically opens 3 files and their associated streams--standard input, standard output, and standard error--when program execution begins.
The file pointers assigned to these files are stdin, stdout, and stderr, respectively.
Function fgetc reads a character from a specified file.
Function fputc writes a character to a specified file.
Function fgets reads a line from a specified file.
Function fputs writes a line to a specified file.
FILE is a structure type defined in the stdio.h  header file. 
As a file is opened, a pointer to the file's FILE structure is returned.
Function fopen takes two arguments--a file name and a file open mode--and opens the file. 
Function feof determines whether the end-of-file indicator for a file has been set.
Function fprintf is equivalent to printf except fprintf receives as an argument a pointer to the file to which the data will be written.
Function fclose closes the file pointed to by its argument.
File open modes:
"w" opens a file for writing; used to create a file, or to discard the contents of a file before writing data.
"r" opens an existing file for reading.
"a" opens a file for appending, or adding records to the end of an existing file.
"r+" opens a file for reading and writing.
"w+" creates a file if it does not exist and discards the current contents of a file if it does exist.
"a+" creates a file if it does not exist, and writing is done at the end of the file.

Function fscanf is equivalent to scanf except that fscanf receives as an argument a pointer to the file (normally other than stdin) from which the data will be read. Function rewind causes the program to reposition the file position pointer for the specified file tot he beginning of the file. Random access file processing is used to access a record directly.
To facilitate random access, data is stored in fixed-length records. Since every record is the same length, the computer can quickly calculate (as a function of the record key) the exact location of a record in relation to the beginning of the file.
Function fwrite writes a block (specific number of bytes) of data to a file.
The compile-time operator sizeof returns the size in bytes of its operand.
Function fseek sets the file position pointer to a specific position in a file based on the starting location of the seek in the file. The seek can start from one of three locations:
SEEK_SET starts from the beginning of the file.
SEEK_CUR starts from the current position in the file.
SEEK_END starts from the end of the file.
Function fread reads a block (specific number of bytes) of data from a file.


 Back Home Next