Silberschatz and Galvin  1999 1.1 C Programming Language Kingdom of Saudi Arabia Ministry of Higher Education Al-Majma’ah University College of Education.

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

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.
FILES Files types and operations. Files Files are used to store data Data can be Text (ASCII only: 0  127) Binary (Full range: 0  256) Each file resides.
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.
Guide To UNIX Using Linux Third Edition
COMP1170 Midterm Preparation (March 17 th 2009) Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education.
C Programming. C vs C++ C syntax and C++ syntax are the same but... C is not object oriented * There is no string class * There are no stream objects.
Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,
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.
C Basic File Input/Output Manipulation C Programming – File Outline v File handling in C - opening and closing. v Reading from and writing to files.
Lone Leth Thomsen Input / Output and Files. April 2006Basis-C-8/LL2 sprintf() and sscanf() The functions sprintf() and sscanf() are string versions of.
CP104 Introduction to Programming File I/O Lecture 33 __ 1 File Input/Output Text file and binary files File Input/output File input / output functions.
Chapter 13 Programming in the Large Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. TA: 鄭筱親 陳昱豪.
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.
File Handling In C By - AJAY SHARMA. We will learn about- FFile/Stream TText vs Binary Files FFILE Structure DDisk I/O function OOperations.
Introduction to Programming Using C Files. 2 Contents Files Working with files Sequential files Records.
CPS120: Introduction to Computer Science Lecture 14 Functions.
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:
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting Files & Directories.
CNG 140 C Programming (Lecture set 10) Spring Chapter 10 Data Files.
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:
Manish K Parmar PGT (CS) K V VVNagar Thursday, December 24, 2015 Lesson on USER DEFINED FUNCTION IN C++ Presented by Manish K Parmar PGT Computer Science.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Preprocessor Midterm Review Lecture 7 Feb 17, 2004.
Functions  A Function is a self contained block of one or more statements or a sub program which is designed for a particular task is called functions.
C Programming Lecture 12 : File Processing
GAME203 – C Files stdio.h C standard Input/Output “getchar()”
The Preprocessor Directives Introduction Preprocessing – Occurs before program compiled Inclusion of external files Definition of symbolic constants.
1 Object-Oriented Programming -- Using C++ Andres, Wen-Yuan Liao Department of Computer Science and Engineering De Lin Institute of Technology
1 CSC103: Introduction to Computer and Programming Lecture No 27.
Silberschatz and Galvin  C Programming Language Kingdom of Saudi Arabia Ministry of Higher Education Al-Majma’ah University College of Education.
Chapter 7: Repetition Structure (Loop) Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills.
Files A collection of related data treated as a unit. Two types Text
CSCI N305: C Programming Copyright ©2006  Department of Computer & Information Science File Handling in C.
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.
C Programming Day 2. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Union –mechanism to create user defined data types.
Silberschatz and Galvin  C Programming Language Decision making in C Kingdom of Saudi Arabia Ministry of Higher Education Al-Majma’ah University.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Chapter 4.
Lecture 11 File input/output
Chapter 7 Text Input/Output Objectives
File I/O.
Module 4 Functions – function definition and function prototype.
Prof: Dr. Shu-Ching Chen TA: Samira Pouyanfar Hector Cen Fall 2017
Plan for the Day: I/O (beyond scanf and printf)
BY GAWARE S.R. COMPUTER SCI. DEPARTMENT
CS111 Computer Programming
CSE1320 Files in C Dr. Sajib Datta
CSE1320 Files in C Dr. Sajib Datta
Chapter 5 - Functions Outline 5.1 Introduction
Arrays, For loop While loop Do while loop
CSE1320 Files in C Dr. Sajib Datta
Topics Introduction to File Input and Output
Manipulating File IO in Visual C++
Beginning C Lecture 11 Lecturer: Dr. Zhao Qinpei
C Preprocessor(CPP).
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
FILE HANDLING IN C.
Text and Binary File Processing
File Input and Output.
File Handling.
Topics Introduction to File Input and Output
C Preprocessing File I/O
C Preprocessor Seema Chandak.
FILE handeling.
Topics Introduction to File Input and Output
C Language B. DHIVYA 17PCA140 II MCA.
CSc 352 File I/O Saumya Debray Dept. of Computer Science
Presentation transcript:

Silberschatz and Galvin  C Programming Language Kingdom of Saudi Arabia Ministry of Higher Education Al-Majma’ah University College of Education - Zulfi Computer Department المملكة العربية السعودية وزارة التعليم العالي جامعة المجمعة كلية التربية بالزلفي قسم الحاسب الآلي Teacher: Chafika LAABIDI OUNI WANNASSI

