0% found this document useful (0 votes)
4 views

Unit-4

Best for all

Uploaded by

mdsaoudanzar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Unit-4

Best for all

Uploaded by

mdsaoudanzar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Unit-IV: User-Defined Data Types and Files

1. User-Defined Data Types (UDTs)


UDTs allow programmers to create their own data types to organize and handle complex data. The two common
types of UDTs are Structures and Enumerations.

a. Structures
A structure is a collection of variables (of different types) grouped together under one name. It is used to model
real-world entities.

Syntax:

struct StructureName {
dataType1 variable1;
dataType2 variable2;
// Add more variables if needed
};

Example:

struct Student {
int rollNo;
char name[50];
float marks;
};

Usage: You can create variables of the structure type and access its members using the dot (.) operator.

struct Student s1;


s1.rollNo = 101;
strcpy(s1.name, "John");
s1.marks = 87.5;
printf("Roll No: %d, Name: %s, Marks: %.2f", s1.rollNo, s1.name, s1.marks);

b. Enumerations
An enumeration (enum) is a user-defined type consisting of named integer constants. It is useful when you have a
set of predefined values.

Syntax:

enum EnumName { value1, value2, value3 };

Example:

enum Day { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday };

Usage: Enumerators are automatically assigned integer values starting from 0.

enum Day today = Monday;


printf("Day: %d", today); // Output: Day: 1

2. File Handling
File handling allows programs to read from and write data to files on the storage device.

File Operations
1. File Creation
2. File Reading
3. File Writing
4. File Appending
5. Closing a File
Types of Files
1. Text Files: Store data in human-readable format.
2. Binary Files: Store data in binary (machine-readable) format.

Steps for File Handling in C


1. Include the file handling library: #include <stdio.h>
2. Declare a file pointer: FILE *filePointer;

3. Open a file using fopen(). Modes: "r" (read), "w" (write), "a" (append), "rb" (read binary), "wb" (write binary),
etc.

4. Perform file operations.


5. Close the file using fclose().

File Functions
1. fopen(): Opens a file.
2. fclose(): Closes an opened file.
3. fprintf(): Writes formatted data to a file.

FILE *filePointer = fopen("filename.txt", "mode");


fclose(filePointer);

Example of Writing Data to a File:

FILE *file = fopen("example.txt", "w");


if (file != NULL) {
fprintf(file, "Name: John\nAge: 20");
fclose(file);
} else {
printf("Error opening file!");
}

Example of Reading Data from a File:

FILE *file = fopen("example.txt", "r");


char line[100];
if (file != NULL) {
while (fgets(line, sizeof(line), file)) {
printf("%s", line);
}
fclose(file);
} else {
printf("Error opening file!");
}

You might also like