1 COMP 2130 Introduction to Computer Systems Computing Science Thompson Rivers University.

Slides:



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

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.
UNIT 15 File Processing.
Character String Manipulation. Overview Character string functions sscanf() function sprintf() function.
Character String Manipulation. Overview Character string functions sscanf() function snprintf() function.
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.
Winter2015 COMP 2130 Intro Computer Systems Computing Science Thompson Rivers University C: Advanced Topics.
Summer 2014 COMP 2130 Intro Computer Systems Computing Science Thompson Rivers University C: Formatted Files.
Structures and Unions Chapter 6. Structure A structure is an aggregate data type  Composed of two or more related variables called member/field/element.
1 CSE1301 Computer Programming: Lecture 9 Input/Output.
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.
8. Structures, File I/O, Recursion 18 th October IIT Kanpur 1C Course, Programming club, Fall 2008.
Structs. Structures We already know that arrays are many variables of the same type grouped together under the same name. Structures are like arrays except.
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: Advanced Topics Summer 2014 COMP 2130 Intro Computer Systems Computing Science Thompson Rivers University.
Standard Input and Output. Overview Data communication with a C program and the outside world is performed through files Files are a non-volatile way.
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.
Winter 2015 COMP 2130 Intro Computer Systems Computing Science Thompson Rivers University C: Assignment-II Tips.
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.
C What you Know* Objective: To introduce some of the features of C. This assumes that you are familiar with C++ or java and concentrates on the features.
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.
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 Data Structures and Algorithms Programs. Different problems. Operations. Size of data. Resources available.
1 Lecture09: File I/O 11/19/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
Chapter 7 : File Processing1 File-Oriented Input & Output CHAPTER 7.
1 CHAPTER6 CHAPTER 6. Objectives: You’ll learn about;  Introduction  Files and streams  Creating a sequential access file  Reading data from a sequential.
FILE IO in ‘C’ by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
Mobility Research Lab mobility.ceng.metu.edu.tr Applied Innovative Interdisciplinary (AI2) Research Lab Short Course on Programming in C/C++
CSE1301 Computer Programming: Lecture 6 Input/Output.
CS 261 – Recitation 7 Spring 2015 Oregon State University School of Electrical Engineering and Computer Science.
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.
C: Advanced Topics Winter 2013 COMP 2130 Intro Computer Systems Computing Science Thompson Rivers University.
Files A collection of related data treated as a unit. Two types Text
CS 241 Section Week #5 9/22/11. 2 Topics This Section File I/O Advanced C.
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.
1 Computer Programming Lecture 15 Text File I/O Assist. Prof Dr. Nükhet ÖZBEK Ege University Department of Electrical&Electronics Engineering
Advanced Programming in the UNIX Environment Hop Lee.
Real Numbers Device driver process within the operating system that interacts with I/O controller logical record 1 logical record 2 logical record 3.
Introduction to Computing Lecture 03: Basic input / output operations Introduction to Computing Lecture 03: Basic input / output operations Assist.Prof.Dr.
Lecture 12 CIS 208 Friday, March 3rd , 2005.
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.
File I/O.
CS 261 – Recitation 7 Fall 2013 Oregon State University
CSC215 Lecture Input and Output.
Plan for the Day: I/O (beyond scanf and printf)
CS111 Computer Programming
CSE1320 Files in C Dr. Sajib Datta
File Input/Output.
CSE1320 Files in C Dr. Sajib Datta
Computer Programming Lecture 15 Text File I/O
CSI 121 Structured Programming Language Lecture 7: Input/Output
I/O in C Lecture 6 Winter Quarter Engineering H192 Winter 2005
Lecture 15 Files.
CSC215 Lecture Input and Output.
CSC215 Lecture Input and Output.
Computing Science Thompson Rivers University
Text and Binary File Processing
File Input and Output.
Standard I/O Library Implementation
File I/O & UNIX System Interface
CSc 352 File I/O Saumya Debray Dept. of Computer Science
Presentation transcript:

1 COMP 2130 Introduction to Computer Systems Computing Science Thompson Rivers University

Contents 2 Basics of Structures Structures and Functions Arrays/Pointers of Structures Standard I/O File Access Error Handling Low Level I/O

Basics of Structures 3 A structure is a collection of one or more variables, possibly of different types, grouped together under a single name for convenient handling. Example: struct point { int x; int y };

