Basic C program Create a text file that contains 10 names, read from the file to
ID: 3736210 • Letter: B
Question
Basic C program Create a text file that contains 10 names, read from the file to see if a given name by the user exists or not. If name exists print name found. Otherwise print couldn’t find the name. Basic C program Create a text file that contains 10 names, read from the file to see if a given name by the user exists or not. If name exists print name found. Otherwise print couldn’t find the name. Create a text file that contains 10 names, read from the file to see if a given name by the user exists or not. If name exists print name found. Otherwise print couldn’t find the name.Explanation / Answer
#include<stdio.h>
#include <stdlib.h>
int main()
{
FILE *file_pointer;
FILE * fp;
char * line = NULL;
size_t len = 0;
ssize_t read;
char str[100];
char fileNames[100];
scanf("%s",str);
file_pointer = fopen("name.txt", "w");
fprintf(file_pointer, "Jitu ");
fprintf(file_pointer, "Mani ");
fprintf(file_pointer, "Santosh ");
fprintf(file_pointer, "Siva ");
fprintf(file_pointer, "Jagan ");
fprintf(file_pointer, "Sai ");
fprintf(file_pointer, "Ravi ");
fprintf(file_pointer, "Bhanu ");
fprintf(file_pointer, "Guptha ");
fprintf(file_pointer, "Mohan ");
fclose(file_pointer);
fp = fopen("name.txt", "r");
while ((read = getline(&line, &len, fp)) != -1) {
if(fileNames==line)
{
printf("Given name is found %d",fileNames);
}
}
else
{
printf("Given name is could not find %",fileNames);
}
fclose(fp);
}