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

Slides:



Advertisements
Similar presentations
File Management in C. What is a File? A file is a collection of related data that a computers treats as a single unit. Computers store files to secondary.
Advertisements

Week 5 Part I Kyle Dewey. Overview Exam will be back Thursday New office hour More on functions File I/O Project #2.
I/O means Input and Output. One way: use standard input and standard output. To read in data, use scanf() (or a few other functions) To write out data,
Files in C Rohit Khokher.
BITS Pilani, Pilani Campus TA C252 Computer Programming - II Vikas Singh File Handling.
File Management in C. A file is a collection of related data that a computers treats as a single unit. File is a collection of data stored permanently.
Files in C Rohit Khokher. Files in C Real life situations involve large volume of data and in such cases, the console oriented I/O operations pose two.
Chapter 11: Data Files & File Processing In this chapter, you will learn about Files and streams Creating a sequential access file Reading data from a.
File Management in C. Console oriented Input/Output Console oriented – use terminal (keyboard/screen) scanf(“%d”,&i) – read data from keyboard printf(“%d”,i)
CSCI 171 Presentation 12 Files. Working with files File Streams – sequence of data that is connected with a specific file –Text Stream – Made up of lines.
File I/O.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 6P. 1Winter Quarter I/O in C Lecture 6.
Console and File I/O - Basics Rudra Dutta CSC Spring 2007, Section 001.
C - Input & Output When we are saying Input that means to feed some data into program. This can be given in the form of file or from command line. C programming.
V-1 University of Washington Computer Programming I File Input/Output © 2000 UW CSE.
Files Programs and data are stored on disk in structures called files Examples Turbo C++ - binary file Word binary file lab1.c - text file lab1.data.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students C File Input and Output Checking input for errors.
1 Homework Introduction to HW7 –Complexity similar to HW6 –Don’t wait until last minute to start on it File Access will be needed in HW8.
N305: C Programming Copyright ©2006  Department of Computer & Information Science File Handling in C.
Lone Leth Thomsen Input / Output and Files. April 2006Basis-C-8/LL2 sprintf() and sscanf() The functions sprintf() and sscanf() are string versions of.
File Handling Spring 2013Programming and Data Structure1.
22. FILE INPUT/OUTPUT. File Pointers and Streams Declarations of functions that perform file I/O appear in. Each function requires a file pointer as a.
Introduction to Programming Using C Files. 2 Contents Files Working with files Sequential files Records.
File IO and command line input CSE 2451 Rong Shi.
CSEB114: Principle of programming Chapter 11: Data Files & File Processing.
ECE 103 Engineering Programming Chapter 44 File I/O Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103 material developed.
1 File Handling. 2 Storage seen so far All variables stored in memory Problem: the contents of memory are wiped out when the computer is powered off Example:
Chapter 7 Files By C. Shing ITEC Dept Radford University.
24-2 Perform File I/O using file pointers FILE * data-type Opening and closing files Character Input and Output String Input and Output Related Chapter:
Structured Programming Approach Module VIII - Additional C Data Types File Handling Prof: Muhammed Salman Shamsi.
GAME203 – C Files stdio.h C standard Input/Output “getchar()”
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the basic properties and characteristics of external files ❏ To.
Files. FILE * u In C, we use a FILE * data type to access files. u FILE * is defined in /usr/include/stdio.h u An example: #include int main() { FILE.
Connecting to Files In order to read or write to a file, we need to make a connection to it. There are several functions for doing this. fopen() – makes.
Silberschatz and Galvin  C Programming Language Kingdom of Saudi Arabia Ministry of Higher Education Al-Majma’ah University College of Education.
Chapter 7 Text Input/Output Objectives
Chapter 7 Text Input/Output Objectives
Chapter 22 – part a Stream refer to any source of input or any destination for output. Many small programs, obtain all their input from one stream usually.
University of Washington Computer Programming I
Chapter 7 Text Input/Output Objectives
File Access (7.5) CSE 2031 Fall July 2018.
File I/O.
UNIT 5 POINTERS AND FILES
File Handling in C.
Plan for the Day: I/O (beyond scanf and printf)
CS111 Computer Programming
CSE1320 Files in C Dr. Sajib Datta
CSE1320 Files in C Dr. Sajib Datta
What you need for the 1st phase of project
File Handling in C.
FILE HANDLING.
File Management in C.
CSE1320 Files in C Dr. Sajib Datta
I/O in C Lecture 6 Winter Quarter Engineering H192 Winter 2005
CSC215 Lecture Input and Output.
Programming and Data Structure
File Input and Output.
File Handling.
File Access (7.5) CSE 2031 Fall January 2019.
File Handling in C.
File Access (7.5) CSE 2031 Fall February 2019.
C Input / Output Prabhat Kumar Padhy
Files.
File Handling in C.
Chapter 5 File Handling in C
Chapter 12: File I/O.
FILE handeling.
Module 12 Input and Output
ETE 132 Lecture 8 By Munirul Haque.
Professor Jodi Neely-Ritz University of Florida
Presentation transcript:

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.

#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);

File Management in C

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.

Steps for file operation Naming Opening Reading Writing Closing

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

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)

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

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+

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);

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

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.

#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 */