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....
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.
Write this program in C++ language. Use the concept of structures. DO NOT use vectors. Q...
Write this program in C++ language. Use the concept of structures. DO NOT use vectors. Q (4) Create a structure called time. Its three members, all type int, should be called hours, minutes, and seconds. Write a program that prompts the user to enter a time value in hours, minutes, and seconds. This should be in 12:59:59 format. This entire input should be assigned first to a string variable. Then the string should be tokenized thereby assigning the 1st token...
(Use the string class to solve the problem) Write a program (in c++) that can be...
(Use the string class to solve the problem) Write a program (in c++) that can be used to train the user to use less sexist language by suggesting alternative versions of sentences given by the user. The program will ask for a sentence, read the sentence into a string variable, and replace all occurrences of masculine pronouns with gender-neutral pronouns. For example, it will replace “he” with “she or he”, and “him” with “her or him”. Be sure to preserve...
Can you write a small program in c++ demonstrate the use of pointer (*) & (&)....
Can you write a small program in c++ demonstrate the use of pointer (*) & (&). Can you also explain the pointer system and how to use them. Thank you.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT