CSCI 330 UNIX and Network Programming

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

A C++ Crash Course Part II UW Association for Computing Machinery Questions & Feedback.
Dynamic Memory Allocation in C.  What is Memory What is Memory  Memory Allocation in C Memory Allocation in C  Difference b\w static memory allocation.
C Characters & Strings Character Review Character Handling Library Initialization String Conversion Functions String Handling Library Standard Input/Output.
 2003 Prentice Hall, Inc. All rights reserved Fundamentals of Characters and Strings Character constant –Integer value represented as character.
Strings.
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.
Lecture 9. Lecture 9: Outline Strings [Kochan, chap. 10] –Character Arrays/ Character Strings –Initializing Character Strings. The null string. –Escape.
Lecture 20 Arrays and Strings
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.
 2000 Prentice Hall, Inc. All rights reserved Fundamentals of Strings and Characters String declarations –Declare as a character array or a variable.
ECE Application Programming Instructor: Dr. Michael Geiger Spring 2012 Lecture 31: PE5.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Characters and Strings.
Array_strcpy void array_strcpy(char dest[], char src[]) { int i = 0; while (src[i] != '\0') { dest[i] = src[i]; i++; } dest[i] = '\0'; }
Pointer, malloc and realloc 1. Name entered was 6 char, not enough space to put null terminator 2 Array of char.
CS 241 Section Week #2 2/4/10. 2 Topics This Section MP1 overview Part1: Pointer manipulation Part2: Basic dictionary structure implementation Review.
Informática II Prof. Dr. Gustavo Patiño MJ
 2000 Prentice Hall, Inc. All rights reserved. Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character.
Chapter 8 Characters and Strings Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
C Programming. C vs C++ C syntax and C++ syntax are the same but... C is not object oriented * There is no string class * There are no stream objects.
CS 311 – Lecture 12 Outline File management system calls Stat() Directory Information  Opendir()  Readdir()  Closedir() Truncate() and remove() Lecture.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
 2007 Pearson Education, Inc. All rights reserved C Characters and Strings.
Strings in C. Strings are Character Arrays Strings in C are simply arrays of characters. – Example:char s [10]; This is a ten (10) element array that.
Introduction to C programming
University of Calgary – CPSC 441. C PROGRAM  Collection of functions  One function “main()” is called by the operating system as the starting function.
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.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character.
1 UNIX System Programming v Objectives –look at how to program with directories –briefly describe the UNIX file system Directories and File System.
ENGR 3950U / CSCI 3020U: Operating Systems Description and C Code of Major Functions in Simulated Unix File System. Instructor: Dr. Kamran Sartipi Faculty.
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.
Character Arrays Based on the original work by Dr. Roger deBry Version 1.0.
Adv. UNIX: dirs/181 Advanced UNIX v Objectives –look at how to program with directories –briefly describe the UNIX file system Special.
Directory structure. Slide 2 Directory Structure  A directory ‘file’ is a sequence of lines; each line holds an i-node number and a file name.  The.
Characters and Strings File Processing Exercise C Programming:Part 3.
File IO and command line input CSE 2451 Rong Shi.
APS105 Strings. C String storage We have used strings in printf format strings –Ex: printf(“Hello world\n”); “Hello world\n” is a string (of characters)
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
Navigating Directories
Review of Lectures 12, 13, 14 -Functions -Arrays -Strings.
Today’s topic Access and manipulate meta data for files –File type, ownership, access permissions, access time, etc How to determine if a file is not there?
CSC141- Introduction to Computer programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 21 Thanks for Lecture Slides:
Strings Programming Applications. Strings in C C stores a string in a block of memory. The string is terminated by the \0 character:
Copyright ©: Nahrstedt, Angrave, Abdelzaher, Caccamo1 Files and file allocation.
NCHU System & Network Lab Lab 14 File and Directory.
GAME203 – C Files stdio.h C standard Input/Output “getchar()”
Chapter 8 Characters and Strings. Objectives In this chapter, you will learn: –To be able to use the functions of the character handling library ( ctype).
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI N305 Characters and Strings Functions.
CS 241 Section Week #8 (10/29/09). Outline MP5 Overview Files & I/O UNIX File Systems inodes Directories Links.
1 Recall that... char str [ 8 ]; str is the base address of the array. We say str is a pointer because its value is an address. It is a pointer constant.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Characters and Strings Dale Roberts, Lecturer Computer Science,
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.
ECE 103 Engineering Programming Chapter 29 C Strings, Part 2 Herbert G. Mayer, PSU CS Status 7/30/2014 Initial content copied verbatim from ECE 103 material.
Principles of Programming - NI Chapter 10: Character & String : In this chapter, you’ll learn about; Fundamentals of Strings and Characters The difference.
S ALVATORE DI G IROLAMO (TA) Networks and Operating Systems: Exercise Session 3.
1 CIS*2450(S04) - Seminar 4 Trouble spots from A1 Practice with Regexs POSIX Regex Library Help on Assignment #2 Python Examples.
Pointers & Arrays 1-d arrays & pointers 2-d arrays & pointers.
C Characters and Strings
Directory Directory is a file containing list of entries,where each entry represents inode number and name of the file stored in that directory.
CSE 333 – Section 3 POSIX I/O Functions.
C Stuff CS 2308.
CSE 333 – Section 3 POSIX I/O Functions.
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.
CSE 333 – Section 3 POSIX I/O Functions.
CSE 333 – Section 3 POSIX I/O Functions.
Characters and Strings Functions
C Characters and Strings
Presentation transcript:

CSCI 330 UNIX and Network Programming The Bash Shell CSCI 330 UNIX and Network Programming Unit VI: Systems Programming in C++ Copyright Department of Computer Science, Northern Illinois University, 2005

Unit Overview C++ review C Library functions programming with Geany CSCI 330 - UNIX and Network Programming Unit Overview C++ review programming with Geany C Library functions C strings environment access regular expressions directory I/O

CSCI 330 - UNIX and Network Programming C++ Review

C Library Functions C library is accessible to C++ CSCI 330 - UNIX and Network Programming C Library Functions C library is accessible to C++ I/O operations: #include <cstdio> functions: printf, scanf, putchar, getchar, ... Strings: #include <cstring> functions: strlen, strcat, strcpy, strstr, memset, ... Standard general utilities: #include <cstdlib> functions: atoi, rand, malloc, free, getenv, exit, system, ... Reference: http://www.cplusplus.com/reference/clibrary

C Strings: strcpy char* strcpy(char* dest, const char* source) CSCI 330 - UNIX and Network Programming C Strings: strcpy char* strcpy(char* dest, const char* source) copies the C string pointed to by source into the array pointed to by dest, including the terminating null character to avoid overflows, the size of the array pointed to by dest must be long enough to contain the same C string as source, including the terminating null character

CSCI 330 - UNIX and Network Programming C Strings: strcpy

General Utility: getenv CSCI 330 - UNIX and Network Programming General Utility: getenv char * getenv ( const char * name ) gets environment string retrieves a C string containing the value of the environment variable whose name is specified as argument if the requested variable is not part of the environment list, the function returns a null pointer

General Utility: getenv CSCI 330 - UNIX and Network Programming General Utility: getenv

General Utility: exit void exit ( int status ) CSCI 330 - UNIX and Network Programming General Utility: exit void exit ( int status ) terminates calling process if status is zero or EXIT_SUCCESS, a successful termination status is returned to the host environment if status is EXIT_FAILURE, an unsuccessful termination status is returned to the host environment

General Utility: system CSCI 330 - UNIX and Network Programming General Utility: system int system ( const char * command ) invokes the command processor to execute a command returns exit status of command if command is a null pointer, the function only checks whether a command processor is available

General Utility: system CSCI 330 - UNIX and Network Programming General Utility: system

Regular Expression Library Functions CSCI 330 - UNIX and Network Programming Regular Expression Library Functions allows to use regular expression to search strings functions: regcomp to prepare, i.e. compile, a regular expression regexec to use prepared expression to search a string

CSCI 330 - UNIX and Network Programming C Library: Regex

Library Function: regcomp CSCI 330 - UNIX and Network Programming Library Function: regcomp int regcomp(regex_t *preg, const char *regex, int cflags) compiles the regular expression string regex into a regex_t structure preg for later use cflags allows variations to regular expression styles 0 selected basic regular expression syntax REG_EXTENDED uses extended regular expression syntax

Library Function: regexec CSCI 330 - UNIX and Network Programming Library Function: regexec int regexec(const regex_t *preg, const char *string, size_t nmatch, regmatch_t pmatch[], int eflags) uses regular expression preg to analyze string nmatch and pmatch[] return the location of matches can be set to 0 if not needed eflags allows variations on end of line matching returns 0 for successful match 

Library Function: regex example CSCI 330 - UNIX and Network Programming Library Function: regex example

Error Handling convention on how to report errors CSCI 330 - UNIX and Network Programming Error Handling convention on how to report errors return -1 in return status set global variable errno errno is index into table of error messages C library function perror translates this error code and prints understandable error message

C Library Function: perror CSCI 330 - UNIX and Network Programming C Library Function: perror

Directory Input/Output CSCI 330 - The UNIX System Directory Input/Output current directory: chdir, getcwd directory I/O functions: opendir, readdir directory I/O types: DIR, struct dirent

Directory I/O function: opendir CSCI 330 - The UNIX System Directory I/O function: opendir

Directory I/O function: opendir CSCI 330 - The UNIX System Directory I/O function: opendir DIR *opendir(const char *name) opens directory name as a stream returns DIR pointer for readdir function returns NULL on error, and errno is: ENOENT directory does not exist ENOTDIR name is not a directory

Directory I/O function: readdir CSCI 330 - The UNIX System Directory I/O function: readdir

Directory I/O function: readdir CSCI 330 - The UNIX System Directory I/O function: readdir struct dirent *readdir(DIR *dirp) returns a pointer to a dirent structure representing the next directory entry in directory dirp returns NULL on reaching end of directory or if an error occurred

CSCI 330 - The UNIX System dirent structure struct dirent { ino_t d_ino; /* inode number */ off_t d_off; /* offset to the next dirent */ unsigned short d_reclen; /* length of this record */ unsigned char d_type; /* type of file */ char d_name[256]; /* filename */ };

Illustration: readDir.cxx CSCI 330 - The UNIX System Illustration: readDir.cxx

CSCI 330 - The UNIX System Directory I/O detail // open directory dirp = opendir(argv[1]); if (dirp == 0) { perror(argv[1]); exit(EXIT_FAILURE); } while ((dirEntry = readdir(dirp)) != NULL) { cout << dirEntry->d_name << endl; closedir(dirp);

Summary C++ programming with C Library functions Next: CSCI 330 - UNIX and Network Programming Summary C++ programming with C Library functions Next: C++ programming with System Calls