Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

Lab 7 (14-11-2023)

File Manipulation System Calls

Student Name: Bilal Hasan Khan


Roll No_BSCS-2021-37

write() System Call:

Task 1:
Write a C++ program that takes user input and uses the write() system call to write the input to a
file named "output.txt". Make sure to handle errors appropriately.

Note: For checking error


if (filename == -1) {
cerr("Error creating the file");
return 1; // Exit with an error code
} else {
cout<<("File created successfully\n");

#include <iostream>
#include <unistd.h>
#include<fcntl.h>
using namespace std;
int main(){
int input[32];
ssize_t user_inp;
int file;
user_inp=read(0,input,32);
file=open("Newtext.txt",O_RDWR|O_CREAT,0777 );
if (file==-1){
cout<<"Error while creating file"<<endl;
return 1;
}
else{
cout<<"File created successfully"<<endl;
}
write(file,input,user_inp);
close(file);

}
Task 2:
Extend the program from Task 1 to prompt the user for the name of the file to write to and the
content to write. Use the write() system call to append the content to the specified file.

#include <iostream>
#include <fstream>
#include <cstring>
#include <unistd.h>
#include <fcntl.h>
using namespace std;
int main(){
cout<<"enter filename: ";
string file_name;
getline(cin,file_name);
int fd = open (file_name.c_str(), O_CREAT | O_RDWR | O_APPEND, 0777);
if (fd == -1) {
cout<<"Error creating the file"<<endl;
return 1; // Exit with an error code
}
cout<<"enter text to write in file: "<<endl;
string text;
getline(cin, text);
cout<<"text length : " <<text.length()<<endl;
ssize_t lengthh = write(fd , text.c_str(), text.length());
if (lengthh == -1) {
cout<<"Error in writing into the file"<<endl;
close(fd);
return 1; // Exit with an error code
}
close(fd);
cout<<"data written";
return 0;
}

read() System Call:

Task 3:
Create a C++ program that uses the open() and read() system calls to open a file named
"input.txt" and display its content on the console.
#include <iostream>
#include <fstream>
#include <cstring>
#include <unistd.h>
#include <fcntl.h>
using namespace std;
int main(){
cout<<"enter filename: ";
string file_name;
getline(cin,file_name);
int fd = open (file_name.c_str(),O_RDONLY);
if (fd == -1) {
cout<<"Error opening the file"<<endl;
return 1; // Exit with an error code
}
ssize_t bytes_read;
const int buffer_size=4096;
char buffer[buffer_size];
bytes_read = read(fd, buffer, sizeof(buffer));
write(1, buffer, bytes_read);
close(fd);
cout<<endl;
return 0;
}

Task 4:
Modify the program from Task 3 to prompt the user for the name of the file to read from. Use the
open() and read() system calls to display the content of the specified file.
#include<iostream>
#include<unistd.h>
#include<fcntl.h>
using namespace std;
int main(){
const int length_of_filename=32;
char filename[length_of_filename];
int text_of_file[32];
int file;
int read_flag;
cout<<"Enter name of file"<<endl;
cin.getline(filename,length_of_filename);
file=open(filename,O_RDONLY, 0777);
if (file !=1){
cout<<"File was opened successfully"<<endl;

read_flag=read(file, text_of_file, sizeof(text_of_file));


if (read_flag==-1){
cout<<"Failed to read"<<endl;
}
else{
cout<<"File was read successfully"<<endl;
write(0,text_of_file,sizeof(text_of_file));
}

close(file);
}
else{
perror("Failed to open file");
}
}

open() System Call:

Task 5:
Write a C++ program that uses the open() system call to create a new file named "example.txt".
Check if the file is created successfully and handle errors appropriately.
#include<iostream>
#include<unistd.h>
#include<fcntl.h>
using namespace std;
int main(){
int file_to_create;

file_to_create=open("example.txt",O_RDWR|O_CREAT,0777);
if (file_to_create != 1){
cout<<"File was created successfully"<<endl;
}
else{
cout<<"Failed to create file"<<endl; }

}
Task 6:
Extend the program from Task 5 to use the open() system call with different flags mentioned in
lab manual to open the file in write mode and append mode. Write some content to the file
using the write() system call.
#include <iostream>
#include <fstream>
#include <cstring>
#include <unistd.h>
#include <fcntl.h>
using namespace std;
int main(){
cout<<"enter filename: ";
string file_name;
getline(cin,file_name);
int fd = open (file_name.c_str(), O_WRONLY | O_APPEND, 0777);
if (fd == -1) {
cout<<"Error creating the file"<<endl;
return 1; // Exit with an error code
}
cout<<"enter text to write in file: "<<endl;
string text;
getline(cin, text);
ssize_t lengthh = write(fd , text.c_str(), text.length());
if (lengthh == -1) {
cout<<"Error in writing into the file"<<endl;
close(fd);
return 1; // Exit with an error code
}
else{
cout<<"bytes written are : " <<lengthh<<endl;
}
close(fd);
cout<<"data has been written";
return 0;
}
close() System Call:
Task 7:
Create a C++ program that opens a file, writes some content using the write() system call, and
then closes the file using the close() system call. Ensure proper error handling.
#include<iostream>
#include<unistd.h>
#include<fcntl.h>
using namespace std;
int main(){
int file_to_create;
ssize_t amount_of_text[32];
int close_flag;
file_to_create=open("example.txt",O_WRONLY|O_CREAT|O_APPEND,0777);
if (file_to_create != 1){
cout<<"File was created successfully"<<endl;
cout<<"--------------------"<<endl;
cout<<"Enter the text you want to append to example.txt"<<endl;
read(0,amount_of_text,sizeof(amount_of_text));
write(file_to_create,amount_of_text,sizeof(amount_of_text));
close_flag=close(file_to_create);
if (close_flag==0){
cout<<"File closed succesfully"<<endl;
}
else{
cout<<"Failed to close file"<<endl;
}

}
else{
cout<<"Failed to create file"<<endl; }

}
Task 8:
Modify the program from Task 7 to use a loop to repeatedly open, write, and close the file.
Experiment with different file opening modes mentioned in lab manual and observe the
behaviour. Add output of the task and code screenshot of each opening mode.
#include <iostream>
#include <unistd.h>
#include <fcntl.h>
#include <cstring>
using namespace std;

int main() {
const char* filename = "example.txt";
ssize_t amount_of_text[32];
int close_flag;
const int modes[] = {O_WRONLY | O_CREAT | O_TRUNC, O_WRONLY | O_CREAT |
O_APPEND, O_RDONLY};

for (int i = 0; i < sizeof(modes) / sizeof(modes[0]); ++i) {


int file_to_create = open(filename, modes[i], 0777);

if (file_to_create != -1) {
cout << "File opened successfully with mode " << modes[i] << endl;
cout << "--------------------" << endl;
cout << "Enter the text you want to write to " << filename << endl;
read(0, amount_of_text, sizeof(amount_of_text));
write(file_to_create, amount_of_text, sizeof(amount_of_text));

close_flag = close(file_to_create);
if (close_flag == 0) {
cout << "File closed successfully" << endl;
} else {
cerr << "Failed to close file" << endl;
}
} else {
cerr << "Failed to open file with mode " << modes[i] << endl;
}
cout << "==========================================" << endl;
}

return 0;
}

You might also like