CSCB09 Software Tools and Systems
Programming
C – I/O
Nandita Vijaykumar/Marcelo Ponce
Winter 2022
Department of Computer and Mathematical Sciences - UTSC
Nandita
Vijaykumar/Marcelo Ponce CSCB09 Software Tools & Systems
Programming – Week 02: C & I/OCMS/UTS - Winter 2022 1 / 17
C Programming Language
• Widely used and important.
• Impressive for a language that
was invented in 1972!
• We will use the standard c99.
• Used in a huge range of
disciplines, from OS,
controlers, upto applications
in scientific disciplines. https://www.tiobe.com/tiobe-index/
Nandita
Vijaykumar/Marcelo Ponce CSCB09 Software Tools & Systems
Programming – Week 02: C & I/OCMS/UTS - Winter 2022 2 / 17
Why C?
• What do you think about C so far ...?
• Disappointed about the lack of support for strings, arrays, etc.... ?
• Turns out you also have to do much of memory management manually ....
Your ride with Python
• Cruise control
• Seat heating
• Cup holders
• Automatic transmission
Your ride with C
• Performance and speed
• But... no amenities... (?!)
• and it only comes with a stickshift...
• ...
• compiled language
Nandita
Vijaykumar/Marcelo Ponce CSCB09 Software Tools & Systems
Programming – Week 02: C & I/OCMS/UTS - Winter 2022 3 / 17
Why C?
• What do you think about C so far ...?
• Disappointed about the lack of support for strings, arrays, etc.... ?
• Turns out you also have to do much of memory management manually ....
Your ride with Python
• Cruise control
• Seat heating
• Cup holders
• Automatic transmission
Your ride with C
• Performance and speed
• But... no amenities... (?!)
• and it only comes with a stickshift...
• ...
•
compiled languageNandita Vijaykumar/Marcelo Ponce CSCB09 Software Tools
& Systems Programming – Week 02: C & I/OCMS/UTS - Winter 2022 3
/ 17
I/O
I/O between User and Program
• From A48, output to terminal (STDOUT): printf()
• STDOUT, STDERR, STDIN
• What about input from keyboard?
• Command line input
• STDIN input
Nandita
Vijaykumar/Marcelo Ponce CSCB09 Software Tools & Systems
Programming – Week 02: C & I/OCMS/UTS - Winter 2022 4 / 17
Exercise 1
• Implement a simplified version of the wc (word count)
utility with only one functionality:
• Your program will use scanf to count the number of
words (strings) the user inputs and print the count to
the screen.
• The user signals the end of input (EOF = End of File) by
hitting Ctrl-D (Ctrl-Z on Windows) on a new line
• (For simplicity you may assume no input word is longer
than 20 characters)
#include
int main() {
// YOUR CODE HERE !!!
}
Nandita
Vijaykumar/Marcelo Ponce CSCB09 Software Tools & Systems
Programming – Week 02: C & I/OCMS/UTS - Winter 2022 5 / 17
Exercise 1 – solution
1 #include
2
3 int main(){
4 char input_string [20];
5 int counter = 0;
6
7 while (scanf("%s", input_string) != EOF) {
8 counter = counter + 1;
9 }
10
11 printf("%d\n", counter);
12 }
Nandita
Vijaykumar/Marcelo Ponce CSCB09 Software Tools & Systems
Programming – Week 02: C & I/OCMS/UTS - Winter 2022 6 / 17
CLA: Command Line Arguments
Command Line Arguments
int main(int argc, char **argv)
• What is: char **arg ?
Nandita
Vijaykumar/Marcelo Ponce CSCB09 Software Tools & Systems
Programming – Week 02: C & I/OCMS/UTS - Winter 2022 7 / 17
Quick review of string and array facts
• What is a string?
• Array of chars
• Null character (’\0’) denotes the string end
• C library functions to operate on strings,
char name [20];
strncpy(name , "CSCB09", 20);
// NOT: name = "CSCB09 ";
• What is the type of name? → char*
• How would you declare an array of 30 strings?
char ∗ name_list [30];
name_list [0] = name;
• What is the type of name_list? → char**
Nandita
Vijaykumar/Marcelo Ponce CSCB09 Software Tools & Systems
Programming – Week 02: C & I/OCMS/UTS - Winter 2022 8 / 17
Quick review of string and array facts
• What is a string?
• Array of chars
• Null character (’\0’) denotes the string end
• C library functions to operate on strings,
char name [20];
strncpy(name , "CSCB09", 20);
// NOT: name = "CSCB09 ";
• What is the type of name?
→ char*
• How would you declare an array of 30 strings?
char ∗ name_list [30];
name_list [0] = name;
• What is the type of name_list? → char**
Nandita
Vijaykumar/Marcelo Ponce CSCB09 Software Tools & Systems
Programming – Week 02: C & I/OCMS/UTS - Winter 2022 8 / 17
Quick review of string and array facts
• What is a string?
• Array of chars
• Null character (’\0’) denotes the string end
• C library functions to operate on strings,
char name [20];
strncpy(name , "CSCB09", 20);
// NOT: name = "CSCB09 ";
• What is the type of name? → char*
• How would you declare an array of 30 strings?
char ∗ name_list [30];
name_list [0] = name;
• What is the type of name_list? → char**
Nandita
Vijaykumar/Marcelo Ponce CSCB09 Software Tools & Systems
Programming – Week 02: C & I/OCMS/UTS - Winter 2022 8 / 17
Quick review of string and array facts
• What is a string?
• Array of chars
• Null character (’\0’) denotes the string end
• C library functions to operate on strings,
char name [20];
strncpy(name , "CSCB09", 20);
// NOT: name = "CSCB09 ";
• What is the type of name? → char*
• How would you declare an array of 30 strings?
char ∗ name_list [30];
name_list [0] = name;
• What is the type of name_list? → char**
Nandita
Vijaykumar/Marcelo Ponce CSCB09 Software Tools & Systems
Programming – Week 02: C & I/OCMS/UTS - Winter 2022 8 / 17
Quick review of string and array facts
• What is a string?
• Array of chars
• Null character (’\0’) denotes the string end
• C library functions to operate on strings,
char name [20];
strncpy(name , "CSCB09", 20);
// NOT: name = "CSCB09 ";
• What is the type of name? → char*
• How would you declare an array of 30 strings?
char ∗ name_list [30];
name_list [0] = name;
• What is the type of name_list? → char**
Nandita
Vijaykumar/Marcelo Ponce CSCB09 Software Tools & Systems
Programming – Week 02: C & I/OCMS/UTS - Winter 2022 8 / 17
Quick review of string and array facts
• What is a string?
• Array of chars
• Null character (’\0’) denotes the string end
• C library functions to operate on strings,
char name [20];
strncpy(name , "CSCB09", 20);
// NOT: name = "CSCB09 ";
• What is the type of name? → char*
• How would you declare an array of 30 strings?
char ∗ name_list [30];
name_list [0] = name;
• What is the type of name_list?
→ char**
Nandita
Vijaykumar/Marcelo Ponce CSCB09 Software Tools & Systems
Programming – Week 02: C & I/OCMS/UTS - Winter 2022 8 / 17
Quick review of string and array facts
• What is a string?
• Array of chars
• Null character (’\0’) denotes the string end
• C library functions to operate on strings,
char name [20];
strncpy(name , "CSCB09", 20);
// NOT: name = "CSCB09 ";
• What is the type of name? → char*
• How would you declare an array of 30 strings?
char ∗ name_list [30];
name_list [0] = name;
• What is the type of name_list? → char**
Nandita
Vijaykumar/Marcelo Ponce CSCB09 Software Tools & Systems
Programming – Week 02: C & I/OCMS/UTS - Winter 2022 8 / 17
Command Line Arguments
int main(int argc, char **argv)
• What is: char **arg ?
• An array of strings
• Need argc to know how many strings to expect in array.
• argv[1] is the first command line argument
• and so, what is argv[0] then?
Nandita
Vijaykumar/Marcelo Ponce CSCB09 Software Tools & Systems
Programming – Week 02: C & I/OCMS/UTS - Winter 2022 9 / 17
Command Line Arguments
int main(int argc, char **argv)
• What is: char **arg ?
• An array of strings
• Need argc to know how many strings to expect in array.
• argv[1] is the first command line argument
• and so, what is argv[0] then?
Nandita
Vijaykumar/Marcelo Ponce CSCB09 Software Tools & Systems
Programming – Week 02: C & I/OCMS/UTS - Winter 2022 9 / 17
Exercise 2
Adding CLAs to wc...
• Extend your word count program to take exactly one command line argument.
• If the commandline argument is “-w” your program will print the number of words
in user input (as before)
• If the commandline argument is “-c” your program will print the number of
characters in user input
• As before, the user signals the end of input (EOF = End of File) by hitting Ctrl-D
(Ctrl-Z on Windows) on a new line
• (For simplicity you may assume that the user does provide exactly one command
line argument).
Nandita
Vijaykumar/Marcelo Ponce CSCB09 Software Tools & Systems
Programming – Week 02: C & I/OCMS/UTS - Winter 2022 10 / 17
Exercise 2 – solution
#include // basic IO, eg. printf
#include // for strings manipulation
int main(int argc , char ∗∗ argv) {
char input_string [20];
char input_c;
int counter = 0;
// CLA processing: −w _ words
if (strcmp(argv[1], "−w") == 0) {
while (scanf("%s", input_string) != EOF) {
counter = counter + 1; // increment counter
}
printf("%d \n", counter);
return 0;
}
1 #include // basic IO, eg. printf
2 #include // for strings manipulation
3
4 int main(int argc , char ∗∗ argv) {
5
6 char input_string [20];
7 char input_c;
8 int counter = 0;
9
10 // CLA processing: −w _ words
11 if (strcmp(argv[1], "−w") == 0) {
12 while (scanf("%s", input_string) != EOF) {
13 counter = counter + 1; // increment counter
14 }
15 printf("%d \n", counter);
16 return 0;
17 }
18
19 // CLA processing: −c _ characters
20 if (strcmp(argv[1], "−c") == 0) {
21 while (scanf("%c", &input_c) != EOF) {
22 counter = counter + 1; // increment counter
23 }
24 printf("%d \n", counter);
25 return 0;
26 }
27
28 printf("Invalid argument!");
29 }
1. In real program:
check that argc==2
2. In real program: read
at most 20 chars
scanf("%20s", ...)
3. Improvements: ...
Nandita
Vijaykumar/Marcelo Ponce CSCB09 Software Tools & Systems
Programming – Week 02: C & I/OCMS/UTS - Winter 2022 11 / 17
Exercise 2 – solution
#include // basic IO, eg. printf
#include // for strings manipulation
int main(int argc , char ∗∗ argv) {
char input_string [20];
char input_c;
int counter = 0;
// CLA processing: −w _ words
if (strcmp(argv[1], "−w") == 0) {
while (scanf("%s", input_string) != EOF) {
counter = counter + 1; // increment counter
}
printf("%d \n", counter);
return 0;
}
1 #include // basic IO, eg. printf
2 #include // for strings manipulation
3
4 int main(int argc , char ∗∗ argv) {
5
6 char inpu _string [20];
7 char input_c;
8 int counter = 0;
9
10 // CLA rocess : −w _ words
11 if (strcmp(argv[1], "−w") == 0) {
12 while (scanf("%s", input_string) != EOF) {
13 counter = counter + 1; // increment counter
14 }
15 printf("%d \n", counter);
16 return 0;
17 }
18
19 // CLA processing: −c _ characters
20 if (st cmp(argv[1], "−c") == 0) {
21 while (scanf("%c", &input_c) != EOF) {
22 counter = counter + 1; // increment counter
23 }
24 printf("%d \n", counter);
25 return 0;
26 }
27
28 printf("Invalid argument!");
29 }
1. In real program:
check that argc==2
2. In real program: read
at most 20 chars
scanf("%20s", ...)
3. Improvements: ...
Nandita
Vijaykumar/Marcelo Ponce CSCB09 Software Tools & Systems
Programming – Week 02: C & I/OCMS/UTS - Winter 2022 11 / 17
I/O between user and program
• From A48, output to terminal (STDOUT): printf()
• What about input from terminal?
• Command line input
• STDIN input
• What about file I/O?
• Turns out you were doing file I/O already with printf & scanf
int printf(const char *format, ...) // writes to STDOUT
int scanf(const char *format, ...) // Reads from STDIN
• For general file I/O need to specify where to read to / write from:
int fprintf(FILE *stream, const char *format, ...)
int fscanf(FILE *stream, const char *format, ...)
Nandita
Vijaykumar/Marcelo Ponce CSCB09 Software Tools & Systems
Programming – Week 02: C & I/OCMS/UTS - Winter 2022 12 / 17
I/O between user and program
• From A48, output to terminal (STDOUT): printf()
• What about input from terminal?
• Command line input
• STDIN input
• What about file I/O?
• Turns out you were doing file I/O already with printf & scanf
int printf(const char *format, ...) // writes to STDOUT
int scanf(const char *format, ...) // Reads from STDIN
• For general file I/O need to specify where to read to / write from:
int fprintf(FILE *stream, const char *format, ...)
int fscanf(FILE *stream, const char *format, ...)
Nandita
Vijaykumar/Marcelo Ponce CSCB09 Software Tools & Systems
Programming – Week 02: C & I/OCMS/UTS - Winter 2022 12 / 17
I/O between user and program
• From A48, output to terminal (STDOUT): printf()
• What about input from terminal?
• Command line input
• STDIN input
• What about file I/O?
• Turns out you were doing file I/O already with printf & scanf
int printf(const char *format, ...) // writes to STDOUT
int scanf(const char *format, ...) // Reads from STDIN
• For general file I/O need to specify where to read to / write from:
int fprintf(FILE *stream, const char *format, ...)
int fscanf(FILE *stream, const char *format, ...)
Nandita
Vijaykumar/Marcelo Ponce CSCB09 Software Tools & Systems
Programming – Week 02: C & I/OCMS/UTS - Winter 2022 12 / 17
I/O between user and program
• From A48, output to terminal (STDOUT): printf()
• What about input from terminal?
• Command line input
• STDIN input
• What about file I/O?
• Turns out you were doing file I/O already with printf & scanf
int printf(const char *format, ...) // writes to STDOUT
int scanf(const char *format, ...) // Reads from STDIN
• For general file I/O need to specify where to read to / write from:
int fprintf(FILE *stream, const char *format, ...)
int fscanf(FILE *stream, const char *format, ...)
Nandita
Vijaykumar/Marcelo Ponce CSCB09 Software Tools & Systems
Programming – Week 02: C & I/OCMS/UTS - Winter 2022 12 / 17
I/O between user and program
• From A48, output to terminal (STDOUT): printf()
• What about input from terminal?
• Command line input
• STDIN input
• What about file I/O?
• Turns out you were doing file I/O already with printf & scanf
int printf(const char *format, ...) // writes to STDOUT
int scanf(const char *format, ...) // Reads from STDIN
• For general file I/O need to specify where to read to / write from:
int fprintf(FILE *stream, const char *format, ...)
int fscanf(FILE *stream, const char *format, ...)
Nandita
Vijaykumar/Marcelo Ponce CSCB09 Software Tools & Systems
Programming – Week 02: C & I/OCMS/UTS - Winter 2022 12 / 17
I/O between user and program
• From A48, output to terminal (STDOUT): printf()
• What about input from terminal?
• Command line input
• STDIN input
• What about file I/O?
• Turns out you were doing file I/O already with printf & scanf
int printf(const char *format, ...) // writes to STDOUT
int scanf(const char *format, ...) // Reads from STDIN
• For general file I/O need to specify where to read to / write from:
int fprintf(FILE *stream, const char *format, ...)
int fscanf(FILE *stream, const char *format, ...)
Nandita
Vijaykumar/Marcelo Ponce CSCB09 Software Tools & Systems
Programming – Week 02: C & I/OCMS/UTS - Winter 2022 12 / 17
File interfaces in C/Unix
• Two main mechanisms for managing open files:
• File descriptors (low-level, managed by OS):
• Each open file is identified by a small integer
• STDIN is 0, STDOUT is 1
• Use for pipes, sockets (will see later what those are ...)
• (Remember how I said in first lecture that everything is a file ..?)
• File pointers (aka streams, file handles) for regular files:
• A C language construct for easier working with file descriptors
• You use a pointer to a file structure (FILE *) as handle to a file.
• The file struct contains a file descriptor and a buffer.
• Use for regular files
Nandita
Vijaykumar/Marcelo Ponce CSCB09 Software Tools & Systems
Programming – Week 02: C & I/OCMS/UTS - Winter 2022 13 / 17
File interfaces in C/Unix
• Two main mechanisms for managing open files:
• File descriptors (low-level, managed by OS):
• Each open file is identified by a small integer
• STDIN is 0, STDOUT is 1
• Use for pipes, sockets (will see later what those are ...)
• (Remember how I said in first lecture that everything is a file ..?)
• File pointers (aka streams, file handles) for regular files:
• A C language construct for easier working with file descriptors
• You use a pointer to a file structure (FILE *) as handle to a file.
• The file struct contains a file descriptor and a buffer.
• Use for regular files
Nandita
Vijaykumar/Marcelo Ponce CSCB09 Software Tools & Systems
Programming – Week 02: C & I/OCMS/UTS - Winter 2022 13 / 17
File interfaces in C/Unix
• Two main mechanisms for managing open files:
• File descriptors (low-level, managed by OS):
• Each open file is identified by a small integer
• STDIN is 0, STDOUT is 1
• Use for pipes, sockets (will see later what those are ...)
• (Remember how I said in first lecture that everything is a file ..?)
• File pointers (aka streams, file handles) for regular files:
• A C language construct for easier working with file descriptors
• You use a pointer to a file structure (FILE *) as handle to a file.
• The file struct contains a file descriptor and a buffer.
• Use for regular files
Nandita
Vijaykumar/Marcelo Ponce CSCB09 Software Tools & Systems
Programming – Week 02: C & I/OCMS/UTS - Winter 2022 13 / 17
Operating on regular files
• Let’s say we want to read from/write to a file my_file.txt
• For such, we need a file handle: FILE *
• A file first needs to be opened to obtain a FILE *
FILE *fopen(const char *filename, const char *mode)
• filename identifies the file to open
• mode tells how to open the file:
"r" for reading, "w" for writing, "a" for appending
• returns a pointer to a FILE struct which is the handle to the file.
• To close a file: void fclose(FILE *stream);
FILE ∗fp = fopen("my_file.txt", "w");
fprintf(fp, "Hello!\n");
fclose(fp);
Nandita
Vijaykumar/Marcelo Ponce CSCB09 Software Tools & Systems
Programming – Week 02: C & I/OCMS/UTS - Winter 2022 14 / 17
Operating on regular files
• Let’s say we want to read from/write to a file my_file.txt
• For such, we need a file handle: FILE *
• A file first needs to be opened to obtain a FILE *
FILE *fopen(const char *filename, const char *mode)
• filename identifies the file to open
• mode tells how to open the file:
"r" for reading, "w" for writing, "a" for appending
• returns a pointer to a FILE struct which is the handle to the file.
• To close a file: void fclose(FILE *stream);
FILE ∗fp = fopen("my_file.txt", "w");
fprintf(fp, "Hello!\n");
fclose(fp);
Nandita
Vijaykumar/Marcelo Ponce CSCB09 Software Tools & Systems
Programming – Week 02: C & I/OCMS/UTS - Winter 2022 14 / 17
Exercise 3
• Extend our Exercise 1 word count program to take exactly one command line
argument, which is a file name.
• This program will open the file, count the number of words in it and print the
count.
1 #include
2
3 int main(){
4 char input_string [20];
5 int counter = 0;
6
7 while (scanf("%s", input_string) != EOF) {
8 counter = counter + 1;
9 }
10
11 printf("%d\n", counter);
12 }
FILE ∗ fopen(const char ∗path , const char ∗ mode);
int fscanf(FILE ∗stream , const char ∗format ,...);
int fclose(FILE ∗ stream);
Nandita
Vijaykumar/Marcelo Ponce CSCB09 Software Tools & Systems
Programming – Week 02: C & I/OCMS/UTS - Winter 2022 15 / 17
Exercise 3
• Extend our Exercise 1 word count program to take exactly one command line
argument, which is a file name.
• This program will open the file, count the number of words in it and print the
count.
1 #include
2
3 int main(){
4 char input_string [20];
5 int counter = 0;
6
7 while (scanf("%s", input_string) != EOF) {
8 counter = counter + 1;
9 }
10
11 printf("%d\n", counter);
12 }
FILE ∗ fopen(const char ∗path , const char ∗ mode);
int fscanf(FILE ∗stream , const char ∗format ,...);
int fclose(FILE ∗ stream);
Nandita
Vijaykumar/Marcelo Ponce CSCB09 Software Tools & Systems
Programming – Week 02: C & I/OCMS/UTS - Winter 2022 15 / 17
Exercise 3 – solution
#include
int main(int argc , char ∗∗ argv){
char input_string [20];
int wc = 0;
FILE ∗ input_file;
input_file = fopen(argv[1], "r");
// In real programs , error check after opening file:
// if (input_file == NULL) then there was an error
while( fscanf(input_file , "%s", input_string) != EOF) {
wc = wc + 1;
}
fclose(input_file);
printf("%d\n", wc);
}
Nandita
Vijaykumar/Marcelo Ponce CSCB09 Software Tools & Systems
Programming – Week 02: C & I/OCMS/UTS - Winter 2022 16 / 17
Basic file I/O functions (stdio.h)
FILE ∗ fopen(const char ∗path , const char ∗ mode);
int fscanf(FILE ∗stream , const char ∗format ,...);
char ∗ fgets(char ∗s, int size , FILE ∗ stream);
char fgetc(FILE ∗ stream);
int fprintf(FILE ∗stream , const char ∗format ,...)
int fputs(const char ∗str , FILE ∗ stream);
int fseek(FILE ∗stream , long int offset , int whence);
void rewind(FILE ∗ stream);
int fclose(FILE ∗ stream);
Nandita
Vijaykumar/Marcelo Ponce CSCB09 Software Tools & Systems
Programming – Week 02: C & I/OCMS/UTS - Winter 2022 17 / 17