Basics of Structures: Example 4 struct database { int id_number; int age; float salary; }; int main() { struct database employee; employee.age = 22; employee.id_number = 1; employee.salary = ; printf(“Employee ID = %d, Age = %d and Salary = %7.2f\n”, employee.id_number, employee.age, employee.salary); }

Structures and Functions 5 The only legal operations on a structure are copying it or assigning to it as a unit, taking its address with &, and accessing its members. This could be done by passing components separately, pass an entire structure, or pass a pointer to it.

Structures and Functions 6 The only legal operations on a structure are copying it or assigning to it as a unit, taking its address with &, and accessing its members. This could be done by passing components separately, pass an entire structure, or pass a pointer to it. If a large structure is to be passed to a function, it’s generally more efficient to pass a pointer than to copy the whole structure.

Arrays/Pointers of Structures 7. (direct member selector) -> (indirect, or pointer, member selector) struct student_rcd { intstudent_number; charname[128]; }; struct student_rcd { intstudent_number; charname[128]; }; void print_rcd(struct student_rcd rcd) { printf("Number: %d\n",rcd.student_number); printf("Name: %s\n", rcd.name); } void read_rcd(struct student_rcd *rcd) { printf("Enter number: "); scanf("%d", &(rcd->student_number)); printf("Enter name: "); scanf("%s", rcd->name); // (*rcd).name } void print_rcd(struct student_rcd rcd) { printf("Number: %d\n",rcd.student_number); printf("Name: %s\n", rcd.name); } void read_rcd(struct student_rcd *rcd) { printf("Enter number: "); scanf("%d", &(rcd->student_number)); printf("Enter name: "); scanf("%s", rcd->name); // (*rcd).name } int main() { struct student_rcd data_entry; read_rcd(&data_entry); print_rcd(data_entry); return 0;} int main() { struct student_rcd data_entry; read_rcd(&data_entry); print_rcd(data_entry); return 0;}

Typedef 8 typedef int Length; // Now Length is a data type. typedef char *String; // Now String is a data type. typedef struct tnode { /* the tree node: */ char *word; /* points to the text */ int count; /* number of occurrences */ struct tnode *left, *right; /* children */ struct tnode *parent; } Treenode; // Now Treenode is a data type.... Length len, maxlen; Length *lengths[]; String p, lineptr[MAXLINES]; Treenodetnode1;

Union 9 A union is a special data type available in C that enables you to store different data types in the same memory location like structures The main difference is that a union may have many members, but only one member can contain a value at any given time. Unions provide an efficient way of using the same memory location for multi-purpose. #include union Data { int i; float f; char str[20]; }; int main( ) { union Data data; data.i = 10; printf( "data.i : %d\n", data.i); return 0; }

Standard I/O 10 The simplest input mechanism is to read one character at a time from the standard input with getchar: int getchar(void) getchar returns the next input character each time it is called, or EOF when it encounters end of file. Redirection > & Piping |

File Access: Binary Files 11 Binary files are very similar to arrays of structures, except the structures are in a disk-file rather than an array in memory. Binary files have two features that distinguish them from text files: You can instantly use any structure in the file. You can change the contents of a structure anywhere in the file. int fseek(FILE*, long, SEEK_SET or SEEK_CURRENT or SEEK_END); // move file position pointer int fwrite(void*, int memb_size, int no_memb, FILE*); int fread(void*, int memb_size, int no_memb, FILE*); int ftell ( FILE * stream );

File Access: Closing 12 The function int fclose(FILE *fp) is the inverse of fopen; It breaks the connection between the file pointer and the external name that was established by fopen, freeing the file pointer for another file. It also flushes the buffer in which putc is collecting output.

Error Handling 13 stderr is assigned to a program the same way that stdin and stdout are assigned. Output written on stderr normally appears on the screen. The function ferror returns non-zero if an error occurred on the stream fp int ferror(FILE *fp)

Low Level I/O: Read and Write 14 read and write system calls are used by C programs through the two functions: read and write. int n_read = read(int fd, char *buf, int n); int n_written = write(int fd, char *buf, int n); File descriptor Character array where data is to go to or come from Number of bytes to be transferred

Low Level I/O: Open, Creat, Close, Unlink 15 Example: Open is rather like the fopen, except that instead of returning a file pointer, it returns a file descriptor, which is just an int. #include int fd; int open(char *name, int flags, int perms); fd = open(name, flags, perms);

Questions? 16