Presentation is loading. Please wait.

Presentation is loading. Please wait.

RECURSION Recursion is a process where a function calls itself. main()

Similar presentations


Presentation on theme: "RECURSION Recursion is a process where a function calls itself. main()"— Presentation transcript:

1 RECURSION Recursion is a process where a function calls itself. main()
{ printf(“RECURSION”); main(); } Recursive function can be effectively used to solve problems where solution is expressed in term of successively applying the same solution to subsets of the problem.

2 #include <stdio.h>
int factorial(int); //function prototype main() { int a; printf("enter a num\n"); scanf("%d",&a); int fact=factorial(a); printf("The factorial of %d is: %d", a,fact); getch(); } int factorial(int a) int fact; if (a == 1) return 1; //recursive step of the factorial else fact= a*factorial(a-1); //calls the factorial function. //The above calls the factorial function from within the factorial function. return (fact);

3 File Management in C

4 Console oriented Input/Output
Console oriented – use terminal (keyboard/screen) scanf(“%d”,&i) – read data from keyboard printf(“%d”,i) – print data to monitor Suitable for small volumes of data Data lost when program terminated Large data volumes Need for flexible approach to store/retrieve data Concept of files File is a set of records/group of related data that can be accessed through the set of library functions.

5 Steps for file operation
Naming Opening Reading Writing Closing

6 File handling functions
FUNCTION NAME OPERATION fopen() creats a new file for use open an existing file for use fclose() closes a file which has been opened for use getc() reads a character from a file putc() writes a character to a file fprintf() writes a set of data values to a file fscanf() reads a set of data values from a file getw() reads an integer from a file putw() writes an integer to a file fseek() sets the position to a desired point in the file ftell() gives the current position in the file rewind() sets the position to the beginning of the file

7 Defining and opening file
Filename Data structure Data structure of a file is defined as FILE in the library of standard I/O function definition. File should be declared as FILE before used. 3. Purpose (e.g. reading, writing, appending)

8 General format for opening file
FILE *fp; /*variable fp is pointer to type FILE*/ fp = fopen(“filename”, “mode”); Eg: FILE *p1,*p2; p1=fopen(“data”,”r”); p2=fopen(“result”,”w”); Mode of files r open the file for reading only w open the file for writing only a open the file for appending (adding) data to it

9 fopen() performs the following task:
Writing mode if file already exists then contents are deleted, else new file with specified name created Appending mode if file already exists then file opened with contents safe else new file created Reading mode if file already exists then opened with contents safe else error occurs. Additional modes of operation r the existing file is opened for both reading and writing w+ a+

10 Closing a file Syntax: fclose(file_pointer); Example: FILE *p1, *p2;
File must be closed as soon as all operations on it completed Syntax: fclose(file_pointer); Example: FILE *p1, *p2; p1 = fopen(“INPUT”, “r”); p2 =fopen(“OUTPUT”, “w”); …….. fclose(p1); fclose(p2);

11 Input/Output operations on files
C provides several different functions for reading/writing getc() – read a character putc() – write a character fprintf() – write set of data values fscanf() – read set of data values getw() – read integer putw() – write integer

12 getc() and putc() Handle one character at a time like getchar() and putchar() syntax: putc(c,fp1); c : a character variable fp1 : pointer to file opened with mode w syntax: c = getc(fp2); fp2 : pointer to file opened with mode r file pointer moves by one character position after every getc() and putc() After entering the input data via keyboard, the program writes it character by Character, to the given file. The end of the data is indicated by entering an EOF character, which is control-Z the file input is closed at this signal.

13 #include <stdio.h>
main() { FILE *f1; char c; printf("DATA INPUT \n"); f1= fopen("INPUT", “w"); /* open file for writing */ while((c=getchar()) != EOF) /*get char from keyboard until CTL-Z*/ putc(c,f1); /*write a character to INPUT */ fclose(f1); /* close INPUT */ printf("DATA OUTPUT \n"); f1=fopen("INPUT", "r"); /* reopen file */ while((c=getc(f1))!=EOF) /*read character from file INPUT*/ printf("%c", c); /* print character to screen */ fclose(f1); getch(); } /*end main */


Download ppt "RECURSION Recursion is a process where a function calls itself. main()"

Similar presentations


Ads by Google