Structures, unions and file handling — Unit 5 Notes (Programming for Problem Solving in C)

BCS101 · Unit 5

Structures, unions and file handling notes — Unit 5

Free unit-wise study notes on structures, unions and file handling for Programming for Problem Solving in C, Semester 1 of B.Tech — Computer Science & Engineering — key concepts, examples, important questions and a revision checklist for semester exams.

Comprehensive hand-written notes covering composite data types (structures and unions) and file I/O operations. Essential for creating real-world applications that store data persistently.

Notebook — 10 pages

Page 1

Wink Notes

B.Tech CSE — 1st Semester

Programming for Problem Solving in C

Unit - 5

1. Introduction to Structures

Arrays can only hold elements of the same data type. What if we want to store information about a Student? We need a name (string), roll number (int), and marks (float). A Structure allows us to group different data types under a single name.

Defining a Structure
struct Student {
    char name[50];
    int roll;
    float marks;
}; // Semicolon is mandatory here!

int main() {
    struct Student s1 = {"Alice", 101, 89.5};
    printf("Name: %s", s1.name); // Using the dot operator
    return 0;
}

Next — Page 2 — Array of Structures

1 of 10

Page 2

Wink Notes

B.Tech CSE — 1st Semester

Programming for Problem Solving in C

Unit - 5

2. Array of Structures

If we need to store data for 60 students, creating 60 individual variables (s1, s2, ...) is impractical. Instead, we create an array where each element is a structure.

Array of Structures Example
struct Student class[60]; // Array of 60 students

// Accessing the 5th student's marks
class[4].marks = 95.0;

Next — Page 3 — Pointers to Structures

2 of 10

Page 3

Wink Notes

B.Tech CSE — 1st Semester

Programming for Problem Solving in C

Unit - 5

3. Pointers to Structures

We can have pointers pointing to structures. When accessing members through a pointer, we must use the arrow operator (`->`) instead of the dot operator (`.`).

Dot (.) vs Arrow (->)
struct Student s1 = {"Bob", 102, 75.5};
struct Student *ptr = &s1;

// These two statements do the exact same thing:
printf("%d", s1.roll);
printf("%d", ptr->roll);

Next — Page 4 — Unions

3 of 10

Page 4

Wink Notes

B.Tech CSE — 1st Semester

Programming for Problem Solving in C

Unit - 5

4. Unions

A Union is similar to a structure, but it uses the same memory location for all its members. This means a union can only hold ONE of its members' values at any given time.

Structure vs Union
FeatureStructureUnion
Keywordstructunion
MemoryAllocates separate memory for each memberAllocates shared memory (size of largest member)
AccessAll members can be accessed at any timeOnly one member can be accessed at a time
SizeSum of all member sizesSize of the largest member

Next — Page 5 — Typedef and Enum

4 of 10

Page 5

Wink Notes

B.Tech CSE — 1st Semester

Programming for Problem Solving in C

Unit - 5

5. Typedef and Enum

Typedef

`typedef` is used to give a new name (alias) to an existing data type. It is often used with structures to avoid typing 'struct' repeatedly.

typedef struct {
    int x, y;
} Point;

Point p1; // No need to write 'struct Point p1'

Enumerations (Enum)

`enum` is a user-defined type consisting of named integer constants.

enum Day { SUN, MON, TUE, WED, THU, FRI, SAT };
// SUN = 0, MON = 1, etc.

Next — Page 6 — Introduction to File Handling

5 of 10

Page 6

Wink Notes

B.Tech CSE — 1st Semester

Programming for Problem Solving in C

Unit - 5

6. Introduction to File Handling

Variables exist in RAM, which is volatile. When the program ends, the data is lost. File handling allows us to store data permanently on the hard drive.

  • FILE pointer: A special pointer used to communicate with the file.
  • Opening a file: fopen()
  • Closing a file: fclose() - Always close files to prevent data corruption!
Basic File Syntax
FILE *fptr;
fptr = fopen("data.txt", "r"); // Open for reading

if(fptr == NULL) {
    printf("Error opening file");
    return 1;
}

fclose(fptr);

Next — Page 7 — File Opening Modes

6 of 10

Page 7

Wink Notes

B.Tech CSE — 1st Semester

Programming for Problem Solving in C

Unit - 5

7. File Opening Modes

Common File Modes in C
ModeDescriptionIf file doesn't exist
"r"Read mode (Text)Returns NULL
"w"Write mode (Text). Overwrites existing.Creates new file
"a"Append mode (Text). Adds to end.Creates new file
"r+"Read & WriteReturns NULL
"w+"Read & Write. Overwrites.Creates new file
"rb"Read mode (Binary)Returns NULL

Next — Page 8 — Reading and Writing Text Files

7 of 10

Page 8

Wink Notes

B.Tech CSE — 1st Semester

Programming for Problem Solving in C

Unit - 5

8. Reading and Writing Text Files

Writing to a file (fprintf / fputs)

FILE *fptr = fopen("file.txt", "w");
fprintf(fptr, "Hello %s", "World"); // Formatted write
fputs("\nNew line", fptr); // String write

Reading from a file (fscanf / fgets)

char buffer[100];
FILE *fptr = fopen("file.txt", "r");

// Read one word at a time
fscanf(fptr, "%s", buffer);

// Read a whole line (safest way)
fgets(buffer, sizeof(buffer), fptr);

Next — Page 9 — Binary Files and End of File

8 of 10

Page 9

Wink Notes

B.Tech CSE — 1st Semester

Programming for Problem Solving in C

Unit - 5

9. Binary Files and EOF

Binary I/O (fread / fwrite)

Used primarily for reading/writing arrays or structures directly to disk in raw memory format.

struct Student s1 = {"Alice", 1, 90};
FILE *fp = fopen("data.bin", "wb");
fwrite(&s1, sizeof(struct Student), 1, fp);

End of File (EOF)

When reading a file, we often want to read until the end. `feof(file_pointer)` returns true if we hit the end. Alternatively, `fscanf` returns `EOF`.

Next — Page 10 — Quick Revision Checklist

9 of 10

Page 10

Wink Notes

B.Tech CSE — 1st Semester

Programming for Problem Solving in C

Unit - 5

10. Quick Revision Checklist

Unit 5 Mastery

  • Can you write the syntax for a `struct` and access it with both `.` and `->`?
  • Do you know the memory differences between a Structure and a Union?
  • Can you list the common file modes (`r`, `w`, `a`)?
  • Can you write a program to open a file, write a string to it, and close it safely?
  • Do you understand the difference between text files and binary files?

10 of 10

Continue in this subject