Files
1. Run the above program when file “read.txt” doesn’t exist.
Ans: Objective of this exercise is for you to observe that “r” mode does not create file and program prints an error at run time. You should be able to see the error message you printed in the program.
As second stage of this exercise, you should use perror in each error condition and rerun this exercise. This will help you see system error message for the same error.
2. Run the above program when file “write.txt” already exists and has some content.
Ans: Objective of this exercise is for you to observe that “w” mode truncates an existing file. Observe the contents of file before and after running the program.
3. Rewrite the above program with mode being passed to fopen as “a” at line 08. Execute the above program multiple times and observe the contents of file “write.txt”.
Ans: Objective of this exercise is for you to observe that “a” appends to an existing file. Observe the contents of file before and after running the program.
4. Which of the following statements are true.
1. fgets with size parameter set to 1 is equivalent to fgetc.
Ans: False. fgets reads in a string and places a terminating ‘\0’ for the string. With size set to 1, you will read in no characters.
2. Every program has three FILE * open by default – stdin, stdout, and stderr.
Ans: True.
3. perror writes errors to stdout.
Ans: False. perror writes to stderr.
4. Value of errno should be checked immediately after a library call fails.
Ans: True.
5. Format strings for printf and fprintf are identical.
Ans: True.
5. Write a program that reads int value from a file, one value per line and prints the sum
#include <stdio.h> int main (int argc, char *argv[]) { int num = 0, sum = 0; FILE * infile = NULL; infile = fopen("input.txt", "r"); if(NULL == infile) { perror("Could not open input file."); return -1; } while(fscanf(infile, "%d", &num) != EOF) { sum += num; } printf("Sum is %d\n", sum); fclose(infile); return 0; }
6. Write a program that asks user to input a word and then finds number of occurrences (case insensitive) of that word in a file.
#include <stdio.h> #include <string.h> #include <ctype.h> int main (int argc, char *argv[]) { char word[32] = { 0 }, line[256] = { 0 }; char * offset = NULL; FILE * infile = NULL; int count = 0; infile = fopen("input.txt", "r"); if(NULL == infile) { perror("Could not open input file."); return -1; } printf("Enter the word to search: "); fgets( word, 32, stdin); /* while we have taken sufficiently large buffers to store a word and a line, * this implementation will fall short if input size is larger, e.g. the word * we are searching could straddle 256 char boundary */ /* C does not have standard case insensitive string comparison. * However, there are many ways to solve this problem. Few are listed below * 1. We can use non standard extensions such as strcasestr * 2. We can convert both word and line to same case and use strstr * 3. We can convert both word and line to same case and use strtok * 4. We can convert both word and line to same case and do brute force comparison, char by char */ while(fgets(line, 256, infile) != NULL) { offset = line; for(int i=0; word[i]; i++) { word[i] = toupper(word[i]); if(word[i] == '\n') { word[i] = '\0'; } } for(int i=0; line[i]; i++) { line[i] = toupper(line[i]); } /* Note that assignment in while control expression is intentional */ while(offset = strstr(offset, word)) { count++; /* It is important to advance offset. E.g. if word was "ss" * and line contained "sss". If we simply increment offset we * will end up counting two occurrences of "ss" */ offset += strlen(word); if(offset-line > strlen(line)) { break; } } } printf("Word %s was found %d times\n", word, count); fclose(infile); return 0; }