CTA-Report Format
CTA-Report Format
A Report
on
Course work / Assignment for CTA
COURSE CODE: 2UCSC501 COURSE TITLE: Database Management System
SEMESTER: 5 DIVISION: A COURSE TEACHER: U.P.Kulkarni
Submitted By
Name: Ms.Ananya U Gaonkar USN: 2SD22CS012
Page 1 of 11
Page |2
Table of Contents:
A1: Write a C program to study all file operations related SYSTEM CALLS 3
A3: Write a java program to access a given access file with known format . 9
Page 2 of 11
Page |3
Minor Work
A1: Write a C program to study all file operations related SYSTEM CALLS
supported by UNIX OS and C libraries for file operations.
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h> // for open, O_RDONLY, O_WRONLY, O_CREAT, O_APPEND
#include <unistd.h> // for close, read, write, unlink
#include <string.h> // for strlen
#include <errno.h> // for error handling
int main() {
int file_desc;
ssize_t bytes_written, bytes_read;
char buffer[100];
Page 3 of 11
Page |4
}
printf("%ld bytes written to file.\n", bytes_written);
Page 4 of 11
Page |5
Page 5 of 11
Page |6
}
// Function to insert an element at a given index
void insert(int arr[], int *size, int element, int index) {
if (*size >= MAX_SIZE) {
printf("Array is full, cannot insert element.\n");
return;
}
if (index < 0 || index > *size) {
printf("Invalid index for insertion.\n");
return;
}
for (int i = *size; i > index; i--) {
arr[i] = arr[i - 1]; // Shift elements to the right
}
arr[index] = element;
(*size)++;
printf("Element %d inserted at index %d.\n", element, index);
}
// Function to delete an element at a given index
void delete(int arr[], int *size, int index) {
if (index < 0 || index >= *size) {
printf("Invalid index for deletion.\n");
return;
}
printf("Element %d deleted from index %d.\n", arr[index], index);
for (int i = index; i < *size - 1; i++) {
arr[i] = arr[i + 1]; // Shift elements to the left
Page 6 of 11
Page |7
}
(*size)--;
}
// Function to search for an element and return its index
int search(int arr[], int size, int element) {
for (int i = 0; i < size; i++) {
if (arr[i] == element) {
return i; // Return index if element is found
}
}
return -1; // Return -1 if element is not found
}
int main() {
int arr[MAX_SIZE]; // Array to hold elements
int size = 0; // Current size of the array
int choice, element, index;
while (1) {
printf("\nMenu:\n");
printf("1. Insert element\n");
printf("2. Delete element\n");
printf("3. Search element\n");
printf("4. Display array\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1: // Insert element
Page 7 of 11
Page |8
Page 8 of 11
Page |9
}
}
return 0;
}
A3: Write a java program to access a given access file with known format
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
// Getters
public int getId() {
return id;
Page 9 of 11
P a g e | 10
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
@Override
public String toString() {
return "ID: " + id + ", Name: " + name + ", Age: " + age;
}
}
public class FileAccessDemo {
// Method to read a file and parse its contents into a list of Records
public static List<Record> readRecordsFromFile(String filePath) {
List<Record> records = new ArrayList<>();
try (BufferedReader br = new BufferedReader(new FileReader(filePath))) {
String line;
while ((line = br.readLine()) != null) {
String[] parts = line.split(", ");
if (parts.length == 3) {
int id = Integer.parseInt(parts[0]);
String name = parts[1];
int age = Integer.parseInt(parts[2]);
records.add(new Record(id, name, age));
} else {
System.out.println("Skipping malformed line: " + line);
Page 10 of 11
P a g e | 11
}
}
} catch (IOException e) {
System.out.println("Error reading file: " + e.getMessage());
}
return records;
}
Page 11 of 11