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.

Slides:



Advertisements
Similar presentations
Lecture 20 Arrays and Strings
Advertisements

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.
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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Introduction Data files –Can be created, updated,
 2007 Pearson Education, Inc. All rights reserved C File Processing.
Chapter 11 C File Processing Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Structures and Unions Chapter 6. Structure A structure is an aggregate data type  Composed of two or more related variables called member/field/element.
Guide To UNIX Using Linux Third Edition
Console and File I/O - Basics Rudra Dutta CSC Spring 2007, Section 001.
Chapter 18 I/O in C. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Standard C Library I/O commands.
A First Book of ANSI C Fourth Edition Chapter 10 Data Files.
 2007 Pearson Education, Inc. All rights reserved C File Processing.
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.
Programming Practice Introduction Tree Operations. Binary Search Tree. File Processing Create, read, write and update files. Sequential.
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.
 2007 Pearson Education, Inc. All rights reserved C File Processing.
Programming Languages -1 (Introduction to C) files Instructor: M.Fatih AMASYALI
1 Lecture09: File I/O 5/6/2013 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
Text and Binary File Processing 程式設計 潘仁義 CCU COMM.
1. Introduction File Declaration and Initialization Creating and Opening File Closing File EOF Reading from and Writing into a File Extra : Random Access.
File IO and command line input CSE 2451 Rong Shi.
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee C Language Part 4.
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:
1 Lecture09: File I/O 11/19/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
Introduction to Systems Programming (CS 0449) C Preprocessing Makefile File I/O.
Chapter 11: Data Files and File Processing Files and streams Creating a sequential access file Reading data from a sequential access file Using fgetc()
Chapter 11 File Processing. Objectives In this chapter, you will learn: –To be able to create, read, write and update files. –To become familiar with.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Introduction Introduce some standard library functions.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 11 – File Processing Outline 11.1Introduction.
Chapter 12 Files (reference: Deitel ’ s chap 11) chap8.
CNG 140 C Programming (Lecture set 10) Spring Chapter 10 Data Files.
FILE IO in ‘C’ by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
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:
chap8 Chapter 12 Files (reference: Deitel ’ s chap 11)
C File Processing. Objectives To be able to create, write and update files. To become familiar with sequential access file processing. To become familiar.
GAME203 – C Files stdio.h C standard Input/Output “getchar()”
ME-2221 COMPUTER PROGRAMMING Lecture 18 FILE OPERATIONS Department of Mechanical Engineering A.H.M Fazle Elahi Khulna University of engineering & Technology.
Chapter 12 Files (reference: Deitel ’ s chap 11) chap8.
1 CSC103: Introduction to Computer and Programming Lecture No 28.
1 CSC103: Introduction to Computer and Programming Lecture No 27.
 2007 Pearson Education, Inc. All rights reserved. 1 C File Processing.
FILES IN C. File Operations  Creation of a new file  Opening an existing file  Reading from a file  Writing to a file  Moving to a specific location.
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.
UniMAP SemI-11/12EKT120: Computer Programming1 Files.
 2007 Pearson Education, Inc. All rights reserved C File Processing.
Silberschatz and Galvin  C Programming Language Kingdom of Saudi Arabia Ministry of Higher Education Al-Majma’ah University College of Education.
11 C File Processing.
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.
Lecture 11 File input/output
TMF1414 Introduction to Programming
Chapter 18 I/O in C.
File I/O.
CS111 Computer Programming
What you need for the 1st phase of project
File I/O We are used to reading from and writing to the terminal:
Chapter 11 – File Processing
Beginning C Lecture 11 Lecturer: Dr. Zhao Qinpei
Text and Binary File Processing
File Input and Output.
File Handling.
Fundamental of Programming (C)
C Input / Output Prabhat Kumar Padhy
C Preprocessing File I/O
Chapter 12: File I/O.
EPSII 59:006 Spring 2004.
A First Book of ANSI C Fourth Edition
Professor Jodi Neely-Ritz University of Florida
File I/O We are used to reading from and writing to the terminal:
Presentation transcript:

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 –single piece of memory that is shared The variables that share the memory may be of the same type or different types Only one variable in the union can be in use at any point of time

3 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Size of the union is fixed at compile time and it’s large enough to accommodate the largest element in the union Accessing elements of a union is similar to accessing elements of a structure Union… union encrypt { int iIntVar; char acArrayVar[2]; }uEnc; uEnc.iIntVar=10; uEnc.acArrayVar[0]=‘a’; uEnc.acArrayVar[1]=‘b’;

