Professional Documents
Culture Documents
PART A
#include<unistd.h>
#include<fstream.h>
#define A_MEGABYTE (1024 * 1024)
int main()
{
char *little_memory;
int megabyte = A_MEGABYTE;
int exit_code = EXIT_FAILURE;
some_memory = (char *)malloc(megabyte);
if (little_memory != NULL) {
sprintf(little_memory,”lovely professional university \n”);
int fputc(int c, FILE *stream);
int putc(int c, FILE *stream);
char *getcwd(char *buf, size_t 20);
printf(“%s”, little_memory);
exit_code = EXIT_SUCCESS;
}
exit( 0 );
}
2. WAP to read a ‘C’ program file and count the following in the program.
1. Total no. of statements
2. Total no. of included files
3. Total no. of blocks and brackets
#include<stdio.h>
#include<sys/stat.h>
#include<dirent.h>
#include<libgen.h>
public class NumberOfLine{
public static void main(String[] args) {
try{
System.out.println("Getting line number of a paritcular file examp
le!");
BufferedReader bf = new BufferedReader(new InputStreamReader(Syste
m.in));
System.out.println("Please enter file name with extension:");
String str = bf.readLine();
File file = new File(str);
if (file.exists()){
FileReader fr = new FileReader(file);
LineNumberReader ln = new LineNumberReader(fr);
int count = 0;
while (ln.readLine() != null){
count++;
}
System.out.println("Total line no: " + count);
ln.close();
}
else{
System.out.println("File does not exists!");
}
}
catch(IOException e){
e.printStackTrace();
}
}
}
3. Write a C program which takes directory name as input and delete all
files, within that directory.
#include <stdio.h>
#include <sys/stat.h>
#include <dirent.h>
#include <libgen.h>
DIR *dp;
struct dirent *ep;
char abs_filename[FILENAME_MAX];
dp = opendir (dirname);
if (dp != NULL)
{
while (ep = readdir (dp)) {
struct stat stFileInfo;
if(S_ISDIR(stFileInfo.st_mode)) {
if(strcmp(ep->d_name, ".") &&
strcmp(ep->d_name, "..")) {
printf("%s directory\n",abs_filename);
recursiveDelete(abs_filename);
}
} else {
printf("%s file\n",abs_filename);
remove(abs_filename);
}
}
(void) closedir (dp);
}
else
perror ("Couldn't open the directory");
remove(dirname);
return 0;
}
2.
PART B
Q4. Illustrate through example in which scenario would you prefer using
perror ( ) and in which strerror ( ).
Ans:
The function strerror() is very similar to perror(), except it returns a pointer to
the error message string for a given value (you usually pass in the variable errno.)
"perror" prints out an error message based on the current value of the external
variable "errno". The value of "errno" is set by C library functions when error
conditions are encountered. Thus the message printed by "perror" reflects the last
error encountered during a call to a library function. The function "strerror" obtains
the text of the appropriate error message.
Syntax of strerror:
#include<string.h>
Char *strerroe(int errnum);
Syntax of perror:
#include<stdio.h>
Void perror(const char *s);
Example
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <io.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
Output
#include<stdio.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<stdlib.h>
#include<unistd.h>
Int main()
{
Int m;
M=open(“ yogendra ”, O_WRONLY , S_IRUSR,S_IRGRP , S_IROTH );
Exit(0);
}
Your Rollno
Ans:
mkdir –p /your_rollno/previour_rollno
cd your_rollno
mkdir next_rollno
AND 2nd method is:
$ Mkdir your_rollno
$ cd your_rollno
Now home directory your_rollno will we set than we can make sub directory………….
Now next_rollno and previous_rollno sub directory has make inside home directory
your_rollno……………………….