Lecture 7: Files opening/closing files reading/writing strings reading/writing numbers (conversion to ASCII) command line arguments
Lecture 5: Files, I/O 0IGXYVI*MPIW 0 opening/closing files reading/writing strings reading/writing numbers (conversion to ASCII) command line arguments files are treated as data streams keyboard, screen, printer,... are also treated as streams from the C perspective writing/reading from a file is the same as writing to the screen/reading from the keyboard
File opening and closing Basic I/O functions included in stdio.h Example: printf function (writes something to stdout)
File opening and closing Basic I/O functions included in stdio.h Example: printf function (writes something to stdout) Basic file operations: FILE *in-file; in-file = fopen\( input.txt, r \);... /* read stuff from input.txt */ status = fclose(in-file); if (status!= 0) printf\( File closing error.\\n \);
eclare variable of type (pointer to) FILE File opening and closing Basic I/O functions included in stdio.h Example: printf function (writes something to stdout) Basic file operations: FILE *in-file; in-file = fopen(input.txt, r );... /* read stuff from input.txt */ status = fclose(in-file); if (status!= 0) printf (File closing error.\n);
File opening and closing Basic I/O functions included in stdio.h Example: printf function (writes something to stdout) Basic file operations: fopen opens the file input.txt in read mode ( r ) and returns a FILE *in-file; pointer to that file. in-file is now linked to input.txt. in-file = fopen\( input.txt, r \);... /* read stuff from input.txt */ status = fclose(in-file); if (status!= 0) printf\( File closing error.\\n \); Declare variable of type (pointer to) FILE
File opening and closing Basic I/O functions included in stdio.h Example: printf function (writes something to stdout) Basic file operations: Declare variable of type (pointer to) FILE fopen opens the file input.txt in read mode ( r ) and returns a FILE *in-file; pointer to that file. in-file is now linked to input.txt. in-file = fopen\( input.txt, r \);... /* read stuff from input.txt */ status = fclose(in-file); if (status!= 0) printf\( File closing error.\\n \); fclose closes the file connected to in-file and if successful returns 0.
File opening and closing Basic I/O functions included in stdio.h Example: printf function (writes something to stdout) Basic file operations: Declare variable of type (pointer to) FILE fopen opens the file input.txt in read mode ( r ) and returns a FILE *in-file; pointer to that file. in-file is now linked to input.txt. in-file = fopen\( input.txt, r \);... /* read stuff from input.txt */ status = fclose(in-file); if (status!= 0) printf\( File closing error.\\n \); fclose closes the file connected to in-file and if successful returns 0. There are 3 pre-opened and pre-defined files: stdin (keyboard), stdout, stderr (screen)!
Possible mode specifiers for fopen r Open a text file for reading w Create a text file for writing a Append to a text file rb Open a binary file for reading wb Open a binary file for writing ab Append to a binary file r+ Open a text file for read/write w+ Create a text file for read/write a+ Append or create a text file for read/write r+b Open a binary file for read/write w+b Create a binary file for read/write a+b Append a binary file for read/write (Binary files are beyond the scope of this course)
Read/write functions (ASCII files) for single characters: int fgetc(file *file-var) int fputc(int character, FILE *file-var)
Read/write functions (ASCII files) for single characters: int fgetc(file *file-var) int fputc(int character, FILE *file-var) for whole strings: char *fgets(char *string, int size, FILE *file-var) int fputs(char *string, FILE *file-var)
Read/write functions (ASCII files) for single characters: int fgetc(file *file-var) int fputc(int character, FILE *file-var) for whole strings: char *fgets(char *string, int size, FILE *file-var) int fputs(char *string, FILE *file-var) for everythings else (conversion routines): int scanf(char *format,...) int fscanf(file *file-var, char *format,...) int sscanf(char *buffer, char *format,...) int printf(char *format,...) int fprintf(file *file-var, char *format,...) int sprintf(char *buffer, char *format,...) (stdin) (file) (string) (stdout) (file) (string)
Read/write functions (ASCII files) for single characters: int fgetc(file *file-var) int fputc(int character, FILE *file-var) for whole strings: char *fgets(char *string, int size, FILE *file-var) int fputs(char *string, FILE *file-var) for everythings else (conversion routines): int scanf(char *format,...) int fscanf(file *file-var, char *format,...) int sscanf(char *buffer, char *format,...) int printf(char *format,...) int fprintf(file *file-var, char *format,...) int sprintf(char *buffer, char *format,...) (stdin) (file) (string) (stdout) (file) (string) Special constant EOF (end of file) also defined in stdio.h
Read/write functions (ASCII files) for single characters: int fgetc(file *file-var) int fputc(int character, FILE *file-var) for whole strings: char *fgets(char *string, int size, FILE *file-var) int fputs(char *string, FILE *file-var) for everythings else (conversion routines): int scanf(char *format,...) int fscanf(file *file-var, char *format,...) int sscanf(char *buffer, char *format,...) int printf(char *format,...) int fprintf(file *file-var, char *format,...) int sprintf(char *buffer, char *format,...) (stdin) (file) (string) (stdout) (file) (string) Special constant EOF (end of file) also defined in stdio.h (for more information see e.g.: http://www.elook.org/programming/c/stdio.html )
Example: read string from keyboard fgets(string-name, sizeof(string-name), stdin)
Example: read string from keyboard fgets(string-name, sizeof(string-name), stdin) sizeof function: returns the size (in bytes) of something
Example: read string from keyboard fgets(string-name, sizeof(string-name), stdin) sizeof function: returns the size (in bytes) of something #include<string.h> char line[100]; int main() { printf\\\( Enter a word: \\\); fgets(line, sizeof(line), stdin); printf\\\( Length of word is %d\\\\n, strlen(line)); return(0);
Example: read string from keyboard fgets(string-name, sizeof(string-name), stdin) sizeof function: returns the size (in bytes) of something #include<string.h> char line[100]; int main() { printf\\\( Enter a word: \\\); fgets(line, sizeof(line), stdin); printf\\\( Length of word is %d\\\\n, strlen(line)); return(0); fgets includes the newline character '\n' in the string
Example: read string from keyboard (2) #include<string.h> char line[100]; int main() { printf\( Enter a word: \); fgets(line, sizeof(line), stdin); /* Better: strip of the newline character */ line[strlen(line)-1] = '\0'; printf\( Length of word is %d\\n, strlen(line)); return(0);
Example: read string from keyboard (2) #include<string.h> char line[100]; int main() { printf\( Enter a word: \); fgets(line, sizeof(line), stdin); /* Better: strip of the newline character */ line[strlen(line)-1] = '\0'; printf\( Length of word is %d\\n, strlen(line)); return(0);
Example: read string from keyboard (2) #include<string.h> char line[100]; int main() { printf\( Enter a word: \); fgets(line, sizeof(line), stdin); /* Better: strip of the newline character */ line[strlen(line)-1] = '\0'; printf\( Length of word is %d\\n, strlen(line)); return(0); Notice the difference between size and length sizeof(line) = number of bytes allocated for line = 100 strlen(line) = number of characters before '\0' (end of string)
Reading numbers using scanf int value; printf\( Enter value: \); scanf\( %d, &value\);
Reading numbers using scanf int value; printf\( Enter value: \); scanf\( %d, &value\); Note: argument of scanf has to be a pointer!
Reading numbers using scanf int value; printf\( Enter value: \); scanf\( %d, &value\); Can be unreliable. If input doesn't match required format the input stream can be messed up! Note: argument of scanf has to be a pointer!
Reading numbers using scanf int value; printf\( Enter value: \); scanf\( %d, &value\); Can be unreliable. If input doesn't match required format the input stream can be messed up! Note: argument of scanf has to be a pointer! Better: using fgets/sscanf int value; char line[100]; printf\( Enter value: \); fgets(line,sizeof(line), stdin); sscanf\(line, %d, &value\);
Reading numbers using scanf int value; printf\( Enter value: \); scanf\( %d, &value\); Can be unreliable. If input doesn't match required format the input stream can be messed up! Note: argument of scanf has to be a pointer! Better: using fgets/sscanf int value; char line[100]; printf\( Enter value: \); fgets(line,sizeof(line), stdin); sscanf\(line, %d, &value\); If format doesn't match input, the input string is still available for further analysis!
Reading numbers using scanf int value; printf\( Enter value: \); scanf\( %d, &value\); Can be unreliable. If input doesn't match required format the input stream can be messed up! Note: argument of scanf has to be a pointer! Better: using fgets/sscanf int value; char line[100]; printf\( Enter value: \); fgets(line,sizeof(line), stdin); sscanf\(line, %d, &value\); If format doesn't match input, the input string is still available for further analysis! Format specifiers for scanf, sscanf, fscanf are the same as for printf, sprintf, fprinf, except for double scanf requires %lf!
Possible problem with scanf main() { int input=0; while(input<1) { printf\( Please enter a positive integer: \); scanf\( %d,&input); printf\( The number you entered was: %d\\n,&input); return(0); What happens if the user (accidentally) enters a character? Why?
Possible problem with scanf main() { int input=0; while(input<1) { printf\( Please enter a positive integer: \); scanf\( %d,&input); printf\( The number you entered was: %d\\n,&input); return(0); What happens if the user (accidentally) enters a character? Why? if scanf cannot match input to format string it does not advance the input stream, i.e. in the above example scanf reads the same (wrong) input again and again.
Command line arguments main(int argc, char *argv[]) { printf\( Number of command line arguments: %d\\n, argc-1\); while (argc > 1){ printf\( %s, argv[1]\); --argc; ++argv;
Command line arguments main(int argc, char *argv[]) { printf\( Number of command line arguments: %d\\n, argc-1\); while (argc > 1){ printf\( %s, argv[1]\); --argc; ++argv; Always declare main like this if you want to use command line arguments!
Command line arguments main(int argc, char *argv[]) { printf\( Number of command line arguments: %d\\n, argc-1\); while (argc > 1){ printf\( %s, argv[1]\); --argc; ++argv; argc indicates the number of command line arguments argv[i] contains the i'th argument argv[0] contains the program name argc >= 1 Always declare main like this if you want to use command line arguments!
Example: password protected program #include<stdlib.h> #include<string.h> char passwd[8] = "CP3065JS"; main(int argc, char *argv[]) { if (argc!= 2) { printf("specify password!\n"); exit(1); if ( strcmp(argv[1], passwd) == 0 ) printf("access Permitted\n"); else { printf("access denied\n"); exit(1); /* now the real program starts */ printf("this program doesn't really do anything.\n"); return(0);
Example: searc hfile.c /* Search specified file for specified character. */ #include <stdio.h> #include <stdlib.h> main(int argc, char *argv[]) { FILE *fp; /* file pointer */ char ch; searchfile.c: searches for specified character in specified file /* check for correct number of command line arguments */ if(argc!=3) { printf("usage: %s <filename> <ch>\n", argv[0]); exit(1); /* exits function is part of stdlib.h */ /* open file for input */ fp = fopen(argv[1], "r"); if(fp == NULL) { printf("cannot open file \n"); exit(1); /* look for character */ while ( (ch=fgetc(fp) )!= EOF ) if (ch == *argv[2]) { printf("%c found\n", ch); break; fclose(fp); return(0);
specified character Example: searc hfile.c /* Search specified file for specified character. */ #include <stdio.h> #include <stdlib.h> main(int argc, char *argv[]) { FILE *fp; /* file pointer */ char ch; searchfile.c: searches for in specified file /* check for correct number of command line arguments */ if(argc!=3) { printf("usage: %s <filename> <ch>\n", argv[0]); exit(1); /* exits function is part of stdlib.h */ /* open file for input */ fp = fopen(argv[1], "r"); if(fp == NULL) { printf("cannot open file \n"); exit(1); /* look for character */ while ( (ch=fgetc(fp) )!= EOF ) if (ch == *argv[2]) { printf("%c found\n", ch); break; fclose(fp); return(0); Exercise: make searchfile.c count the number of occurrences of a specified character