4 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Lists - Stacks Stack is a Linear data structure in which addition of new element or deletion of existing element always takes place at the same end The end at which addition and deletion takes place is called as the top of the stack The element which is entered most recently is the first to be removed from the stack i.e. Last in First Out (LIFO)

5 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Lists - Stacks Illustration of a Stack Stack empty 10 pushed on the stack 20 pushed on the stack 30 pushed on the stack 30 popped from the stack Top

6 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Lists - Queues Queue is a data structure in which addition of new element is done at the rear of the queue and deletion of an element is done from the front of the queue. The first element to enter the queue is the first one to go out i.e. First in First out (FIFO). Queue is a Linear Data structure

7 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Lists - Queues Illustration of a Queue queue with one element 20 added to the queue 30 added to the queue 10 removed from the queue Front Rear Front Rear Front Rear

8 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Command Line Arguments What are command line arguments? echo command to display “Hello World” in UNIX > echo Hello World The input to echo are command line arguments –Hello World

9 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Command Line Arguments… In a C program the function that accepts command line arguments is the main() function To write the main() function to accept command-line arguments the syntax is: main( int argc, char *argv[] ) { … }

10 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Command Line Arguments… argc is the number of elements on the command line i.e. the command and the arguments Example: > echo Hello World –argc will have the value 3 for the command: if argc is 1, then there are no arguments only the command argv[] is an array of all the elements on the command line Example: > echo Hello World –argv[0] points to the string “echo” –argv[1] points to the string “Hello” –argv[2] points to the string “World” –argv[3] is NULL value Note: “echo” is the command and the rest are arguments

11 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Command Line Arguments… /* Program to echo arguments */ #include main(int argc, char *argv[]) { /* Increment pointer to point to arguments*/ argv++; /* Print the arguments on screen */ while (*argv != NULL) { printf("%s ", *argv); argv++; } printf("\n"); }

12 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Need for File I/O Data that outlives the program –needs to be stored –in a permanent storage –so that it can be referred to later Example –word processing application might save the text in a linked list or some other data structure when the application is running, but when the application is closed the contents of the linked list need to stored in a file

13 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Need for File I/O C has several functions for I/O –available as I/O library The functions in the I/O library –Uses the corresponding I/O function’s of the underlying operating system

14 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Classification of File I/O functions

15 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Understanding Streams The C Language I/O system provides a consistent interface to the programmer independent of the actual I/O device used I/O streams provides a level of abstraction between the programmer and the I/O device Stream is a logical connection to a file, screen, keyboard and other I/O devices

16 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 C Program Input Stream Disk File Output Stream Understanding Streams…

17 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 There are 2 types of streams –Text stream (stream of ASCII characters) –Binary stream (stream of bytes) C has an inbuilt structure which represents a the logical stream –FILE –This structure has a buffer –a variable of the structure FILE has to be created for interacting with the actual storage The FILE structure is nothing but a stream of characters/bytes which acts as a logical representative of the actual file Understanding Streams…

18 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 File Opening Modes ModeDescription “r”Searches file. If the file exists, loads it into memory and sets up a pointer which points to the first character in it. If the file doesn’t exists it returns a null. Operations possible - reading from the files “w”Searches file. If the file exists, contents are overwritten. If the file doesn’t exists a new file is created. Returns a null if unable to open the file. Operations possible - writing to the file. “a”Searches file. If the file exists, loads it into memory and sets up a pointer which points to the end of the file. If the file doesn’t exists a new file is created. Returns a null if unable to open the file. Operations possible - appending new contents at the end of the file.

19 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 File Opening Modes… ModeDescription “r+”Same as “r” Operations possible – reading existing contents, writing new contents, modifying existing contents of the file. “w+”Same as “w” Operations possible – writing new contents, reading them back and modifying existing contents of the file. “a+”Same as “a” Operations possible – reading existing contents, appending new contents to the end of file, cannot modify existing contents of the file.

20 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 High Level I/O functions Functions to open a File. –fopen() is the function to open a file Prototype: FILE *fopen (char *name, char *mode); –first argument specifies the file name and –second argument specifies a mode in which the file has to be opened

21 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 High Level I/O functions… fopen() –If the file is found at the specified location then fopen() creates a variable of type FILE structure and returns a pointer to it. This pointer is termed as a file pointer –If the file could not be opened then fopen() returns a NULL

22 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Functions to close a File –fclose() is the counterpart of fopen() Prototype: int fclose( FILE *stream ); –The argument to fclose() is the file pointer returned by fopen() –fclose() returns 0 if the file was closed successfully otherwise it returns -1 High Level I/O functions…

