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

Slides:



Advertisements
Similar presentations
Unit 10 Miscellaneous Advanced Topics Introduction to C Programming.
Advertisements

Introduction to C Programming
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.
A C++ Crash Course Part II UW Association for Computing Machinery Questions & Feedback.
Computer Programming for Engineering Applications ECE 175 Intro to Programming.
Using Files Declare a variable, called file pointer, of type FILE * Use function fopen to open a named file and associate this file with the file pointer.
Chapter 6 Structures By C. Shing ITEC Dept Radford University.
1 Chapter 10 Strings and Pointers. 2 Introduction  String Constant  Example: printf(“Hello”); “Hello” : a string constant oA string constant is a series.
Character String Manipulation. Overview Character string functions sscanf() function sprintf() function.
Character String Manipulation. Overview Character string functions sscanf() function snprintf() function.
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Introduction Data files –Can be created, updated,
Chapter 11 C File Processing Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
ISP - 2 nd Recitation Functions Pointers Structs Files Code Examples Homework!
Structures and Unions Chapter 6. Structure A structure is an aggregate data type  Composed of two or more related variables called member/field/element.
Console and File I/O - Basics Rudra Dutta CSC Spring 2007, Section 001.
An Introduction to C Programming (assuming that you already know Java; this is not an introduction to C++)
8. Structures, File I/O, Recursion 18 th October IIT Kanpur 1C Course, Programming club, Fall 2008.
Lone Leth Thomsen Input / Output and Files. April 2006Basis-C-8/LL2 sprintf() and sscanf() The functions sprintf() and sscanf() are string versions of.
File Handling Spring 2013Programming and Data Structure1.
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.
Characters and Strings File Processing Exercise C Programming:Part 3.
File IO and command line input CSE 2451 Rong Shi.
1 COMP 2130 Introduction to Computer Systems Computing Science Thompson Rivers University.
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:
Introduction As programmers, we don’t want to have to implement functions for every possible task we encounter. The Standard C library contains functions.
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.
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)
GAME203 – C Files stdio.h C standard Input/Output “getchar()”
Gramming An Introduction to C Programming (assuming that you already know Java; this is not an introduction to C++)
C: Advanced Topics Winter 2013 COMP 2130 Intro Computer Systems Computing Science Thompson Rivers University.
 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.
Connecting to Files In order to read or write to a file, we need to make a connection to it. There are several functions for doing this. fopen() – makes.
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.
6/9/2016Course material created by D. Woit 1 CPS 393 Introduction to Unix and C START OF WEEK 10 (C-4)
Real Numbers Device driver process within the operating system that interacts with I/O controller logical record 1 logical record 2 logical record 3.
An Introduction to C Programming (assuming that you already know Java; this is not an introduction to C++)
TMF1414 Introduction to Programming
An Introduction to C Programming
C Programming:Part 3 Characters and Strings File Processing Exercise.
CS111 Computer Programming
C: Primer and Advanced Topics
Chapter 11 – File Processing
Input/Output and the Operating Systems
Computing Science Thompson Rivers University
Programming and Data Structure
Text and Binary File Processing
File Handling.
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)
File Handling in C.
CSc 352 File I/O Saumya Debray Dept. of Computer Science
Professor Jodi Neely-Ritz University of Florida
COMP 2130 Intro Computer Systems Thompson Rivers University
Presentation transcript:

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

TRU-COMP2130 C: Advanced Topics2 Character Pointers #include gets(), puts() strcpy(), strlen(), strcmp(), strcat(),... toupper(),...

TRU-COMP2130 C Programming3 char name[256], tmp[256]; name[0] = C; name[1] = O; name[2] = M; name[3] = P; name[4] = \0;// it is very important. name[5] = ; name[6] = 2; name[7] = 1; name[8] = 3; name[9] = 0; name[10] = \0; // it is very important. printf(course number = %s\n, name); printf(%p\n, name); printf(course number = %s\n, &(name[5])); scanf(%s, name);// not &name sprintf(tmp, course name is %s., name);

TRU-COMP2130 C: Advanced Topics4 Pointer Arrays: Pointers to Pointers void f(int *x[13]); // 13 int* variables void f(int (*x)[13]); // pointer to an array of 13 ints // equivalent to int x[][13] Command-line arguments int main(int argc, char *argv[]); argcthe number of arguments argv[0]the program name, e.g., a.out argv[1]the first argument from the user E.g., $./a.out test this comp argc: 4 argv[0]:./a.out argv[1]: test

