in c estse repor Q3) Write a C program that reads from the user a name of a file
ID: 3730477 • Letter: I
Question
in c
estse repor Q3) Write a C program that reads from the user a name of a file and a directory path. The file should exist in the current directory. Then the program should search in the path given whether the file name is found or not. It should search in all subdirectories of the given path. If there exists a file with th e same name, the program should also check if they have the same contents. The program outputs the following messages in the following cases . If the found file has a complete match: "Complete match: file path" the found file has a complete match: "Complete match: file_path.". If only the name matches: "Name match: file path." If the file not found: "File not found." Also the program checks for all possible errors and mistakes and display a correct and clear error messages Save your file as hw3q3. Do not use any unix commands in your code. Include the source file to the report. Show the compilation commands in your report. Test your run several times with all search possibilities and show your tests in the report. ,04) Write a C program that takes two string inputs from the user that represent two file names. The first file contains some integer numbers for input, while the second file will be used for output. Then, open the first file for input and the second file for output. Next, the mairn process creates a child process that shares the two files. The child process counts the even numbers from the input file and save the line: Child process PID: xxxx count number of even numbers- yy" in the output file and terminates. Meanwhile, the parent process counts the odd numbers concurrently, then it reaps the child process. After that, it prints to the output file the line: " Parent process PID: xxxx count number of odd numbers yy". save your file as hw3g4. Show the compilation commands in your report. Test your run several timnes with all earch possilities and tiow your tests in the report. 2 I P ag eExplanation / Answer
3.
#include <unistd.h>
#include <sys/types.h>
#include <dirent.h>
#include <stdio.h>
#include <string.h>
void listdir(const char *path, char *name, int n)
{
DIR *dir;
struct dirent *entry;
int found = 0;
if (!(dir = opendir(path)))
return;
while ((entry = readdir(dir)) != NULL) {
if (entry->d_type == DT_DIR) {
char path1[1024];
snprintf(path1, sizeof(path), "%s/%s", path, entry->d_name);
listdir(path1,name, n+1);
} else {
if (strcmp(entry->d_name, name) == 0){
if (n == 0){
printf("Complete match: %s ",path);
}
else {
printf("Nmme match: %s ",path);
}
found = 1;
return;
}
}
}
if (found == 0)
printf("File Not Found ");
closedir(dir);
}
int main(void) {
char name[50];
char path[50];
printf("Enter a file name:");
scanf("%s", name);
printf("Enter a path:");
scanf("%s", path);
listdir(path, name, 0);
return 0;
}
4.
/* Following is the code for parent, child and zombie process */
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>
#include <stdlib.h>
/*The Main Function Start*/
int main(int argc, char *argv[])
{
int count = 0;
int pid;
char file1[30];
char file2[30];
printf("Enter input file name:");
scanf("%s",file1);
printf("Enter output file name:");
scanf("%s",file2);
pid = fork();
if (pid < 0){
perror("Error in fork ");
return 0;
}
if (pid == 0){
FILE *fp1;
FILE *fp2;
int a;
fp1 = fopen(file1,"r");
if (fp1 == NULL){
printf("Error opening file in child ");
return 0;
}
fp2 = fopen(file2,"w");
if (fp1 == NULL){
printf("Error opening file in child ");
return 0;
}
int count = 0;
while (!feof(fp1)){
fscanf(fp1, "%d",&a);
if (a % 2 == 0)
count++;
}
fclose(fp1);
//printf(" Chile process PID:%d count number of even numbers = %d ",getpid(), count);
fprintf(fp2," Child process PID:%d count number of even numbers = %d ",getpid(), count);
fclose(fp2);
}
if (pid){
int returnStatus;
waitpid(pid, &returnStatus, 0);
//printf("Hello ");
FILE *fp3;
FILE *fp4;
int a;
fp3 = fopen(file1,"r");
if (fp3 == NULL){
printf("Error opening file in child ");
return 0;
}
fp4 = fopen(file2,"a");
if (fp4 == NULL){
printf("Error opening file in child ");
return 0;
}
int count = 0;
while (!feof(fp3)){
fscanf(fp3, "%d",&a);
if (a % 2 != 0)
count++;
}
fclose(fp3);
fprintf(fp4," Parent process PID:%d count number of odd numbers = %d ",getpid(), count);
fclose(fp4);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.