#include<stdio.h>/* Header files for the bellow functions */
1
FILE* fopen(char *fileName, char *mode);
Returns a FILE pointer associated with the file name or NULL if error occurs. Modes are r(read), w(write), a(append). There are three special file pointer; stdin(standard input), stdout(standard output), stderr(standard error).
1
intgetc(FILE *filePointer);
Returns next character from the file stream referred to by filePointer. It returns EOF if end of file or error occurs.
1
intputc(int c, FILE *filePointer);
Writes character c to the file referred to by filePointer. Retuens the character written or EOF if error occurs.
char *fgets(char *line, int MAX_CHAR, FILE *filePointer);
Reads the next line from the file referred to by filepointer into char array line. MAX_CHAR - 1 characters will be read. Returns pointer to the line or NULL if EOF or error occurs.
1
intfputs(char *line, FILE *filePointer);
Writes a string to a file. Returns EOF if error occurs, zero otherwise.
1
intremove(constchar *fileName);
Removes fileName from file system. Returns non zero if attempt fails
Rename oldFileName to newFileName. Returns non zero if attempt fails.
1
FILE* tmpfile(void);
Creates a temporary file of mode wb+. It will be removed automatically when the file is closed.
1
intfclose(FILE *filePointer);
Closes a file.
Error Handling
1 2
intferror(FILE *filePointer); /* Returns non zero if an error occurs */ intfeof(FILE *filePointer); /* Return non zero if eof occurs */
1
voidperror(FILE *filePointer, chars *s);
Prints interactive error message to filePointer if no argument specified Or s.
Low Level Access
1
#include<fcntl.h>/* Header file for the bellow functions */
1
intopen(char *name, int flags, int perms);
Returns an integer associated with the file, which is called a file descriptor. When shell runs a program three files are open automatically the standard input, standard output and standard error. Their associated file descriptors are 0, 1 & 2.
Here are some flags:
1 2 3
O_RDONLY - Open file in read only mode O_WRONLY - Open file in write only mode O_RDWR - Open file in read / write mode
Perm is the unix file permission specified as a 3 digit octal.
1
intcreate(char *name, int perms);
Creates a file using name and returns a file descriptor associated with it. Returns -1 if fails.
1
#include<unistd.h>/* Header file for the bellow functions */
1
intread(int fileDescriptor, char *buffer, int n);
Reads n bytes from a file associated with fileDescriptor into buffer array. Returns number of bytes transferred. A return value of zero indicates EOF and -1 indicates an error.
1
intwrite(int fileDescriptor, char *buffer, int n);
Writes n bytes from buffer array to a file associated with fileDescriptor. It returns number of bytes written.
1
longlseek(int fileDescriptor, long offset, int origin);
Set the current position of the file to offset which is taken relative to the location specified by origin. Orgigin can be 0, 1 or 2 to specify that offset is to be measured from the begaining, current position or end of the file.
1
intunlink(constchar *path);
Removes a file. Returns 0 upon success otherwise -1.
1
intclose(int fileDescriptor);
Closes a file.
User Defined Types
Structure
Creating a structure:
1 2 3 4 5
structpacket{ int size; double length; double breadth; };
intmain(){ /* Can be treated like regular variable */ enumTypet;
/* Value assignment can be one of the enum members */ t = INT;
/* Can be used directly without a enumerated variable */ if(t == INT){ printf("This is an integer.\n"); } elseif(t == DOUBLE){ printf("This is a double.\n"); } elseif(t == CHAR){ printf("This is a character variable.\n"); }
return0; }
Bit Fields
Bit field is used to pack several variables into a machine word. Bit fields are declared just like regular structures. But variable type have to be unsigned int and bit length of each variable is specified using : operator.
1 2 3 4 5 6
structflags{ unsignedint is_color:1// 1 bit length unsignedint is_sound:1// 1 bit length unsignedint is_open:1// 1 bit length unsignedint status:4// 4 bit length };
Typedefs
Typedefs are used to give an alias for a type.Structure:
1
typedef TYPE ALIAS
Example:
1
typedefdoubledist_t; // dist_t can be used instead of double