• work similar to getc and putc • used to read and write integer values • useful when we deal with only integer data putw(integer, fp); getw(fp); fprintf() and fscanf() functions These can handle a grup of mxed data simultaneously work identical to scanf () and printf() fprintf(fp,”control string”,list); fp – file pointer associated with a file opened for writing control string – contains output specifications for the items in the list (var, constants, strings) Ex: printf(f1, “%s %d %f”, name, age, 7.5); fscanf() fscanf(fp,”control string”, list); This would cause reading of items in the list from the file specified by fp, according to the specifications contained in the control string Ex: scanf(f2, “%s %d”, item, quntity); Note: 1. scanf, fscanf return the number of item that are successfully read 2. when end of the file is reached, it returns EOF Error Handling during i/o operations Typical error situations during file operations: 1. Trying to read beyond the EOF mark 2. Device overflow 3. Trying to use a file that has not been opened 4. Trying to perform an operation on a file, when the file is opened for another type of operation 5. opening a file with an invalid filename 6. Attemting to write to a write-protected file Random Access to files (fseek, ftell, rewind) To access a particular part of a file ftell(): takes a file pointer and returns a number of type long, that corresponds to the current position in the file, that can be used later. n = ftell(fp); n – relative offset (in bytes) of the current position (means n bytes have already been read or written) rewind(): takes a file pointer and resets the position to the start of the file. rewind(fp); n=ftell(fp); would assign 0 to n because the file position has been set to the start. This function helps us in reading a file more than once, without having to close and open again. Note: Whenever a file is opened for reading or writing, a rewind is done implicitly fseek() It is used to move the file position to a desired location within the file. fseek(file_ptr, offset, position); file_ptr: pointer to the file concerned offset: a number or variable of type long that specifies the number of bytes to be moved from the location specified by ‘position’ offset may be position(move forward) or negative (move backward) position: an integer number that can take one of 3 values: (0 – beginning of file, 1 – current position, 2 – end of file) fseek(file_ptr, offset, position); Statement Meaning fseek(fp,0L,0) Go to start (rewind) fseek(fp,0L,1) Stay at the current position fseek(fp,0L,2) Go to end of file fseek(fp,m,0) Move to (m+1)th byte in the file fseek(fp,m,1) Go forward by m bytes fseek(fp,-m,1) Go backward by m bytes from current position fseek(fp,-m,2) Go backward by m bytes from end Command Line Arguments CLA is a parameter supplied to a program when the program is invoked This parameter may represent a filename the program should process Ex: C:\ PROGRAM X_FILE Y_FILE copies contents of X_FILE to Y_FILE PROGRAM is the file name where executable code of the program is stored This eliminates the need for program to request the user to enter the filenames during execution Command Line Arguments main() function can also take arguments like other functions sum = add(a,b); • main() can take two arguments called argc and argv • information contained in the command line is passed on to the program through these arguments when main is called by the system argc: is a argument counter that counts the number of arguments on the command line argv: is an argument vector and represents an array of character pointers that point to the command line arguments; size of this array is equal to value of ‘argc’ C:\ PROGRAM X_FILE Y_FILE for this case: argc = 3 argv[0]PROGRAM argv[1] X_FILE argv[2] Y_FILE In order to access the command line arguments, we must declare man as: main(int argc, char *argv[]) { ------- } The first parameter of command lin is always the program name and therefore arg[0] always represents the program name Additional info • `dir`: Lists files and directories in the current directory. • `cd`: Changes the current directory. • `md`: Creates a new directory. • `rd`: Removes (deletes) a directory. • `copy`: Copies files. • `del`: Deletes files. • `ren`: Renames files