23 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Functions for reading characters. –fgetc() is a function to read characters from a file Prototype: int fgetc( FILE *stream ); –The argument to fgetc() is the FILE pointer returned by fopen() –fgetc() returns the character read as an int or returns EOF (end of file) to indicate an error EOF is a macro which is #defined to -1 High Level I/O functions…

24 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Functions for writing characters. –fputc() is a function to write characters to a file Prototype: int fputc( int c, FILE *stream ); –The first argument to fputc() is the character to be written to the file –The second argument to fputc() is the FILE pointer returned by fopen() –fputc() returns the character written to the file It returns a EOF to indicate an error High Level I/O functions…

25 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Functions for reading lines –fgets() is a function to read lines from a file Prototype: char *fgets( char *string, int n, FILE *stream ); –first argument is a character pointer which points to a buffer which will receive the line read from the file –second argument is the size of the buffer –third argument to fgets() is the FILE pointer returned by fopen() –fgets() returns a non negative number if successful NULL to indicate an error or end of file High Level I/O functions…

26 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Functions for writing lines –fputs() is a function to write lines to a file Prototype: int fputs( const char *string, FILE *stream ); –first argument to fputc() is a character pointer which points to a buffer which holds the string to be written to the file –second argument to fputc() is the FILE pointer returned by fopen() –fputs() returns a non negative number if successful It returns a EOF to indicate an error High Level I/O functions…

27 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Functions for reading blocks of data –A block will have records of the same type. Example of record could be employee record which stores employee details like employee #, Name date of joining etc. –fread() is a function to read blocks of data from a file Prototype: size_t fread( void *buffer, size_t size, size_t count, FILE *stream ); first argument is a void pointer, which points to a buffer that receives the blocks read from the file second argument is the size of each record third argument is the number of records to be read fourth argument is the FILE pointer returned by fopen() –fread() returns the number of full items actually read maybe less than count if an error occurs or if the end of the file is encountered before reaching count High Level I/O functions…

28 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Functions for writing blocks of data –fwrite() is a function to write blocks of data to a file –Prototype: size_t fwrite( const void *buffer, size_t size, size_t count, FILE *stream ); first argument is a void pointer to a constant, which points to a buffer that has the blocks of data, to be written to the file second argument is the size of each record third argument is the number of records to be written fourth argument is the FILE pointer returned by fopen() –fwrite() returns the number of full items actually written which may be less than count if an error occurs High Level I/O functions…

29 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 High Level functions can be categorized as –text mode –binary mode so far we have seen the text mode functions. There are 3 main areas where text and binary mode files are differ –Handling new lines –Representation of End of File –Storage of numbers High Level I/O functions…

30 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Behavior of newlines if a file is opened –In a text mode operation every newline character is converted –to a carriage return – linefeed combination when it is written to the disk. Likewise the carriage return – linefeed combination –is converted to a newline character when it is read from the disk –In a binary mode operation the above mentioned conversions don’t take place High Level I/O functions…

31 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 End of File detection for a file opened –In a text mode operation a special character, whose ASCII value 26, is inserted after the last character in the file to mark the end of file. –In a binary mode operation there is no special character to indicate end of file –The binary mode files keep track of the end of file from the number of characters present in the directory entry of the file –A file opened for writing in binary mode should be opened for reading in binary mode only High Level I/O functions…

32 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Storage of numbers for a file opened –In text mode operation, integers & floats are stored as strings of characters Example: –12345 will occupy 5 bytes because individual digits are stored as characters occupying one byte each –In a binary mode operation integers and floats are stored exactly in the same way as they are stored in memory Example: –integers will occupy 2 bytes and floats will occupy 4 bytes High Level I/O functions…

33 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Functions for random access of file contents –fseek() is a function to move the file pointer to a specified location –Prototype: int fseek( FILE *stream, long offset, int origin ); first argument is the FILE pointer returned by fopen() second argument specifies the number of offset bytes from the initial position. third argument specifies the initial position. It can be one of the following: –SEEK_CUR Current position of file pointer –SEEK_END End of file –SEEK_SET Beginning of file –fseek() returns Zero if successful otherwise, it returns a nonzero value –The value of offset can be a negative value also High Level I/O functions…

34 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Function for returning the current position of the file pointer. –ftell() is a function to get the current position of a file pointer Prototype: long ftell( FILE *stream ); –The argument to ftell() is the FILE pointer returned by fopen() –On error, ftell() returns –1L High Level I/O functions…

35 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Function for repositioning the file pointer to the beginning of a file. –rewind() repositions the file pointer to the beginning of a file Prototype: void rewind( FILE *stream ); –The argument to rewind() is the FILE pointer returned by fopen() High Level I/O functions…

36 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Thank You!