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

CTA-Report Format

This report details coursework for the Database Management System course at SDM College of Engineering and Technology, submitted by Ms. Ananya U Gaonkar. It includes C programs demonstrating file operations in UNIX, indexing and associated operations, and a Java program for accessing a formatted file. The report is structured with a table of contents and code implementations for each assignment.

Uploaded by

ananyagaonkar344
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

CTA-Report Format

This report details coursework for the Database Management System course at SDM College of Engineering and Technology, submitted by Ms. Ananya U Gaonkar. It includes C programs demonstrating file operations in UNIX, indexing and associated operations, and a Java program for accessing a formatted file. The report is structured with a table of contents and code implementations for each assignment.

Uploaded by

ananyagaonkar344
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Page |1

SDM COLLEGE OF ENGINEERING AND


TECHNOLOGY
Dhavalagiri, Dharwad-580002, Karnataka State, India.
Email: [email protected]

DEPARTMENT OF COMPUTER SCIENCE AND


ENGINEERING

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

[ Academic Year- 2023-24]

Date of Submission: 10-10-2024

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

supported by UNIX OS and C libraries for file operations.

A2 .Write a C program to demonstrate indexing and associated operations. 5

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];

// 1. Creating and opening a file using 'open' system call


file_desc = open("testfile.txt", O_WRONLY | O_CREAT | O_TRUNC, 0644);
if (file_desc < 0) {
perror("Error opening file");
exit(EXIT_FAILURE);}
printf("File created and opened successfully.\n");
// 2. Writing to the file using 'write' system call
const char *data = "Hello, this is a test for file operations in Unix using system calls.\n";
bytes_written = write(file_desc, data, strlen(data));
if (bytes_written < 0) {
perror("Error writing to file");
close(file_desc);
exit(EXIT_FAILURE);

Page 3 of 11
Page |4

}
printf("%ld bytes written to file.\n", bytes_written);

// 3. Closing the file using 'close' system call


if (close(file_desc) < 0) {
perror("Error closing file");
exit(EXIT_FAILURE);
}
printf("File closed successfully.\n");

// 4. Re-opening the file in read mode


file_desc = open("testfile.txt", O_RDONLY);
if (file_desc < 0) {
perror("Error reopening file");
exit(EXIT_FAILURE);
}
printf("File reopened in read mode.\n");

// 5. Reading the contents of the file using 'read' system call


bytes_read = read(file_desc, buffer, sizeof(buffer) - 1);
if (bytes_read < 0) {
perror("Error reading file");
close(file_desc);
exit(EXIT_FAILURE);
}
buffer[bytes_read] = '\0'; // Null terminate the string
printf("File content:\n%s\n", buffer);

Page 4 of 11
Page |5

// 6. Closing the file again


if (close(file_desc) < 0) {
perror("Error closing file");
exit(EXIT_FAILURE);
}
printf("File closed after reading.\n");

// 7. Deleting the file using 'unlink' system call


if (unlink("testfile.txt") < 0) {
perror("Error deleting file");
exit(EXIT_FAILURE);
}
printf("File deleted successfully.\n");
return 0;
}

A2 .Write a C program to demonstrate indexing and associated operations.


#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 100 // Maximum size of the array
// Function to display the elements of the array
void display(int arr[], int size) {
printf("Array elements: ");
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");

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

printf("Enter the element to insert: ");


scanf("%d", &element);
printf("Enter the index at which to insert: ");
scanf("%d", &index);
insert(arr, &size, element, index);
break;
case 2: // Delete element
printf("Enter the index at which to delete: ");
scanf("%d", &index);
delete(arr, &size, index);
break;
case 3: // Search element
printf("Enter the element to search for: ");
scanf("%d", &element);
index = search(arr, size, element);
if (index != -1) {
printf("Element %d found at index %d.\n", element, index);
} else {
printf("Element not found.\n");
}
break;
case 4: // Display array
display(arr, size);
break;
case 5: // Exit
exit(0);
default:printf("Invalid choice. Please try again.\n");

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;

// Class representing a Record with ID, Name, and Age


class Record {
private int id;
private String name;
private int age;

public Record(int id, String name, int age) {


this.id = id;
this.name = name;
this.age = age;
}

// 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;
}

// Main method to demonstrate file access and record parsing


public static void main(String[] args) {
String filePath = "data.txt"; // Path to the file
List<Record> records = readRecordsFromFile(filePath);

// Display the parsed records


System.out.println("Parsed Records:");
for (Record record : records) {
System.out.println(record);
}
}
}

Page 11 of 11

You might also like