Académique Documents
Professionnel Documents
Culture Documents
# 1 What data types do the return values o the following functions belong to? a. fopen() b. fgetc() c. fscanf() # 2 Why is the file stdio.h included in programs that use disk files? # 3 What other file input statement is the following statement equivalent to? x = getc (stdin); /* x is char type */ # 4 What is the error in the following statement? fputc (x); */ x is char type */
NIIT
NIIT
NIIT
NIIT
into a character array) # 9 fprintf (ptr_to_file, %s:%s, sno, sname); or fprintf (ptr_to_file, %4s20s, sno, sname); # 10 No, fscanf() assumes that the field separator is a white-space.
NIIT
SPL SESSION
Objectives At the end of this session, you will be able to: Explain how C treats files Write file-handling programs in C using the following functions to open and close files: fopen() fclose() Write file-handling programs in C using the following function for inputs and outputs from files: fgetc() fgets() fscanf() fputc()
NIIT SEM Q/CPR/CR/SESSION 6/8/VER06/95
NIIT
FILE HANDLING IN C
File inputs-outputs is similar to input from/to the terminal Files are treated as streams of characters no concept of an indexed or a sequential file
Function are available for single character as well as multiple character input-output from/to files
NIIT
OPENING FILES
fopen() is used to open files Example #include < stdio.h> main () { FILE *ptr1, *ptr2, *ptr3; ptr1 = fopen (a.dat, r); /* r read mode */ ptr2 = fopen (b.dat, w); /* w write mode */ Ptr3 = fopen (z.dat, a); /* a append mode */ :: }
NIIT
*ptr3;
NIIT
NIIT
Additional parameter in both function appropriate file pointer while ((c = fgetc (ptr1)) ! = EOF) /*EOF is int type*/ fputc (c, ptr2); fgetc() return an integer value which is type cast into character before being assigned to variable EOF return value is equal to -1
NIIT
CLOSING FILES
fclose() is used to close files Example # include < stdio.h> main () { : : fclose (ptr1); fclose (ptr2); } Parameter in fclose() is file pointer Each file has to be closed by a separate fclose()
NIIT SEM Q/CPR/CR/SESSION 6/15/VER06/95
NIIT
NIIT
fgets()
fputs()
(similar to gets())
(similar to puts())
NIIT
NIIT
Additional parameter pointer to the file fscanf() returns number of variables that are actually assigned values after the read; in case of end-of-file return value EOF fscanf() assumes that field separator is any whitespace, I.e. blank character, a tab or a newline
NIIT
NIIT
NIIT
NIIT
NIIT
Offset** Offset From Current Position * New Position* (7-1) Current 11 17 Position
NIIT
NIIT
NIIT
NIIT
NIIT
NIIT
NIIT
SPL EXERCISE
# 1 A utility called hprint has to be written in C, which will allow a user to display, on screen, any number of lines from the beginning of any file. The user has to specify both the number and the file name on the command line in the following format: hprint number file name
The maximum line length is 80 characters. The program should handle possible errors.
NIIT
SPL EXERCISE
#2 Go through the following program called inpcopy.c and its error listing on compilation and then correct the program. 1 # include < stdio.h> 2 main () 3{ 4 file fp; 5 char c; 6 7 fp = fopen (file, w); 8 9 while ((c = fgetc (stdin)) != EOF) 10 fputc (c, fp);
NIIT SEM Q/CPR/CR/SESSION 6/36/VER06/95
SPL EXERCISE
11 12 fclose (fp); 13 } Error listing: inpcopy.c, line 4: file undefined inpcopy.c, line 4: syntax error inpcopy.c, line 7: fp undefined inpcopy.c, line 7: w undefined inpcopy.c, line 7: warning: illegal pointer / integer combination, op = inpcopy.c, line 9: c undefined
NIIT
NIIT
CLASSROOM EXERCISE
#1 One measure of programming skills in C is whether a programmer is able to simulate UNIX utilities, most of which have originally been written in C itself. As a step towards this, try to program a simplified version of the cut utility, called diffcut, that displays the first field from each record of a file, named on the command line. The utility should assume that the fields are separated by a blank character. The printing of the field from each record should be done through a function. (Assume that the maximum line length is 80 characters)
NIIT SEM Q/CPR/CR/SESSION 6/39/VER06/95
NIIT
CLASSROOM EXERCISE
#2 Modify the difficult utility so that it allows any field to be displayed from the file. The number of the field to be displayed should be accepted during execution.
NIIT
NIIT
NIIT