TRU-COMP2130 C: Advanced Topics5 2. Structures User-defined data structure struct student_rcd {// class without methods in Java intstudent_number; charname[128];... };... struct student_rcd record[10], *rp; struct student_rcd test;// how to declare a struct variable test.student_number = 10;// how to access a member print_rcd(test); read_rcd(&test); record[0].student_number = 5; rp = (struct student_rcd *)malloc(sizeof(struct student_rcd) * 3); rp->student_number = 20; // Be careful with the -> (*(rp+1)).student_number = 40; rp[2].student_number = 30;...

TRU-COMP2130 C: Advanced Topics6 void print_rcd(struct student_rcd rcd) { printf(Number: %d\n, rcd.student_number); printf(Name: %s\n, rcd.name); // name is an array. // not &(rcd.name) } void read_rcd(struct student_rcd *rcd) { printf(Enter number: ); scanf(%d, &(rcd->student_number)); // reference rqd printf(Enter name: ); scanf(%s, rcd->name); // name is an array. // not &(rcd->name) }

TRU-COMP2130 C: Advanced Topics7 Self-Referential Structures struct tnode { /* the tree node: */ char *word; /* points to the text */ int count; /* number of occurrences */ struct tnode *left; /* left child */ struct tnode *right; /* right child */ struct tnode *parent; }; struct tnode root; root.left = (struct tnode *)malloc(...);

TRU-COMP2130 C: Advanced Topics8 Typedef 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]; Treenodetnode; p = (String) malloc(100); // p[0],..., p[99]

TRU-COMP2130 C: Advanced Topics9 Example struct Student { char name[128]; int number; Student *next; }; typedef struct Student Student; Student *head = null; // important Student *new, *tmp; new = create_student(); // create a record, read data from the user, // store them into the record add_student(head, new); // add the new record at the end of the list tmp = find_student(head, ); // find the record whose number is.. printf(%d: %s\n, tmp->number, tmp->name); // print the record delete_student(head, ); Name Number next Name Number next Name Number next Head null

TRU-COMP2130 C: Advanced Topics10... create_student(... ) // create a record, read data from the user, { // store them into the record Student *new = (... )malloc(... ); scanf(%s,...) // read name scanf(%d,...) // read number new->next = null; // very important; (*new).next = null return...; };... add_student(... head,... new) // add the new record at the end of the list { Student *tmp; if (head == null) // when there is no record yet head = new; else { while((*tmp).next != null) // move to the last record tmp = (*tmp).next; // You cannot use array syntaxes (*tmp).next = new; // because the Student objects were not } // consecutively created. return; }

TRU-COMP2130 C: Advanced Topics11... find_student(... head,... no) // find the record { Student *tmp; tmp = head; while(tmp != null) { if ((... == no) break; tmp =...; }... delete_student(... head,... no) {... }

TRU-COMP2130 C: Advanced Topics12 Unions For polymorphism union u_tag { // the shared storage int ival; float fval; char *sval; }... int utype; union u_tag u;... u.ival = 20; if (utype == INT) printf("%d\n", u.ival); if (utype == FLOAT) printf("%f\n", u.fval); if (utype == STRING) printf("%s\n", u.sval); else printf("bad type %d in utype\n", utype);

TRU-COMP2130 C: Advanced Topics13 3. Input and Output Standard input from keyboard $ prog < infileinput redirection $ otherprog | progpipe int getchar() int putchar(int c)

TRU-COMP2130 C: Advanced Topics14 Formatted input int scanf (char *format, arg1, arg2,...)// from stdin int sscanf (char *string, char *format, arg1, arg2,...);// from string The arguments must be references. {float f; int i; string the_string = "foo -3.6 fum dum 17"; sscanf(the_string, "foo %f fum dum %d", f, i); } sscanf("foo", "f%s", a);

TRU-COMP2130 C: Advanced Topics15 File Access #include FILE *in, *out; // FILE is defined in in = fopen(in_filename, r);// mode: r, w, a, r+, w+, a+ if (in == NULL)... out = fopen(out_filename, w); fclose(in); fprintf(out, format..., variables...); fscanf(...); fgets(...); 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*);

TRU-COMP2130 C: Advanced Topics16 int fputc(int, FILE*); int fputs(char*, FILE*); int fgetc(FILE*); int fscanf(FILE*, char* format,...); int fprintf(FILE*, char* format,...); Examples: A file copy program, using fopen(), fseek(), fwrite(), fread(), fclose(). Files containing student records struct student {... }; struct student record; FILE *fp = fopen(test, w+); // read and write; file truncated; fwrite(&record, sizeof(struct student), 1, fp); fread(&record, sizeof(struct student), 1, fp);

TRU-COMP2130 C: Advanced Topics17 How to obtain the current position:: long ftell(FILE*);

TRU-COMP2130 C: Advanced Topics18 Error Handling – Stderr and Exit fprintf(stderr, char*,...); exit(int);// non zero means error

TRU-COMP2130 C Programming19 math.h Some MATH related functions # include double sqrt(double); double pow(double, double); double fabs(double);... Link with –lm-lm means libm.a, that contains math utilities, is used $ gcc program3-5.c –lm