270 likes | 338 Views
Topic 13 – Various Other Topics. Enumerated Types. Enumerated Types. We have seen a way to construct new datatypes that are composed of a combination of other datatypes. This is called a structure.
E N D
Enumerated Types • We have seen a way to construct new datatypes that are composed of a combination of other datatypes. This is called a structure. • The C language also allows the creation of new datatypes that are “simple” datatypes, similar to ints, chars, etc… CISC105 – Topic 13
Enumerated Types • An enumerated type is a datatype with its own, programmer-defined, list of possible values. • This is useful when a datatype is necessary that can have one of a set of some possible values. • An an example, consider a new datatype for the manufacturer of cars. CISC105 – Topic 13
Enumerated Types • This datatype definition could look like: typedef enum { GM, Honda, Ford, Nissan, Toyota, Chrysler } automaker; • This definition defines a new datatype, automaker. A variable of this datatype can have any of the six values listed in the definition. CISC105 – Topic 13
Enumerated Types typedef enum { GM, Honda, Ford, Nissan, Toyota, Chrysler } automaker; • This definition goes in the same location in a C file as a structure definition, before the function prototypes. • Once this definition is present, any function is free to declare variables of this new datatype. CISC105 – Topic 13
Enumerated Types • This allows us to use a variable of this new datatype in the same ways as a variable of other datatypes: automaker car1, car2 = Ford; car1 = Nissan; if (car1 == Nissan) printf(“It’s a Nissan!”); if (car2 == Ford) printf(“It’s a Ford!”); CISC105 – Topic 13
Enumerated Types • So, why is this called an enumerated type? • The computer does not know what a “Ford” is. Therefore, the enumerated type definition uses integers that correspond to the enumerated values. CISC105 – Topic 13
Enumerated Types • Thus, GM has a value of 0, Honda has a value of 1, Ford has a value of 2, Nissan has a value of 3, Toyota has a value of 4, and Chrysler has a value of 5. typedef enum { GM, Honda, Ford, Nissan, Toyota, Chrysler } automaker; CISC105 – Topic 13
Enumerated Types • We can see that GM (0) is less than Honda (1), which is less than Ford (2), etc… Thus, we know that (for example): • Honda < Chrysler • Ford != Nissan • Toyota >= GM typedef enum { GM, Honda, Ford, Nissan, Toyota, Chrysler } automaker; CISC105 – Topic 13
A Further Example void display_carmake(automaker carmake) { switch (carmake) { case GM: printf(“The make is GM.”); break; case Honda: printf(“The make is Honda.”); break; case Ford: printf(“This make is Ford.”); break; case Nissan: printf(“The make is Nissan.”); break; case Toyota: printf(“The make is Toyota.”); break; default: printf(“The make is Chrysler”); } } CISC105 – Topic 13
Input Redirection • We have previously seen how to use a text file to substitute for user input in a program (batch mode). • The program still expects input using scanf statements. • The text file must contain one value for each value expected in the program. • This is done using the input redirection operator at the UNIX (or Windows) prompt: a.out < input_data CISC105 – Topic 13
Input Redirection • It is important to realize that the program has no knowledge it will be receiving its input data from a file. • This operator simply instructs the operating system to change where the standard input device is. It replaces the normal input device (the keyboard) with the file specified. • Thus, when the scanf statement attempts to read data from the standard input device, it reads from the file instead of the keyboard. CISC105 – Topic 13
Program-Controlled File I/O • It is possible to write a C program such that it directly works with a file, instead of altering the standard input device. • The first thing that must be done is to declare a file pointer variable. • This is a variable that stores the information necessary to access a file. • File pointers are of type FILE *. CISC105 – Topic 13
Program-Controlled File I/O • One file pointer is required for each file that will be accessed. • Once a file pointer has been declared, it must be assigned to a file and that file must be opened. • This is done using a fopen function call. This function takes two parameters, a string with the file name, and a string with the type of access desired (such as read, write, etc…) CISC105 – Topic 13
Here, the file “datafile” is opened for read (“r”) access. A pointer to this file is assigned to input_file. Note that we can use this file pointer to READ the file only! Here, the file “output” is opened for write (“w”) access. A pointer to this file is assigned to output_file. Note that we can use this file pointer to WRITE the file only! Here, the file pointers are declared. They are not yet associated with any file. Program-Controlled File I/O • Once the file is opened, it can be accessed by using the file pointer associated with it. FILE *inp, *outp; inp = fopen(“datafile”,”r”); outp = fopen(“output”,”w”); CISC105 – Topic 13
Errors in Opening Files • When the operating system is unable to open a file, for whatever reason, the pointer returned (the file pointer) is set to NULL. • Thus, to see if a file is opened successfully, test the file pointer to see if it is NULL after the call to fopen. CISC105 – Topic 13
Errors in Opening Files FILE *inp, *outp; inp = fopen(“input_file”,”r”); outp = fopen(“output_file”,”w”); if (inp == NULL || outp == NULL) printf(“Error opening one of the files.”); else { /* files were both opened correctly and we can access them */ } CISC105 – Topic 13
File Input • Once the file pointer is ready, input from a file opened with “read” access can be performed using the fscanf function. This function works just like scanf except that it takes one additional parameter, the file pointer: fscanf(inp,”%d”,&x); CISC105 – Topic 13
Closing File Pointers • Once all access to an open file is completed, the file must be closed. • This is done with a call to fclose, which takes one parameter, the name of the file pointer that controls the file the program has finished accessing. CISC105 – Topic 13
Closing File Pointers FILE *inp, *outp; inp = fopen(“input_file”,”r”); outp = fopen(“output_file”,”w”); if (inp == NULL || outp == NULL) printf(“Error opening one of the files.”); else { /* do something with files! */ fclose(inp); fclose(outp); } CISC105 – Topic 13
File Input • Using this syntax has the same effect as using scanf and redirecting the input using the “<“ operator. • The file that is being read must contain the data the program is expecting, and it must be in order. • In addition to fscanf, there is also the fgets function which reads in an entire line from the file and stores that line in a string variable. CISC105 – Topic 13
File Input • fgets takes three parameters, the string to store the line being read, a maximum number of characters to read in, and the file pointer to read from. • This second parameter can be used to prevent an overflow of the string. • As an example, CISC105 – Topic 13
File Input • This last statement reads in a line from the file that inp points to, input_file. It stores the first 19 characters in the string buffer. FILE * inp; char buffer[20]; inp = fopen(“input_file”,”r”); fgets(buffer,19,inp); CISC105 – Topic 13
File Output • Just like we can read from a file that has been opened with read access, we can write to a file that has been opened with write access. • This is done with the fprintf function. • This function behaves just like printf except it takes one addition parameter, the file pointer to write to. CISC105 – Topic 13
File Output FILE * outp; int linenum = 1; outp = fopen(“output_file”,”w”); fprintf(outp,“This string will go as the %d st line in the file.”, linenum); • What does this call to fprintf do? It outputs the string “This string will go as the 1 st line in the file.” to the file pointed to by outp, output_file. Since this happens immediately after the program opens the file for writing, this string will be the 1st line of the file, output_file. CISC105 – Topic 13