Session #5 File I/O Bit Masks, Fields & Bit Manipulations

Slides:



Advertisements
Similar presentations
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.
Advertisements

Chapter 11 C File Processing Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Bit Operations C is well suited to system programming because it contains operators that can manipulate data at the bit level –Example: The Internet requires.
A bit can have one of two values: 0 or 1. The C language provides four operators that can be used to perform bitwise operations on the individual bits.
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.
Applications with Random File Access CHAPTER 8. C.13 1 Where am I on the Binary File? » While a binary file is processed, it is a need to know current.
F28PL1 Programming Languages Lecture 7 Programming in C - 2.
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.
Lone Leth Thomsen Input / Output and Files. April 2006Basis-C-8/LL2 sprintf() and sscanf() The functions sprintf() and sscanf() are string versions of.
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.
 2007 Pearson Education, Inc. All rights reserved C File Processing.
1 Lecture09: File I/O 5/6/2013 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
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.
Bit Operations Horton pp Why we need to work with bits Sometimes one bit is enough to store your data: say the gender of the student (e.g. 0.
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee C Language Part 4.
1 Lecture09: File I/O 11/19/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
FILE IO in ‘C’ by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
C Programming Lecture 12 : File Processing
GAME203 – C Files stdio.h C standard Input/Output “getchar()”
CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 11 : September 18.
Adv. UNIX:fp/101 Advanced UNIX v Objectives of these slides: –a more detailed look at file processing in C Special Topics in Comp. Eng.
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.
 2007 Pearson Education, Inc. All rights reserved C File Processing.
CSE 220 – C Programming Bitwise Operators.
Lecture 12 CIS 208 Friday, March 3rd , 2005.
External Files: Abstractly, a file can be thought of as a stream of data (either char or binary). C has two groups of files: standard files, such as stdin,
Chapter 4 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
File I/O.
CGS 3460, Lecture 41 Apr 21, 2006 Hen-I Yang
Bit Operations Horton pp
CSC215 Lecture Input and Output.
Plan for the Day: I/O (beyond scanf and printf)
C Programming:Part 3 Characters and Strings File Processing Exercise.
What to bring: iCard, pens/pencils (They provide the scratch paper)
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
C: Primer and Advanced Topics
Introduction to Programming
File Management in C.
Formatting Output.
CSE1320 Files in C Dr. Sajib Datta
Programming and Data Structures
File I/O We are used to reading from and writing to the terminal:
Lecture 15 Files.
CSC215 Lecture Input and Output.
CSC215 Lecture Input and Output.
Text and Binary File Processing
File Input and Output.
Henning Schulzrinne Columbia University
Fundamental of Programming (C)
C Input / Output Prabhat Kumar Padhy
Chapter 12: File I/O.
Applications with Random File Access
Module 10 Operations on Bits
Module 12 Input and Output
File I/O & UNIX System Interface
Bit Manipulations CS212.
Professor Jodi Neely-Ritz University of Florida
I/O CS580U - Fall 2018.
Bit Operations Horton pp
File I/O We are used to reading from and writing to the terminal:
ECE 120 Midterm 1 HKN Review Session.
Presentation transcript:

Session #5 File I/O Bit Masks, Fields & Bit Manipulations C Tutorial Session #5 File I/O Bit Masks, Fields & Bit Manipulations

Communicating with files Often times, you need read information from a file or write information into a file. What is a file? Or what is a file to your C program. C views a file as a continuous sequence of bytes, each of which can be read individually.

The text view and the binary view The two views of a file are binary and text. In binary view, each and every byte of the file is accessible to a program. In text view, the local environment’s representation are mapped to the C view when a file is read.

Standard files C programs automatically open three files on your behalf, standard input, standard output and standard error output. Standard input, by default is your normal input device, usually your keyboard. Both standard output and standard error output, by default, are your normal output device, usually your display screen.

The fopen function Syntax: fopen(char *filename, char *mode) Example: FILE *fp; if ((fp = fopen(“filename”,”r”))== NULL) { printf(“can’t open file.\n”); … }

Checking command-line arguments Pass arguments from shell command. Example: int main(int argc, char *argv[]) The first argument argc is the number of arguments passed to your program. The array argv strores arguments. argv[0] is the program name, argv[n] is the nth argument passed to your program.

The getc and putc function Similar to getchar function, getc reads one byte from the target file. In our previous example, we can use getc as following: FILE *fp; … getc(fp); getc returns an EOF symbol when reaches the end of the file.

Other file I/O functions fprintf(FILE *, format, args..) fscanf(FILE *, format, args…) fgets(char *buf, int MAX, FILE *) fputs(char *buf, FILE*) fclose(FILE *)

The fseek function The fseek function enables you to treat a file like an array. Syntax: int fseek(FILE *, long offset, int whence); fseek(fp, 0L, SEEK_SET); //go to the beginning of the file fseek(fp, 10L, SEEK_SET); //go 10 bytes into the file. fseek(fp, 2L, SEEK_CUR); //advance 2 bytes from the current position. fseek(fp, -10L, SEEK_END); //back up 10 bytes from the end of the file.

The ftell and rewind functions The ftell function returns the current value of the file position indicator. The rewind function sets the file position indicator to the beginning of the file stream. The rewind function is equivalent to: fseek(fp, 0L, SEEK_SET);

Number Notation Decimal - 10 Unsigned – 10u Long – 10L Floating Point 10.0 Octal (Base 8) – 010 (8 decimal) Hexadecimal (Base 16) – 0x10 (decimal 16)

Bit fiddling With C, you can manipulate the individual bits in a variable. Why we need to play with bits? Hardware devices if often controlled by sending it one byte or two, in which each bit has a particular meaning. Many compression and encryption operations manipulate individual bits.

C’s bitwise operators DO NOT confuse & with && & AND | OR ^ XOR ~ One's Complement (unary) << Left shift >> Right Shift DO NOT confuse & with && & is bitwise AND, && logical AND Similarly for | and ||

Turn on 1 bit We can use bitwise or to turn on a bit flag. Sample code: int flag; flag = flag | 8; // turn on the forth bit from right. Note 8 in binary is 00001000 Similarly if we want to turn on the 1st and 3rd bits, we can use flag = flag | 5;

Turn off 1 bit We can use bitwise and to turn off 1 bit. Sample code: int flag; flag = flag & (~4) // turn off the 3rd bit from right. Similarly, if we want to turn off 1st, 2nd, and 4th bits from right, we can use flag = flag & (~11);

Bit shifting The shift operators perform appropriate shift by operator on the right to the operator on the left. The right operator must be positive. The vacated bits are filled with zero (i.e. There is NO wrap around). Example: x = 0x02; // 00000010 x >>= 2; // 00000000 x = 0x02; // 00000010 x <<= 2; // 00001000 Therefore a shift left is equivalent to a multiplication by 2 Similarly a shift right is equal to division by 2 Number << n multiplies number by 2 to the nth power. Number >> n divides number by 2 to the nth power.

Bit fields Struct box_props{ unsigned int opaque :1; unsigned int fill_color :3; unsigned int show_border :1 unsigned int border_color :3 unsigned int border_style :2 }

Examples Consider the following mask, and two bit strings from which we want to extract the final bit: mask = 0x01; // 00000001 value1 = 0x9B; // 10011011 value2 = 0x9C; // 10011100 x = mask & value1; // 0x01 (00000001) x = mask & value2; // 0x00 (00000000) The zeros in the mask mask off the first seven bits and only let the last bit show through. (In the case of the first value, the last bit is 1; in the case of the second value, the last bit is 0.) Alternatively, masks can be built up by operating on several flags, usually with inclusive OR: flag1 = 00000001; flag2 = 00000010; flag3 = 00000100; mask = flag1 | flag2 | flag3; // mask = 00000111 (0x07)