0% found this document useful (0 votes)
39 views

1.demonstration of Open, Read, Write and Close Functions Pract2.c

The document demonstrates C programs for various tasks: 1. Opening, reading, writing and closing a file using functions like open(), read(), write() and close(). It writes and reads a message from a file. 2. Copying contents from one file to another using fopen(), fgetc(), fputc() and fclose() functions. It copies character by character and prints success message. 3. Calculating factorial of a number using recursion and iteration. It takes number as argument, calculates factorial and prints result.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views

1.demonstration of Open, Read, Write and Close Functions Pract2.c

The document demonstrates C programs for various tasks: 1. Opening, reading, writing and closing a file using functions like open(), read(), write() and close(). It writes and reads a message from a file. 2. Copying contents from one file to another using fopen(), fgetc(), fputc() and fclose() functions. It copies character by character and prints success message. 3. Calculating factorial of a number using recursion and iteration. It takes number as argument, calculates factorial and prints result.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

1.

Demonstration of open(), read(), write() and close() functions

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

/*Do the following program and save as cp. or copy.c */

/**
* 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;

/* Input path of files to copy */


printf("Enter source file path: ");
scanf("%s", sourcePath);
printf("Enter destination file path: ");
scanf("%s", destPath);

/*
* Open source file in 'r' and
* destination file in 'w' mode
*/
sourceFile = fopen(sourcePath, "r");
destFile = fopen(destPath, "w");

/* fopen() return NULL if unable to open file in given mode. */


if (sourceFile == NULL || destFile == NULL)
{
/* Unable to open file hence exit */
printf("\nUnable to open file.\n");
printf("Please check if file exists and you have read/write privilege.\n");

exit(EXIT_FAILURE);
}

/*
* Copy file contents character by character.
*/
ch = fgetc(sourceFile);
while (ch != EOF)
{
/* Write to destination file */
fputc(ch, destFile);

/* Read next character from source file */


ch = fgetc(sourceFile);
}

printf("\nFiles copied successfully.\n");

/* Finally close files to release resources */


fclose(sourceFile);
fclose(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

#Encryption and decryption.

tr a-z mnbvcxzlkjhgfdsapoiuytrewq < file1 >file2

tr mnbvcxzlkjhgfdsapoiuytrewq <file2 >file3

#No of unique words in the given intput

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

awk –F”~” `{print “dept name”;} {print $2;}` emp.data


awk –F”~” $2 ~/commerce/ `{print $1, $3;}` emp.data
awk –F”~” `BEGIN{sum=0} $2 ~/commerce/ {sum=$sum + $3;}
END {print “total sal:”, $sum;}` emp.data

Write an AWK program which accepts input from the standard input and prints the total of
any column specified as an argument

cut -d"|" -f3 emp2.txt | uniq > temp.txt


echo
echo "Employee Salary Report"
echo
for k in `cat temp.txt`
do
echo $k
echo "----------------------------------------------------------------------"
cat emp2.txt | awk -F'|' '{printf("%-20s %-15s %15s\n",$1,$2,$3)}' | awk /`echo $k`/
echo "----------------------------------------------------------------------"
echo -n " Total Salary : "
cat emp2.txt | awk /`echo $k`/ | awk -F'|' '{sum+=$3} END {print sum}'
echo
done

You might also like