Structures and Unions Chapter 6. Structure A structure is an aggregate data type  Composed of two or more related variables called member/field/element.

Slides:



Advertisements
Similar presentations
C: Advanced Topics-II Winter 2013 COMP 2130 Intro Computer Systems Computing Science Thompson Rivers University.
Advertisements

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.
Memory and Files Dr. Andrew Wallace PhD BEng(hons) EurIng
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.
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.
Kernel File Interface operating systems (or programming I/O in Unix)
Lec11: File Processing 廖雪花 TEL: 年 5 月.
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.
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.
 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.
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.
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:
1 Lecture09: File I/O 11/19/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
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.
 2000 Prentice Hall, Inc. All rights reserved Introduction Data files –Can be created, updated, and processed by C programs –Are used for permanent.
© 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.
Chapter 7 Files By C. Shing ITEC Dept Radford University.
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
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.
Structured Programming Approach Module VIII - Additional C Data Types File Handling Prof: Muhammed Salman Shamsi.
Chapter 12 Files (reference: Deitel ’ s chap 11) chap8.
1 CSC103: Introduction to Computer and Programming Lecture No 28.
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 A collection of related data treated as a unit. Two types Text
 2007 Pearson Education, Inc. All rights reserved. 1 C File Processing.
Files Programming 1. 2 What is a File? Is a block of arbitrary information, or resource for storing information, which is available to a computer program.
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.
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.
Real Numbers Device driver process within the operating system that interacts with I/O controller logical record 1 logical record 2 logical record 3.
By C. Shing ITEC Dept Radford University
11 C File Processing.
Chapter 4 File Processing
Lecture 11 File input/output
TMF1414 Introduction to Programming
File I/O.
CS111 Computer Programming
File Input/Output.
What you need for the 1st phase of project
Introduction to Programming
Chapter 11 – File Processing
Lecture 13 Input/Output Files.
Beginning C Lecture 11 Lecturer: Dr. Zhao Qinpei
Input/Output and the Operating Systems
Text and Binary File Processing
File Input and Output.
Chapter: 7-12 Final exam review.
Accessing Files in C Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
Fundamental of Programming (C)
CSc 352 File I/O Saumya Debray Dept. of Computer Science
Professor Jodi Neely-Ritz University of Florida
Presentation transcript:

Structures and Unions Chapter 6

Structure A structure is an aggregate data type  Composed of two or more related variables called member/field/element struct tag-name{ type member1; type member2; type member3;. type memberN; } variable-list;

Structure Contd. struct point{ int x; int y; }; Tag-name and variable-list are optional but one of them must present Structure can be initialized strcut point pt = {20, 30}; Member can be accessed via. operator struct point pt; pt.x = 20; pt.y = 30;

Array of Structures stuct student{ long id; char name[20]; char dept; }; struct student s[100];

Array of Structures Contd. stuct { int id; char name[20]; char dept[4]; } s[100]; scanf(“%d”, &s[0].id); scanf(“%s”, s[0].name); scanf(“%s”, s[0].dept);

Nested Structures struct careof{ char houseno[20]; int roadno; char location[80]; char phone[20]; }; struct student_info{ int id; char name[80]; struct careof address; }s;

Nested Structures Contd. Accessing the nested structure scanf(“%s”, s.address.houseno); scanf(“%d”, &s.address.roadno);

Union union is a single piece of memory that is shared by two or more variables union tag-name{ type member1; type member2; type member3;. type memberN; } variable-list;

Union Contd. union u_type{ int i; char c[2]; float d; }sample; 1234 d c[0]c[1] i

Suppose that a constant may be an int, a float, or a char struct { char name[20]; int utype; union u_tag{ int ival; float fval; char cval; }u; }symtab[MAX];

Structure: Bit-fields So far we cannot access at bit level Bit-fields are useful when you want to pack information into the smallest possible space struct b_type{ unsigned dept: 3; unsigned stock: 2; }var_list;

Structure: Bit-fields Contd. The members are all either integer or unsigned integer  For integer the left most bit will be regarded as sign bit Can be assigned values confirming its limit A field may overlap a word boundary is implementation-defined Fields may not be named (: and width) used for padding Processor architecture dependent

File I/O Chapter 9 Teach Yourself by Herbert Schildt

Understanding Streams Stream: the C I/O system supplies a consistent interface to the programmer for device I/O files  A level of abstraction  A logical interface File: actual device providing I/O is called a file

Standard Streams stdin stdout stderr

Types of Stream Two types  Text  Binary Text file  Contains ASCII characters  Some character translation  So, no one-to-one correspondence between what is sent to the stream and what is written to the file Binary file  May be used with any type of data  No character translation  So there is one-to-one correspondence

How to open a file? FILE *fopen (char *filename, char *mode); Stdio.h Filename  path Mode  “r”, “w”, “a”, “rb”, “ab”, “r+”  Consequence of those modes

How to close a file? Int fclose (FILE *fp); In order to improve efficiency most file system write data to disk one sector at a time Fclose flushes the buffer

How to know it is the end of file? Int feof (FILE *fp); Int ferror (FILE *fp);

How to read and write a character? Int fgetc (FILE *fp); Int fputc (int ch, FILE *fp); And more on this later

An example: reading a text file and displaying it in the screen FILE *fp; if ((fp = fopen (“a.txt”, “r”))==NULL){ //error and exit } while (! feof (fp)){ putchar (fgetc(fp)); } fclose (fp);

Writing and reading strings and others Int fputs (char *str, FILE *fp); Int fgets (char *str, int num, FILE *fp); Int fprintf (FILE *fp, format speci, variable(s)); Int fscanf (FILE *fp, format speci, address(es));

How to read/write in binary mode? size_t fread (void *buffer, size_t size, size_t num, FILE *fp); size_t fwrite (void *buffer, size_t size, size_t num, FILE *fp); size_t: defined in stdio.h unsigned long Void pointer: pointer of any data types

An example: reading a text file and displaying it in the screen FILE *fp; char ch; if ((fp = fopen (“a.txt”, “rb”))==NULL){ //error and exit } while (! feof (fp)){ fread(&ch, sizeof (char), 1); putchar (ch); } fclose (fp);

Writing an entire array double d[10] = {10.2, 20.3,….}; fwrite (d, sizeof d, 1, fp); //entire fread (d, sizeof (double), 5, fp); //only first five elements

Random Access So far, we have seen write and read sequentially  Beginning to end Using another function we can access any point in a file  Used only in binary mode (one-to-one) int fseek (FILE *fp, long offset, int origin);

Origins are SEEK_SET//seek from the start of file SEEK_CUR//seek from current location SEEK_END//seek from end of file long ftell (FILE *fp);

Example: copy a file into another in reverse order FILE *in, *out; char ch; long loc; fseek(in, 0L, SEEK_END); loc = ftell(in); loc = loc -1; //skip the end marker while (loc >= 0){ fseek(in, loc, SEEK_SET); ch = fgetc(in); fputc(ch, out); loc--; }

Example?? 10 double numbers are written in a file User want to access any number