Question

In: Computer Science

In C, write a program that makes use of the flags O_CREAT and O_EXCL, and observe...

In C, write a program that makes use of the flags O_CREAT and O_EXCL, and observe what does it do when you try to open an existing file

Solutions

Expert Solution

Solution:

The observations are clearly given in the program in the form of comments

------------------------------------------------------------------------------------------------------------------------------------------------------------------

O_CREAT:

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <errno.h>
#include <sys/stat.h>
#include <string.h>
#include <fcntl.h>

int main(int arg, char *argv[])
{
int fd;
err = 0;
  
if(arg!=2)
{
printf("Usage:");
return 1;
}

/* The O_CREAT flag enables the open() System call to create a file
If the file does not exist in the path which is given */

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

if(fd==-1)
{
printf("\n Open() System call failed [%s]",strerror(err));
return 1;
}
  
else
{
printf("\n Open() Succeded");
/* Here we see that the open() system call is successfully called,
One can easily perform read operations on the file
The file needs to be closed after the operation using close(). */
}

return 0;
}

----------------------------------------------------------------------------------------------------------------------------------------------------------------

O_EXCL

#include <stdio.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <errno.h>
#include <sys/types.h>
#include <string.h>

int main(int arg, char *argv[])
{
errno=0;
int fd;

if(arg!=2)
{
printf("Usage:");
return 1;
}

/* By using the O_CREAT flag, the open() system call is used to create a file
If the file does not exist in the path which is given
then the O_EXCL flag makes sure that an error is Successfully thrown in O_CREAT*/

  
fd = open(argv[1],O_RDONLY|O_CREAT|O_EXCL,S_IRWXU);

if(fd==-1)
{
printf("\n open() System call failed [%s] " ,strerror(errno));
return 1;
}
  
else
{
printf("\n Open() System call passed\n");
/* Here we see that the open() system call is Successful.
One can easily start read operations now
After all the work is done, please close the file which can be easily done using close().*/
}

return 0;
}


Related Solutions

Use Visual Python or equivalent, to write a program that allows the user to observe the...
Use Visual Python or equivalent, to write a program that allows the user to observe the following: Damped and forced harmonic oscillators. The program should be user friendly and have default values for the initial velocities, positions, masses, and spring constants as well as damping constants. Values and frequencies for the forced oscillators should also be given. It should allow the user to input values for the velocities, positions, spring constants, and masses. The program should determine automatically the scale...
C++ DO not use arrays to write this program. Write a program that repeatedly generates three...
C++ DO not use arrays to write this program. Write a program that repeatedly generates three random integers in the range [1, 100] and continues as follows: If the right-most digit of all the three integers is equal, the program displays them in ascending order on the screen and continues. If the generated integers have different right-most digits, they are not displayed and the program continues. The program terminates once the right-most digits of all the three random numbers are...
Code in C# please. Write a program that will use the greedy algorithm. This program will...
Code in C# please. Write a program that will use the greedy algorithm. This program will ask a user to enter the cost of an item. This program will ask the user to enter the amount the user is paying. This program will return the change after subtracting the item cost by the amount paid. Using the greedy algorithm, the code should check for the type of bill. Example: Cost of item is $15.50 User pays a $20 bill $20...
The Fallen Flags Division of Block C Enterprises makes toy trucks and cars by a plastic...
The Fallen Flags Division of Block C Enterprises makes toy trucks and cars by a plastic molding process. The panel truck requires 3 minutes of molding time, 2 minutes of painting time and 1 minute of packing time. The pickup truck requires 2.5 minutes of molding time, 3 minutes of painting time and 1 minute of packing time. The car requires 1.5 minutes of molding time, 1.5 minutes of painting time and 1 minutes of packing time. There are a...
use linux or c program. please provide the answer in details. Write a program that will...
use linux or c program. please provide the answer in details. Write a program that will simulate non - preemptive process scheduling algorithm: First Come – First Serve Your program should input the information necessary for the calculation of average turnaround time including: Time required for a job execution; Arrival time; The output of the program should include: starting and terminating time for each job, turnaround time for each job, average turnaround time. Step 1: generate the input data (totally...
Write a C program with the following prompt *do not use gotostatement* "Write an interactive...
Write a C program with the following prompt *do not use goto statement* "Write an interactive program that implements a simple calculator. The program should allow the user to choose a binary arithmetic operation and enter two terms to which to apply the operation. The program should then compute the result and display it to the user. Your calculator will have two modes: double-precision mode and integer mode. Double-precision mode will do calculations and output using variables of type double....
write a simple program to demonstrate the use of static type of variables in c++... use...
write a simple program to demonstrate the use of static type of variables in c++... use comments to explain plz
In C program, Use "do...while" and "for" loops to write a program that finds all prime...
In C program, Use "do...while" and "for" loops to write a program that finds all prime numbers less than a specified value.
PLease use c++ Write a checkbook balancing program. The program will read in, from the console,...
PLease use c++ Write a checkbook balancing program. The program will read in, from the console, the following for all checks that were not cashed as of the last time you balanced your checkbook: the number of each check (int), the amount of the check (double), and whether or not it has been cashed (1 or 0, boolean in the array). Use an array with the class as the type. The class should be a class for a check. There...
Create a C++ program that makes use of both concepts i.e. Array of structure and array...
Create a C++ program that makes use of both concepts i.e. Array of structure and array within the structure by using the following guidelines: 1. Create an Array of 5 Structures of Student Records 2. Each structure must contain: a. An array of Full Name of Student b. Registration Number in proper Format i.e 18-SE-24 c. An array of Marks of 3 subjects d. Display that information of all students in Ascending order using “Name” e. Search a particular student...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT