Islamic University of Gaza Faculty of Engineering Department of Computer Engineering ECOM 4010: Lab # 4 Files & Queues in C Eng. Haneen El-Masry October, 2013
2 FILE * Files in C For C File I/O you need to use a FILE pointer, which will let the program keep track of the file being accessed. fopen To open a file you need to use the fopen function, which returns a FILE pointer. Once you've opened a file, you can use the FILE pointer to let the compiler perform input and output functions on the file. fopen Modes FILE *fopen(const char *filename, const char *mode); The allowed modes for fopen are as follows: r: open for reading. w: open for writing (file need not exist). a: open for appending (file need not exist). r+: open for reading and writing, start at beginning. w+: open for reading and writing (overwrite file). a+: open for reading and writing (append if file exists). Note: It's possible for fopen to fail even if your program is perfectly correct: you might try to open a file specified by the user, and that file might not exist (or it might be writeprotected). In those cases, fopen will return NULL pointer. fclose When you're done working with a file, you should close it using the function. int fclose(file *a_file);
3 fclose returns zero if the file is closed successfully. Reading From File fscanf: Reads formatted input from a stream. int fscanf(file *stream, const char *format,...) getline: It is the recommended way to read lines from a stream. ssize_t getline (char **lineptr, size_t *n, FILE *stream) This function reads an entire line from stream, storing the text (including the newline and a terminating null character) in a buffer and storing the buffer address in *lineptr. Before calling getline, you should place in *lineptr the address of a buffer *n bytes long, allocated with malloc.
4 If you set *lineptr to a NULL pointer, and *n to zero, before the call, then getline allocates the initial buffer for you by calling malloc. When getline is successful, it returns the number of characters read (including the newline, but not including the terminating null). If an error occurs or end of file is reached without any bytes read, getline returns -1.
5 Note: If you need to convert String to integer, you can use atoi function. Writing to a File int num = atoi(s); fprintf: Sends formatted output to a stream. int fprintf(file *stream, const char *format,...)
6 Structures and typedef Structures In the C language structures are used to group together different types of variables under the same name. For example you could create a structure telephone : which is made up of a string (that is used to hold the name of the person) and an integer (that is used to hold the telephone number). Declaration With the declaration of the structure you have created a new type, called telephone. Access struct telephone { char *name; int number; }; To access the members of the structure telephone, you must use a dot between the structure name and the variable name.
7 #include<stdio.h> struct telephone{ char *name; int number; }; int main(){ struct telephone index; index.name = "Haneen"; index.number = 12345; printf("name: %s\n", index.name); printf("telephone number: %d\n",index.number); return 0; } Type definitions and Structures Type definitions make it possible to create your own variable types. e.g., To define a new data type as a pointer to an integer. typedef int *int_ptr; It is also possible to use type definitions with structures, to save us from always having to type 'struct SomeStruct'. #include<stdio.h> typedef struct telephone{ char *name; int number; }TELEPHONE; int main(){ TELEPHONE index; index.name = "Haneen"; index.number = 12345; printf("name: %s\n", index.name); printf("telephone number: %d\n",index.number); return 0; }
8 Pointer to Structures If you want a pointer to a structure you have to use the -> (infix operator) instead of a dot. Queues implementation using Singly Linked List in C
9
10
11 Best Wishes