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

Assignment_9

The document contains a series of C programming assignments focused on file operations, including opening, creating, reading, writing, and deleting files. Each assignment is accompanied by source code that demonstrates the required functionality, such as counting lines in a file or appending text to an existing file. The document serves as a practical guide for learning file handling in C.
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)
16 views

Assignment_9

The document contains a series of C programming assignments focused on file operations, including opening, creating, reading, writing, and deleting files. Each assignment is accompanied by source code that demonstrates the required functionality, such as counting lines in a file or appending text to an existing file. The document serves as a practical guide for learning file handling in C.
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/ 34

Shubhashish Das Roy (23CS8013)

CSS 452 LAB


DATE:03/04/2025
ASSIGNMENT 9
Shubhashish Das Roy (23CS8013)

Q1. Write a C program that:

• Opens an existing file named sample.txt.

• If the file does not exist, create a new file named sample.txt.

• Print a message indicating whether the file was opened or created.

Source Code:

#include<stdio.h>

#include<unistd.h>

#include<fcntl.h>

int main(int argc, char *argv[])

int fd,openFlags;

mode_t filePerms;

fd=open(argv[1],O_RDONLY);

if(fd<0)

printf("File not found!");

openFlags = O_CREAT | O_WRONLY | O_TRUNC;

filePerms = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH |


S_IWOTH;

fd=open(argv[1],openFlags,filePerms);

printf("\nNew file created!");

close(fd);
Shubhashish Das Roy (23CS8013)

else

printf("File opened!");

printf("\n");

return 0;

Output :
Shubhashish Das Roy (23CS8013)

Q2. Write a C program to open an existing file named sample.txt and write the text “Welcome
to NIT Durgapur” into the file. Then, close the file.

Source Code:

#include<stdio.h>

#include<unistd.h>

#include<fcntl.h>

#include<string.h>

int main(int argc, char *argv[])

int fd,openFlags;

char buf[50];

mode_t filePerms;

openFlags = O_CREAT | O_WRONLY | O_TRUNC;

filePerms = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;

fd=open(argv[1],openFlags,filePerms);

printf("\nNew file created!");

sprintf(buf,"Welcome to NIT Durgapur!");

write(fd,buf,strlen(buf));

close(fd);

printf("\n");

return 0;
Shubhashish Das Roy (23CS8013)

Output :
Shubhashish Das Roy (23CS8013)

Q3.Write a C program to open an existing file named sample.txt, read its content, and display
it on the screen. Then, close the file.

Source Code:

#include<stdio.h>

#include<unistd.h>

#include<fcntl.h>

#include<string.h>

#define BUFSIZE 50

int main(int argc, char *argv[])

int fd,openFlags;

char buf[BUFSIZE];

mode_t filePerms;

ssize_t numread;

fd=open(argv[1],O_RDONLY);

if(fd<0) printf("\nFile not exist!");

else

printf("Content of file: \n");

while((numread=read(fd,buf,BUFSIZE))>0)

write(1,buf,numread);
Shubhashish Das Roy (23CS8013)

close(fd);

printf("\n");

return 0;

Output :

Q4. Write a C program that accepts a filename as a command-line argument, opens the file,
and prints its content.

Source Code:

#include<stdio.h>

#include<unistd.h>

#include<fcntl.h>

#include<string.h>

#define BUFSIZE 50

int main(int argc, char *argv[])

