UNIT-5: C Programming - Structures, File Handling, and File Functions
1. Difference Between Structure and Union with Examples
Feature Structure Union
Memory
Separate memory for each member Shared memory for all members
Allocation
Size Sum of sizes of all members Size of the largest member
Changing one member doesn't affect
Modification Changing one member affects others
others
When only one value is needed at a
Use Case When all values need to be stored
time
Structure Example:
#include <stdio.h>
struct Student {
int id;
float marks;
};
int main() {
struct Student s1 = {101, 95.5};
printf("ID: %d, Marks: %.2f", s1.id, s1.marks);
return 0;
Union Example:
#include <stdio.h>
union Data {
int i;
float f;
};
int main() {
union Data d;
d.i = 10;
d.f = 20.5;
printf("i: %d, f: %.2f", d.i, d.f); // i is affected by f
return 0;
2. Explain About Nested Structures with a Sample C Program Example
• A structure inside another structure is called a nested structure.
Example:
#include <stdio.h>
struct Date {
int day, month, year;
};
struct Student {
int id;
char name[20];
struct Date dob; // Nested structure
};
int main() {
struct Student s1 = {101, "John", {15, 8, 2000}};
printf("ID: %d\nName: %s\nDOB: %d-%d-%d", s1.id, s1.name, s1.dob.day, s1.dob.month,
s1.dob.year);
return 0;
3. Write a C Program to Copy One File to Another
#include <stdio.h>
int main() {
FILE *source, *target;
char ch;
source = fopen("source.txt", "r");
target = fopen("target.txt", "w");
if(source == NULL || target == NULL) {
printf("Error opening file.");
return 1;
while((ch = fgetc(source)) != EOF) {
fputc(ch, target);
printf("File copied successfully.");
fclose(source);
fclose(target);
return 0;
4. Define a File? Write a C Program to Read Strings from a File
File:
• A file is a collection of data stored on a storage device. In C, file operations are handled using
functions from the stdio.h library.
Example Program to Read Strings from a File:
#include <stdio.h>
int main() {
FILE *fp;
char str[100];
fp = fopen("data.txt", "r");
if(fp == NULL) {
printf("File not found.");
return 1;
while(fgets(str, sizeof(str), fp)) {
printf("%s", str);
fclose(fp);
return 0;
5. Explain the Following Functions
(i) fopen():
• Used to open a file in different modes like read, write, append.
• Returns a pointer to the file object if successful, otherwise NULL.
Syntax:
FILE *fp = fopen("filename.txt", "mode");
Modes: "r", "w", "a", "r+", "w+", "a+"
Example:
FILE *fp = fopen("data.txt", "r");
(ii) fclose():
• Used to close an opened file.
• Frees the memory and resources allocated to that file.
Syntax:
fclose(fp);
(iii) ftell():
• Returns the current position (in bytes) of the file pointer.
Syntax:
long pos = ftell(fp);
Example:
FILE *fp = fopen("data.txt", "r");
printf("Position: %ld", ftell(fp));
(iv) fseek():
• Sets the position of the file pointer to a specific location.
Syntax:
fseek(fp, offset, origin);
• offset: number of bytes to move
• origin: reference point (SEEK_SET, SEEK_CUR, SEEK_END)
Example:
fseek(fp, 0, SEEK_END); // move to end of file