Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
57 views3 pages

Exp# 2a Open System Call Aim: O - TRUNC Options

The document describes an experiment to create a file and write input from the user to that file using open and write system calls. The algorithm opens a file with O_CREAT and O_TRUNC flags, reads input from the user until Ctrl-D, writes the input to the file, and closes the file. The program code implements this by opening a file from the command line, reading from standard input, writing to the file descriptor, and closing the file. Running the program demonstrates creating and writing to a file.

Uploaded by

Abuzar Sh
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)
57 views3 pages

Exp# 2a Open System Call Aim: O - TRUNC Options

The document describes an experiment to create a file and write input from the user to that file using open and write system calls. The algorithm opens a file with O_CREAT and O_TRUNC flags, reads input from the user until Ctrl-D, writes the input to the file, and closes the file. The program code implements this by opening a file from the command line, reading from standard input, writing to the file descriptor, and closing the file. Running the program demonstrates creating and writing to a file.

Uploaded by

Abuzar Sh
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/ 3

CS2257 Operating System Lab

Exp# 2a open system call

Aim
To create a file and to write contents.

Algorithm

1. Declare a character buffer buf to store 100 bytes.


2. Get the new filename as command line argument.
3. Create a file with the given name using open system call with O_CREAT and
O_TRUNC options.
4. Check the file descriptor.
a) If file creation is unsuccessful, then stop.
5. Get input from the console until user types Ctrl+D
a) Read 100 bytes (max.) from console and store onto buf using read system call
b) Write length of buf onto file using write system call.
6. Close the file.
7. Stop

Result
Thus a file has been created with input from the user. The process can be verified by
using cat command.

http://cseannauniv.blogspot.com Vijai Anand


CS2257 Operating System Lab

Program

/* File creation - fcreate.c */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>

main(int argc, char *argv[])


{
int fd, n, len;
char buf[100];

if (argc != 2)
{
printf("Usage: ./a.out <filename>\n");
exit(-1);
}

fd = open(argv[1], O_WRONLY|O_CREAT|O_TRUNC, 0644);


if(fd < 0)
{
printf("File creation problem\n");
exit(-1);
}

printf("Press Ctrl+D at end in a new line:\n");


while((n = read(0, buf, sizeof(buf))) > 0)
{
len = strlen(buf);
write(fd, buf, len);
}
close(fd);
}

http://cseannauniv.blogspot.com Vijai Anand


CS2257 Operating System Lab

Output

$ gcc fcreate.c

$ ./a.out hello
File I/O
Open system call is used to either open or create a file.
creat system call is used to create a file. It is seldom used.
^D

http://cseannauniv.blogspot.com Vijai Anand

You might also like