1.demonstration of Open, Read, Write and Close Functions Pract2.c
1.demonstration of Open, Read, Write and Close Functions Pract2.c
Pract2.c
#include <stdio.h>
#include <fcntl.h>
int main()
{
int fd;
char buffer[80];
static char message[]=”Hello, SPCE – Bangalore”;
fd=open(“myfile.txt”,O_RDWR);
if (fd != -1)
{
printf(“myfile.txt opened with read/write access\n”);
write(fd,message,sizeof(message));
lseek(fd,0,0);
read(fd,buffer,sizeof(message));
printf(“%s — was written to myfile.txt \n”,buffer);
close(fd);
}
}
2.Write a C program to demonstrate the cp command – or
copy a file from one to another in unix environment. Use cc or gcc to compile your copy/cp command
/**
* C program to copy contents of one file to another.
*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *sourceFile;
FILE *destFile;
char sourcePath[100];
char destPath[100];
char ch;
/*
* Open source file in 'r' and
* destination file in 'w' mode
*/
sourceFile = fopen(sourcePath, "r");
destFile = fopen(destPath, "w");
exit(EXIT_FAILURE);
}
/*
* Copy file contents character by character.
*/
ch = fgetc(sourceFile);
while (ch != EOF)
{
/* Write to destination file */
fputc(ch, destFile);
return 0;
}
Sample programs
factorial()
{
number=$1
fact=1
while test $number -gt 1
do
fact=`expr $fact \* $number`
number=`expr $number - 1`
done
echo "$fact"
}
if test $# -ne 1
then echo "please enter a valid number"
else
echo "the factorial of $1 is...:"
factorial $1
fi
using recursion
fact=1
factorial()
{
n1=$1
if test $1 -eq 0
then
return
else
fact=`expr $n1 \* $fact`
n1=`expr $n1 - 1`
factorial $n1
fi
}
if test $# -ne 1
then echo "please enter a valid number"
else
factorial $1
echo "the factorial of $1 is...:$fact"
fi
wordcount()
{
count=0
wrcount=0
echo "please enter a set of strings"
read string
for wrkey in $string
do
count=0
for wr in $string
do
if test $wrkey = $wr
then
count=`expr $count + 1`
fi
done
if test $count -eq 1
then
wrcount=`expr $wrcount + 1`
fi
done
return $wrcount
}
wordcount
echo "the number of unique words are...:$?"
or unique CMD
Write an AWK program that reads a file and prints a report that groups employees of the
same dept. the following are the content of the report:
a) department name on the top
b) employee details
c) total salary
Write an AWK program which accepts input from the standard input and prints the total of
any column specified as an argument