1.
File handling functions: fopen(), fclose(), fprintf(), fscanf(), fseek(), ftell(),
rewind()
Theory: File handling lets a C program store and retrieve data on disk. Files are accessed through FILE pointers
and operated using standard functions. Each function below performs a specific file operation.
Simple Algorithm:
1. Decide file name and mode (read/write/append/binary).
2. Call fopen() to open file and get FILE * pointer.
3. Use fprintf/fscanf or fgetc/fputc/fread/fwrite to write/read.
4. Use fseek/ftell/rewind to move the file pointer if needed.
5. Check errors with ferror() or if fopen returns NULL.
6. Close file with fclose() when done.
Simple C Code:
#include <stdio.h>
int main(void) {
FILE *fp = fopen("[Link]", "w"); /* open for writing */
if (fp == NULL) return 1; /* error check */
fprintf(fp, "Hello %d\n", 2025); /* write text */
fclose(fp); /* close file */
return 0;
}
2. C program to copy contents of one file to another
Theory: Copying a file involves reading from a source and writing to a destination. Use text-mode functions for
plain text and binary-mode for non-text files.
Simple Algorithm:
1. Open source file in read mode and destination in write mode.
2. Read characters or blocks from source until EOF.
3. Write each character or block to destination.
4. Close both files.
Simple C Code:
#include <stdio.h>
int main(void) {
FILE *src = fopen("[Link]","r");
FILE *dst = fopen("[Link]","w");
int ch;
if (!src || !dst) return 1;
while ((ch = fgetc(src)) != EOF) fputc(ch, dst);
fclose(src); fclose(dst);
return 0;
}
3. Sequential vs Random file access (with examples)
Theory: Sequential access reads data in order. Random (direct) access jumps to any location using fseek.
Sequential is simple; random is used for fixed-size records and faster retrieval.
Simple Algorithm:
1. For sequential read: open file and read from start to end using fgetc/fgets or fread in loop.
2. For random access: use fseek to move to byte offset and then fread/fwrite.
3. Use ftell to know current position and rewind to go to start.
Simple C Code:
/* Sequential read */
#include <stdio.h>
int main(void) {
FILE *f = fopen("[Link]","r"); int c;
if (!f) return 1;
while ((c=fgetc(f))!=EOF) putchar(c);
fclose(f);
return 0;
}
/* Random access (read 3rd record of fixed size) */
#include <stdio.h>
struct rec { int id; char name[20]; };
int main2(void) {
FILE *f = fopen("[Link]","rb");
struct rec r;
if (!f) return 1;
fseek(f, 2 * sizeof(r), SEEK_SET); /* third record (0-based) */
fread(&r, sizeof(r), 1, f);
fclose(f);
return 0;
}
4. Explain fread() and fwrite() with suitable examples
Theory: fread and fwrite are used to read/write binary blocks. They are efficient for structures and large data.
Simple Algorithm:
1. Open file in 'rb' or 'wb' mode.
2. Prepare buffer or structure to read/write.
3. Call fread/fwrite with pointer, element size, and count.
4. Check return value to ensure correct number of items processed.
5. Close the file.
Simple C Code:
#include <stdio.h>
struct student { int roll; char name[20]; };
int main(void) {
struct student s = {1, "Kannan"}, r;
FILE *f = fopen("[Link]","wb");
if (!f) return 1;
fwrite(&s, sizeof(s), 1, f);
fclose(f);
f = fopen("[Link]","rb");
if (!f) return 1;
fread(&r, sizeof(r), 1, f);
fclose(f);
/* r now has the same data */
return 0;
}
5. Program to count vowels, consonants, digits, and spaces in a file
Theory: Counting character types reads each character and classifies it using simple tests. Use ctype.h helpers.
Simple Algorithm:
1. Open file in text mode for reading.
2. Initialize counters for vowels, consonants, digits, spaces.
3. Loop reading each character until EOF and update counters.
4. Close file and print counts.
Simple C Code:
#include <stdio.h>
#include <ctype.h>
int main(void) {
FILE *f = fopen("[Link]","r");
int ch, v=0,c=0,d=0,s=0;
if (!f) return 1;
while ((ch=fgetc(f))!=EOF) {
if (isdigit(ch)) d++;
else if (isspace(ch)) s++;
else if (isalpha(ch)) {
char lc = tolower(ch);
if (lc=='a'||lc=='e'||lc=='i'||lc=='o'||lc=='u') v++;
else c++;
}
}
fclose(f);
printf("Vowels=%d Consonants=%d Digits=%d Spaces=%d\n", v,c,d,s);
return 0;
}
6. Explain command line arguments with examples
Theory: Command-line arguments let user pass inputs when running the program. argc is count; argv holds
strings.
Simple Algorithm:
1. Define main with argc and argv parameters.
2. Check argc to ensure required arguments are provided.
3. Convert strings to numbers if needed (atoi, atof).
4. Use the values and exit.
Simple C Code:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
if (argc < 3) { printf("Usage: %s a b\n", argv[0]); return 1; }
int a = atoi(argv[1]); int b = atoi(argv[2]);
printf("Sum = %d\n", a + b);
return 0;
}
7. Explain binary vs text files in detail
Theory: Text files store readable characters with translations for newline; binary files store raw byte data. Choose
binary for compact storage and exact data, text for portability and human readability.
Simple Algorithm:
1. Decide file type based on data (text vs structured binary).
2. Use text functions (fprintf/fscanf) for text files.
3. Use fread/fwrite for binary files ensuring consistent struct sizes.
4. Handle newline differences across platforms for portability.
Simple C Code:
/* Text write */
#include <stdio.h>
int main(void) {
FILE *f = fopen("[Link]","w"); if (!f) return 1;
fprintf(f, "Number=%d\n", 10); fclose(f); return 0;
}
/* Binary write */
#include <stdio.h>
int main2(void) {
int x = 10; FILE *f = fopen("[Link]","wb");
if (!f) return 1; fwrite(&x, sizeof(x), 1, f); fclose(f); return 0;
}
8. Describe file pointer operations and error handling in file processing
Theory: File pointer operations let you inspect and move position within a file. Error handling ensures safe
operations.
Simple Algorithm:
1. Open file and check for NULL.
2. Use ftell to get current position; fseek to move; rewind to go to start.
3. After IO operations check ferror or feof.
4. Use perror for system error messages and always close files.
Simple C Code:
#include <stdio.h>
int main(void) {
FILE *f = fopen("[Link]","rb");
if (!f) { perror("open"); return 1; }
fseek(f, 0, SEEK_END);
long size = ftell(f); /* file size */
rewind(f); /* go back to start */
if (ferror(f)) perror("file error");
fclose(f);
return 0;
}