{
Shubhashish Das Roy (23CS8013)

int fd,openFlags;

char buf[BUFSIZE];

mode_t filePerms;

ssize_t numread;

fd=open(argv[1],O_RDONLY);

if(fd<0) printf("\nFile not exist!");

else

printf("Content of file: \n");

while((numread=read(fd,buf,BUFSIZE))>0)

write(1,buf,numread);

close(fd);

printf("\n");

return 0;

OUTPUT:
Shubhashish Das Roy (23CS8013)

Q5. Write a C program that accepts two filenames as command-line arguments (a source file
and a destination file), reads the content from the source file, and writes it to the destination
file.

Source Code:

#include<stdio.h>

#include<unistd.h>

#include<fcntl.h>

#include<string.h>

#define BUFSIZE 50

int main(int argc, char *argv[])

int fd,openFlags,fd2;

char buf[BUFSIZE];

mode_t filePerms;

ssize_t numread;

fd=open(argv[1],O_RDONLY);

if(fd<0) printf("\nFile not exist!");

else

openFlags = O_CREAT | O_WRONLY | O_TRUNC;

filePerms = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;

fd2=open(argv[2],openFlags,filePerms);

while((numread=read(fd,buf,BUFSIZE))>0)

{
Shubhashish Das Roy (23CS8013)

write(fd2,buf,numread);

close(fd);

close(fd2);

printf("\n");

return 0;

OUTPUT:

Q6. Write a C program that:

• Accepts a filename and a word as command-line arguments.

• Searches for the word in the file.

• Displays the line numbers where the word appears

Source Code:

#include <stdio.h>

#include <unistd.h>
Shubhashish Das Roy (23CS8013)

#include <fcntl.h>

#include <string.h>

#include <stdlib.h>

#define BUFSIZE 1024

int main(int argc, char *argv[]) {

if (argc != 3) {

printf("Usage: %s <filename> <word>\n", argv[0]);

return 1;

int fd = open(argv[1], O_RDONLY);

if (fd < 0) {

perror("Error opening file");

return 1;

char buf[BUFSIZE];

char *word = argv[2];

ssize_t numread;

int line_number = 1;

int found = 0;

char temp[BUFSIZE];

int temp_index = 0;
Shubhashish Das Roy (23CS8013)

while ((numread = read(fd, buf, BUFSIZE)) > 0)

for (int i = 0; i < numread; i++) {

if (buf[i] == '\n' || temp_index >= BUFSIZE - 1)

temp[temp_index] = '\0';

if (strstr(temp, word))

printf("Word found in line number: %d\n", line_number);

found = 1;

line_number++;

temp_index = 0;

else

temp[temp_index++] = buf[i];

if (!found) {

printf("Word not found in the file.\n");

close(fd);
Shubhashish Das Roy (23CS8013)

return 0;

OUTPUT:
Shubhashish Das Roy (23CS8013)

Q7. Write a C program that:

• Accepts two filenames as command-line arguments (source file and destination file).

• Reads the content from the source file.

• Writes the content in reverse order to the destination file

Source Code:

#include <stdio.h>

#include<fcntl.h>

#include<unistd.h>

#include<string.h>

#define BUFF_SIZE 100

int main(int argc,char* argv[])

int fd1,fd2,op;

char buf[100],arr[100];

mode_t fp;

ssize_t numread;

fd1=open(argv[1],O_RDONLY);

op=O_CREAT|O_WRONLY|O_TRUNC;

fp=S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH;

fd2=open(argv[2],op,fp);

if(fd1<0)

printf("File 1 not found...\n");


Shubhashish Das Roy (23CS8013)

exit(0);

else

printf("Writing to 2nd file...\n");

while ((numread = read(fd1, buf, BUFF_SIZE)) > 0)

int k=0;

for (int i = (int)numread-1; i>=0; i--)

arr[k++]=buf[i];

write(fd2,arr,strlen(arr));

close(fd1);

close(fd2);

printf("Done...\n");

return 0;

}
Shubhashish Das Roy (23CS8013)

OUTPUT:
Shubhashish Das Roy (23CS8013)

Q8. Write a C program that:

• Accepts a filename as a command-line argument.

• Checks whether the file exists.

• Prints "File exists" if it does, otherwise prints "File does not exist".

Source Code:

#include<stdio.h>

#include<unistd.h>

#include<fcntl.h>

int main(int argc, char *argv[])

int fd,openFlags;

mode_t filePerms;

fd=open(argv[1],O_RDONLY);

if(fd<0)

printf("File not exist!");

else

printf("File exist!");

close(fd);

printf("\n");
Shubhashish Das Roy (23CS8013)

return 0;

OUTPUT:
Shubhashish Das Roy (23CS8013)

Q9. Write a C program that:

• Accepts a filename as a command-line argument.

• Reads the file and counts the number of lines.

• Displays the total number of lines.

Source Code:

#include <stdio.h>

#include <unistd.h>

#include <fcntl.h>

#include <string.h>

#include <stdlib.h>

#define BUFSIZE 1024

int main(int argc, char *argv[]) {

if (argc != 2) {

printf("Usage: %s <filename> <word>\n", argv[0]);

return 1;

int fd = open(argv[1], O_RDONLY);

if (fd < 0) {

perror("Error opening file");

return 1;
Shubhashish Das Roy (23CS8013)

char buf[BUFSIZE];

ssize_t numread;

int line_number = 0;

char temp[BUFSIZE];

int temp_index = 0;

while ((numread = read(fd, buf, BUFSIZE)) > 0)

for (int i = 0; i < numread; i++) {

if (buf[i] == '\n' || temp_index >= BUFSIZE - 1)

temp[temp_index] = '\0';

line_number++;

temp_index = 0;

else

temp[temp_index++] = buf[i];

printf("\nNumber of lines: %d\n",line_number);

close(fd);

return 0;
Shubhashish Das Roy (23CS8013)

OUTPUT:

Q10. Write a C program that:

• Opens an existing file named log.txt.

• Appends the text "New log entry.\n" to the file.

• Closes the file.

Source Code:

#include<stdio.h>

#include<unistd.h>

#include<fcntl.h>

#include<string.h>

int main(int argc, char *argv[])

int fd,openFlags;

char buf[50];

mode_t filePerms;
Shubhashish Das Roy (23CS8013)

openFlags = O_CREAT | O_WRONLY;

filePerms = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;

fd=open(argv[1],openFlags,filePerms);

lseek(fd,0,SEEK_END);

sprintf(buf,"New log entry.\n");

write(fd,buf,strlen(buf));

close(fd);

printf("\n");

return 0;

OUTPUT:
Shubhashish Das Roy (23CS8013)

Q11. Write a C program that:

• Accepts a filename as a command-line argument.

• Deletes the specified file.

• Prints a success message.

Source Code:

#include<stdio.h>

#include<unistd.h>

#include<fcntl.h>

int main(int argc, char *argv[])

int fd,openFlags;

mode_t filePerms;

fd=open(argv[1],O_RDONLY);

if(fd<0)

printf("File not exist!");

else

close(fd);

printf("File Deleted!");

execl("/bin/rm", "rm", argv[1], (char *)0);

printf("\n");
Shubhashish Das Roy (23CS8013)

return 0;

OUTPUT:
Shubhashish Das Roy (23CS8013)

Miscellaneous Question:

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <sys/types.h>

#include <sys/stat.h>

#include <unistd.h>

#include <dirent.h>

#include <fcntl.h>

#include <time.h>

#include <errno.h>

#include <utime.h>

#define MAX_PATH 4096

#define BUFFER_SIZE 4096

void sync_files(const char *src_path, const char *dst_path);

void copy_file(const char *src, const char *dst);

void copy_dir(const char *src, const char *dst);

void delete_item(const char *path);

int compare_file_metadata(const char *src, const char *dst);

void update_metadata(const char *src, const char *dst);

int main(int argc, char *argv[])


Shubhashish Das Roy (23CS8013)

if (argc != 3)

fprintf(stderr, "Usage: %s <src_dir> <dst_dir>\n", argv[0]);

return 1;

sync_files(argv[1], argv[2]);

return 0;

void sync_files(const char *src_path, const char *dst_path)

DIR *src_dir = opendir(src_path);

DIR *dst_dir = opendir(dst_path);

if (src_dir == NULL && dst_dir == NULL) return;

if (src_dir == NULL)

delete_item(dst_path);

return;

if (dst_dir == NULL)

{
Shubhashish Das Roy (23CS8013)

copy_dir(src_path, dst_path);

return;

struct dirent *entry;

while ((entry = readdir(src_dir)) != NULL)

if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)

continue;

char src_item_path[MAX_PATH];

char dst_item_path[MAX_PATH];

snprintf(src_item_path, sizeof(src_item_path), "%s/%s", src_path, entry->d_name);

snprintf(dst_item_path, sizeof(dst_item_path), "%s/%s", dst_path, entry->d_name);

struct stat src_stat, dst_stat;

if (stat(src_item_path, &src_stat) == -1)

perror("stat src");

continue;

if (stat(dst_item_path, &dst_stat) == -1 && errno == ENOENT)


Shubhashish Das Roy (23CS8013)

if (S_ISREG(src_stat.st_mode))

copy_file(src_item_path, dst_item_path);

} else if (S_ISDIR(src_stat.st_mode))

copy_dir(src_item_path, dst_item_path);

continue;

if (S_ISREG(src_stat.st_mode))

if (S_ISREG(dst_stat.st_mode))

if (compare_file_metadata(src_item_path, dst_item_path) != 0)

copy_file(src_item_path, dst_item_path);

} else

delete_item(dst_item_path);

copy_file(src_item_path, dst_item_path);

} else if (S_ISDIR(src_stat.st_mode))

{
Shubhashish Das Roy (23CS8013)

if (S_ISDIR(dst_stat.st_mode))

sync_files(src_item_path, dst_item_path);

} else

delete_item(dst_item_path);

copy_dir(src_item_path, dst_item_path);

rewinddir(dst_dir);

while ((entry = readdir(dst_dir)) != NULL)

if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)

continue;

char src_item_path[MAX_PATH];

char dst_item_path[MAX_PATH];

snprintf(src_item_path, sizeof(src_item_path), "%s/%s", src_path, entry->d_name);

snprintf(dst_item_path, sizeof(dst_item_path), "%s/%s", dst_path, entry->d_name);


Shubhashish Das Roy (23CS8013)

struct stat src_stat;

if (stat(src_item_path, &src_stat) == -1 && errno == ENOENT)

delete_item(dst_item_path);

closedir(src_dir);

closedir(dst_dir);

update_metadata(src_path, dst_path);

void copy_file(const char *src, const char *dst)

int src_fd = open(src, O_RDONLY);

int dst_fd = open(dst, O_WRONLY | O_CREAT | O_TRUNC, 0644);

if (src_fd == -1 || dst_fd == -1)

perror("copy_file");

if(src_fd != -1) close(src_fd);

if(dst_fd != -1) close(dst_fd);

return;

}
Shubhashish Das Roy (23CS8013)

char buffer[BUFFER_SIZE];

ssize_t bytes_read;

while ((bytes_read = read(src_fd, buffer, BUFFER_SIZE)) > 0)

write(dst_fd, buffer, bytes_read);

close(src_fd);

close(dst_fd);

printf("[o]%s\n", dst);

void copy_dir(const char *src, const char *dst)

mkdir(dst, 0777);

printf("[+]%s\n", dst);

sync_files(src, dst);

void delete_item(const char *path)

struct stat statbuf;

if (stat(path, &statbuf) == -1) return;

if (S_ISREG(statbuf.st_mode))

{
Shubhashish Das Roy (23CS8013)

unlink(path);

printf("[-]%s\n", path);

} else if (S_ISDIR(statbuf.st_mode))

DIR *dir = opendir(path);

if (dir == NULL) return;

struct dirent *entry;

while ((entry = readdir(dir)) != NULL)

if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)

continue;

char item_path[MAX_PATH];

snprintf(item_path, sizeof(item_path), "%s/%s", path, entry->d_name);

delete_item(item_path);

closedir(dir);

rmdir(path);

printf("[-]%s\n", path);

}
Shubhashish Das Roy (23CS8013)

int compare_file_metadata(const char *src, const char *dst)

struct stat src_stat, dst_stat;

if (stat(src, &src_stat) == -1 || stat(dst, &dst_stat) == -1) return -1;

if (src_stat.st_size != dst_stat.st_size || src_stat.st_mtime != dst_stat.st_mtime)

return -1;

return 0;

void update_metadata(const char *src, const char *dst)

struct stat src_stat, dst_stat;

if (stat(src, &src_stat) == -1 || stat(dst, &dst_stat) == -1) return;

struct utimbuf times;

times.actime = src_stat.st_atime;

times.modtime = src_stat.st_mtime;

if (src_stat.st_mtime != dst_stat.st_mtime)

utime(dst, &times);

printf("[t]%s\n", dst);

}
Shubhashish Das Roy (23CS8013)

if (src_stat.st_mode != dst_stat.st_mode)

chmod(dst, src_stat.st_mode);

printf("[p]%s\n", dst);

OUTPUT:

You might also like