Silberschatz and Galvin  C loops: definition When you need to execute a block of code several number of times. In general, statements are executed sequentially: The first statement in a function is executed first, followed by the second, and so on. A loop statement allows us to execute a statement or group of statements multiple times and following is the general form of a loop statement in most of the programming languages:

Silberschatz and Galvin  Definition (cont)

Silberschatz and Galvin  Types C programming language provides the following types of loop to handle looping requirements. Loop TypeDescription while loop Repeats a statement or group of statements while a given condition is true. It tests the condition before executing the loop body. for loop Execute a sequence of statements multiple times and abbreviates the code that manages the loop variable. do...while loop Like a while statement, except that it tests the condition at the end of the loop body nested loops You can use one or more loop inside any another while, for or do..while loop.

Silberschatz and Galvin  While loop

Silberschatz and Galvin  While loop

Silberschatz and Galvin  For loop

Silberschatz and Galvin  For loop

Silberschatz and Galvin  Do while loop

Silberschatz and Galvin  Do while loop

Silberschatz and Galvin  Header Files A header file is a file with extension.h which contains C function declarations and macro definitions and to be shared between several source files. There are two types of header files: the files that the programmer writes and the files that come with your compiler.

Silberschatz and Galvin  Header Files (cont) You request the use of a header file in your program by including it, with the C preprocessing directive #include like you have seen inclusion of stdio.h header file, which comes along with your compiler.

Silberschatz and Galvin  How to Divide a Program between Several Files Where a function is spread over several files, each file will contain one or more functions. One file will include main while the others will contain functions which are called by others. These other files can be treated as a library of functions

Silberschatz and Galvin  The advantages of this are: The object can easily be re-used in other programs. All related functions are stored together. Later changes to the object require only one file to be modified. How to Divide a Program between Several Files

Silberschatz and Galvin  Include Syntax Both user and system header files are included using the preprocessing directive #include. It has following two forms: #include This form is used for system header files. It searches for a file named file in a standard list of system directories.

Silberschatz and Galvin  Header Files (cont) #include "file" This form is used for header files of your own program. It searches for a file named file in the directory containing the current file.

Silberschatz and Galvin  C – Recursion Recursion is the process of repeating items in a self-similar way. Same applies in programming languages as well where if a programming allows you to call a function inside the same function that is called recursive call of the function as follows.

Silberschatz and Galvin  C – Recursion (cont) The C programming language supports recursion, i.e., a function to call itself. But while using recursion, programmers need to be careful to define an exit condition from the function, otherwise it will go in infinite loop. Recursive functions are very useful to solve many mathematical problems like to calculate factorial of a number.

Silberschatz and Galvin  File I/O A file represents a sequence of bytes, does not matter if it is a text file or binary file. C programming language provides access on high level functions as well as low level (OS level) calls to handle file on your storage devices.

Silberschatz and Galvin  Opening Files You can use the fopen( ) function to create a new file or to open an existing file, this call will initialize an object of the type FILE, which contains all the information necessary to control the stream. Following is the prototype of this function call: FILE *fopen( const char * filename, const char * mode );

Silberschatz and Galvin  Opening Files Filename is string literal, which you will use to name your file and access mode can have one of the following values:

Silberschatz and Galvin  Opening Files ModeDescription rOpens an existing text file for reading purpose. w Opens a text file for writing, if it does not exist then a new file is created. Here your program will start writing content from the beginning of the file. a Opens a text file for writing in appending mode, if it does not exist then a new file is created. Here your program will start appending content in the existing file content. r+Opens a text file for reading and writing both. w+ Opens a text file for reading and writing both. It first truncate the file to zero length if it exists otherwise create the file if it does not exist. a+ Opens a text file for reading and writing both. It creates the file if it does not exist. The reading will start from the beginning but writing can only be appended.

Silberschatz and Galvin  Closing a File To close a file, use the fclose( ) function. The prototype of this function is: int fclose( FILE *fp ); The fclose( ) function returns zero on success, or EOF if there is an error in closing the file.

Silberschatz and Galvin  Writing a File int fputs( const char *s, FILE *fp ); The function fputs() writes the string s to the output stream referenced by fp. It returns a non-negative value on success, otherwise EOF is returned in case of any error. You can use int fprintf(FILE *fp,const char *format,...) function as well to write a string into a file.

Silberschatz and Galvin  Writing a File Example: #include main() { FILE *fp; fp = fopen("/tmp/test.txt", "w+"); fprintf(fp, "This is testing for fprintf...\n"); fputs("This is testing for fputs...\n", fp); fclose(fp); }

Silberschatz and Galvin  Reading a File char *fgets( char *buf, int n, FILE *fp ); The functions fgets() reads up to n - 1 characters from the input stream referenced by fp. It copies the read string into the buffer buf, appending a null character to terminate the string.

Silberschatz and Galvin 