Escolar Documentos
Profissional Documentos
Cultura Documentos
Ans:
#include <stdio.h>
int main()
{
char tmpname[L_tmpnam];
char *filename;
FILE *tmpfp;
filename = tmpnam(tmpname);
printf("Temporary file name is: %s\n", filename);
tmpfp = tmpfile();
if(tmpfp)
printf("Opened a temporary file OK\n");
else
perror("tmpfile");
exit(0);
}
PART B
4. Write a C program to check if any key is hit and make change in the
terminal settings as follows:
c- Switch to canonical mode.
n- Switch to non canonical mode.
s- Does not allow the signal keys like ctrl+D etc.
o- Reset the original settings.
q- Quit from program.
And while waiting for any key to hit it should print the message “Please
press any key”.
Ans :-
#include<stdlib.h>
#include<stdio.h>
#include<unistd.h>
#include<fcntl.h>
#include<term.h>
#include<curses.h>
#include<termios.h>
Int main()
{
Char ch;
While ()
Ans:
#include <stdio.h>
#include <unistd.h>
char *menu[] = {
"a − add new record",
"d − delete record",
"q − quit",
NULL,
};
int getchoice(char *greet, char *choices[], FILE *in, FILE *out);
int main()
{
int choice = 0;
FILE *input;
FILE *output;
if(!isatty(fileno(stdout))) {
fprintf(stderr,"You are not a terminal, OK.\n");
}
input = fopen("/dev/tty", "r");
output = fopen("/dev/tty", "w");
if(!input || !output) {
fprintf(stderr,"Unable to open /dev/tty\n");
exit(1);
}
do {
choice = getchoice("Please select an action", menu, input, output);
printf("You have chosen: %c\n", choice);
} while(choice != 'q');
exit(0);
}
int getchoice(char *greet, char *choices[], FILE *in, FILE *out)
{
int chosen = 0;
int selected;
char **option;
do {
fprintf(out,"Choice: %s\n",greet);
option = choices;
while(*option) {
fprintf(out,"%s\n",*option);
option++;
}
do {
selected = fgetc(in);
} while(selected == '\n');
option = choices;
while(*option) {
if(selected == *option[0]) {
chosen = 1;
break;
}
option++;
}
if(!chosen) {
fprintf(out,"Incorrect choice, select again\n");
}
} while(!chosen);
return selected;
}
The program should de-allocate the memory occupied before exiting from the
program.
Q6 WAP to create a file with name provided as command line argument and place a
write lock on it
ANS:
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
int main (int argc, char* argv[])
{
char* file = argv[1];
int fd;
struct flock lock;
printf ("opening %s\n", file);
/* Open a file descriptor to the file. */
fd = open (file, O_WRONLY);
printf ("locking\n");
/* Initialize the flock structure. */
memset (&lock, 0, sizeof(lock));
lock.l_type = F_WRLCK;
/* Place a write lock on the file. */
fcntl (fd, F_SETLKW, &lock);
printf ("locked; hit Enter to unlock... ");
/* Wait for the user to hit Enter. */
getchar ();
printf ("unlocking\n");
/* Release the lock. */
lock.l_type = F_UNLCK;
fcntl (fd, F_SETLKW, &lock);
close (fd);
return 0;
}