Unix C + GNU HTML Perl Python
Libraries & Files Comp-206 : Introduction to Software Systems Week 6
COMP 206 - Joseph Vybihal 2007 Software Systems
Joseph Vybihal Computer Science McGill University
1
Unix C + GNU HTML Perl Python
Announcements • Midterm exam: In Class – October 24 – Review: October 22nd ?
COMP 206 - Joseph Vybihal 2007 Software Systems
2
Unix C + GNU HTML Perl Python
Part 1 COMP 206 - Joseph Vybihal 2007 Software Systems
C’s Standard Libraries
3
Unix C + GNU HTML Perl Python
Many Libraries
COMP 206 - Joseph Vybihal 2007 Software Systems
• • • • • • • •
#include <stdlib.h> • .h simply refers to a “header” file. #include <stdio.h> It is basically the same as a .c file but with the extension .h to indicate that it #include <string.h> gets included by another file. • Normally .h is used for files that have #include <math.h> common info needed by many .c files. #include Many more come standard in C Many more designed special for your compiler Many more can be purchased 4
Unix C + GNU HTML Perl Python
5 COMP 206 - Joseph Vybihal 2007 Software Systems
#include <string.h>
Unix C + GNU HTML Perl Python
String Functions
COMP 206 - Joseph Vybihal 2007 Software Systems
• • • • • •
int strcmp(char *s1, char*s2) int strncmp(char *s1, char*s2, int len) int strlen(char *string) char *strcpy(char *dest, char*src) … strncpy char *strcat(char *dest, char*src) … strncat void *memset(char* string, char character, int len)
Some random examples... Char * vs. char array[] 6
Unix C + GNU HTML Perl Python
void
#include <string.h>
*memccpy(void *restrict, const void *restrict, int, size_t);
COMP 206 - Joseph Vybihal 2007 Software Systems
void *memchr(const void *, int, size_t); int memcmp(const void *, const void *, size_t); void *memcpy(void *restrict, const void *restrict, size_t); void *memmove(void *, const void *, size_t); void *memset(void *, int, size_t); char *strcat(char *restrict, const char *restrict); char *strchr(const char *, int); int strcmp(const char *, const char *); int strcoll(const char *, const char *); char *strcpy(char *restrict, const char *restrict); size_t strcspn(const char *, const char *); char *strdup(const char *); char *strerror(int); int *strerror_r(int, char *, size_t); size_t strlen(const char *); char *strncat(char *restrict, const char *restrict, size_t); int strncmp(const char *, const char *, size_t); char *strncpy(char *restrict, const char *restrict, size_t); char *strpbrk(const char *, const char *); char *strrchr(const char *, int); size_t strspn(const char *, const char *); char *strstr(const char *, const char *); char *strtok(char *restrict, const char *restrict); char *strtok_r(char *, const char *, char **); size_t strxfrm(char *restrict, const char *restrict, size_t);
7
Unix C + GNU HTML Perl Python
Creating your own lib functions • How can we make strlen? • How can we make strcmp?
COMP 206 - Joseph Vybihal 2007 Software Systems
• How would we then include them in our programs?
8
Unix C + GNU HTML Perl Python
9 COMP 206 - Joseph Vybihal 2007 Software Systems
#include <stdio.h>
Unix C + GNU HTML Perl Python
Three Standard I/O Methods • Console I/O – Def: I/O between the user’s keyboard and screen – Defaults: • stdin (keyboard), stdout (screen), stderr (screen) • The defaults can be redirected (to a file or a port)
• Stream I/O (sequential) – String I/O • Def: I/O between your program and a string • Defaults: – Null terminated, contiguous set of characters COMP 206 - Joseph Vybihal 2007 Software Systems
– Sequential File I/O • Def: I/O between your program and a text file • Defaults: – Sequential file, with white space formatting and EOF character
• Random I/O (blocks) – Def: I/O between your program and any file (text/binary) – Defaults: • Standard offset movement patterns
10
Unix C + GNU HTML Perl Python
#include <stdio.h> • Contains: printf and scanf • Contains additional keyboard and screen:
COMP 206 - Joseph Vybihal 2007 Software Systems
– gets(char array); // Read a string, insert \0 – char c = getch(); // Read a single char – char c = getchar(); // Load buffer, then by char – char c = getche(); // Read a char and echo – putc(char); // Write a single char to screen
11
Stdio Library
Unix C + GNU HTML Perl Python
constants
COMP 206 - Joseph Vybihal 2007 Software Systems
functions
12
Unix C + GNU HTML Perl Python
Stdio Library (2)
COMP 206 - Joseph Vybihal 2007 Software Systems
**
13
Unix C + GNU HTML Perl Python
Example #include <stdio.h> int main(void) { char string[200]; int x; char y; gets(string);
COMP 206 - Joseph Vybihal 2007 Software Systems
sscanf(string, “%d %c”, &x, &y); }
14
Unix C + GNU HTML Perl Python
Key & Screen Example #include <stdio.h> int main(void) { char c = ‘0’; printf(“Input characters until x: ”);
COMP 206 - Joseph Vybihal 2007 Software Systems
while(c != ‘x’ && c != ‘X”) { c = getch(); if (c >= ‘0’ && c • Library containing I/O functions and definitions
COMP 206 - Joseph Vybihal 2007 Software Systems
– FILE *ptr; – fopen – fclose – feof – fscanf – fprintf – etc. 19
Unix C + GNU HTML Perl Python
FILE Structure • A struct typedef’d to FILE • Conatins: typedef struct {
COMP 206 - Joseph Vybihal 2007 Software Systems
char *fpos; /* Current position of file pointer */ void *base; /* Pointer to the base of the file */ unsigned short handle; /* File handle */ short flags; /* I/O mode Flags in masked binary*/ short unget; /* 1-byte buffer for ungetc (b15=1 if non-empty) */ unsigned long alloc; /* Number of currently allocated bytes for the file */ unsigned long allocPtr; /* current character in buffer */ unsigned short buffincrement; /* Number of bytes allocated at once */ } FILE;
This is actually machine dependent and changes… (why?)
20
Unix C + GNU HTML Perl Python
Opening a file • You can open a file using the fopen function. • FILE* fopen(const char* file, const char* mode)
• Once a file is opened, it returns a FILE pointer. That pointer can then be used to modify the file. – The file pointer points to a structure that contains information about the file, such as COMP 206 - Joseph Vybihal 2007 Software Systems
• • • •
Location of buffer Current character position in buffer Open mode Any errors that might have occured.
• The “mode” indicates what type of access is required. • In case of error, fopen returns null.
21
Unix C + GNU HTML Perl Python
Open mode • The mode is specified by a single character: – r : opens a file in read mode – w : opens a file in write mode – a : opens a file in append mode
COMP 206 - Joseph Vybihal 2007 Software Systems
• If a non-existing file is opened in write or append mode, it is first created. • If an existing file is opened in write mode, it's original content is discard. • To open a file in binary mode, a “b” should be appended to the mode string. 22
Unix C + GNU HTML Perl Python
Formated File IO • The fprintf and fscanf functions correspond to their printf and scanf counterpart. – int fscanf(FILE *fp, char *format, ...) – int fprintf(FILE *fp, char *format, ...)
COMP 206 - Joseph Vybihal 2007 Software Systems
• The only difference is that fscanf and fprintf explicitly require a file pointer as their first parameter. – printf assumes output should go to stdout – scanf assumes that input should come stdin 23
Unix C + GNU HTML Perl Python
Line IO • The fgets and fputs functions can be used to manipulate lines of IO. – char* fgets(char *line, int maxline, FILE *fp) – int fputs(char *line, file *fp);
COMP 206 - Joseph Vybihal 2007 Software Systems
• A line of text is defined as a character of array termined with either an end-of-line character or a null character, • The fgets function reads the next line and stores in the provided character array. – Note that the function does not create a character array. – Before calling this function, you should have allocated a memory space large enough to review the input. – At most maxline characters are read.
• The fputs function writes a new string into the specified file. 24
Unix C + GNU HTML Perl Python
Character I/O • int fgetc(FILE *) – Return the ASCII value of the character at ptr
• int fputc(int c, FILE *) – Writes an ASCII value to the file pointer COMP 206 - Joseph Vybihal 2007 Software Systems
25
Unix C + GNU HTML Perl Python
Closing a file • Once you've finished with a file, you should use the fclose function to close the file. int fclose(FILE *fp);
COMP 206 - Joseph Vybihal 2007 Software Systems
• This will write any data that might have remained in the buffer. • This is particularly a good idea if you open and close files often in your application. – A process can only open a specific number of files at a time. 26
Unix C + GNU HTML Perl Python
End of File • int feof(FILE *) – Tests if FILE * is at the end of the file – Returns 0 if not, 1 if it is.
COMP 206 - Joseph Vybihal 2007 Software Systems
27
File I/O Function Example Unix C + GNU HTML Perl Python
#include <stdlib.h> #include <stdio.h> void main(void) { FILE *file_ptr; char line[300];
Why?
rt, wt, at rb, wt, ab r+wt, w+wb
file_ptr = fopen(“c:\\mystuff.txt”, “rt”); if (file_ptr == NULL) { printf(“Error”); return; } COMP 206 - Joseph Vybihal 2007 Software Systems
fgets(line,299,file_ptr); while(!feof(file_ptr)) { printf(“%s”, line); fgets(line,299,file_ptr); } fclose(file_ptr); }
How does this work?
28
Unix C + GNU HTML Perl Python
Question • Copy the contents of one text file into another one.
COMP 206 - Joseph Vybihal 2007 Software Systems
29
Unix C + GNU HTML Perl Python
Other File I/O • fread(&VAR, BYTES, QUANTITY, FILE); • fwrite(&VAR, BYTES, QUANTITY, FILE); • fseek(FILE, OFFSET, DIRECTION);
COMP 206 - Joseph Vybihal 2007 Software Systems
SEEK_SET SEEK_CUR SEEK_END
0 1 2
30
Unix C + GNU HTML Perl Python
Questions • What does a binary file look like physically on disk? • How does FSEEK work?
COMP 206 - Joseph Vybihal 2007 Software Systems
31
Unix C + GNU HTML Perl Python
COMP 206 - Joseph Vybihal 2007 Software Systems
#include <stdlib.h>
32
Unix C + GNU HTML Perl Python
#include <stdlib.h> • • • •
#define NULL 0 #define EXIT_FAILURE 1 #define EXIT_SUCCESS 0 int x = rand(void); // 0 to RAND_MAX
COMP 206 - Joseph Vybihal 2007 Software Systems
33
Constants Unix C + GNU HTML Perl Python
COMP 206 - Joseph Vybihal 2007 Software Systems
34
functions Unix C + GNU HTML Perl Python
** later
35 COMP 206 - Joseph Vybihal 2007 Software Systems
**
Unix C + GNU HTML Perl Python
Example #include <stdlib.h> #include <stdio.h> int main(void) { char string[200];
COMP 206 - Joseph Vybihal 2007 Software Systems
printf(“Please input a command: ”); gets(string); system(string); }
36
Unix C + GNU HTML Perl Python
Example #include <stdio.h> /* for the printf and scanf */ #include <stdlib.h> /* for rand and EXIT */ int main(void) { int randomValue, factor; printf(“Enter factor: ”); scanf(“%d”, &factor); randomValue = rand();
COMP 206 - Joseph Vybihal 2007 Software Systems
printf(“Random value = %d\n”, factor*randomValue); return EXIT_SUCCESS; }
37
Unix C + GNU HTML Perl Python
Part 2 COMP 206 - Joseph Vybihal 2007 Software Systems
#include <math.h>
38
Unix C + GNU HTML Perl Python
#include <math.h> • Standard math:
COMP 206 - Joseph Vybihal 2007 Software Systems
– – – – – –
double y = sqrt(double); double y = pow(base,exponent); int x = abs(int); double y = fabs(double); double x = floor(double); double x = ceil(double);
• Trigonometry: – sin, cos, tan, asin, acos, atan 39
Unix C + GNU HTML Perl Python
COMP 206 - Joseph Vybihal 2007 Software Systems
40
Unix C + GNU HTML Perl Python
Part 3 COMP 206 - Joseph Vybihal 2007 Software Systems
#include
41
Unix C + GNU HTML Perl Python
#include • Case manipulation: – int c = toupper(int); – int c = tolower(int);
• Character testing: COMP 206 - Joseph Vybihal 2007 Software Systems
– int x = isalpha(int); – int x = isalphanum(int); – int x = isdigit(int);
Note: char is in int. 42
Unix C + GNU HTML Perl Python
COMP 206 - Joseph Vybihal 2007 Software Systems
43
Unix C + GNU HTML Perl Python
Part 4
44 COMP 206 - Joseph Vybihal 2007 Software Systems
At Home
Unix C + GNU HTML Perl Python
Question • Write a program that reads in a text file and carries out statistics on it:
COMP 206 - Joseph Vybihal 2007 Software Systems
– How many vowels – How many punctuations – How many digits – How many letters in total – How many characters in total – How many white spaces • Note: characters = letters + white spaces + punctuations 45
Unix C + GNU HTML Perl Python
Try this • Write a program that reads a text file into a 2D array and then asks the user to input two words. Then, from within another function, scan the 2D array for the first word and replace it by the second word.
COMP 206 - Joseph Vybihal 2007 Software Systems
46