Write a C program so that a user can enter first and last name of students. Gene
ID: 3827413 • Letter: W
Question
Write a C program so that a user can enter first and last name of students. Generate email address and identification number of the students. Search by last name and if a match is found display the email address and identification number of the student in the console. Please make sure that is has DELETE AND SORTING CAPABILITIES.
Extra credit : Store the entries into text file.
Suggestion: use a function enter() to take the information from the user and generate email address and identification number. Use a search() function to search for the student.
Explanation / Answer
Hi,
I could not write the exact functionlity but this code wil search for the string in the file and it will print the entire line if the string matches-
C code-
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int search(FILE *, char *);
void main(int argc, char * argv[])
{
FILE *fp1;
int p;
fp1 = fopen("data.txt", "r+");
if (fp1 == NULL)
{
printf("cannot open the file ");
exit(0);
}
search(fp1, "abc");
fclose(fp1);
}
/* Searches the lines */
int search(FILE *fp, char * str)
{
FILE *fp1;
fp1 = fopen("fp1","w");
char s[10],c;
int len = strlen(str);
int i = 0;
int d;
int seek = fseek(fp, 0, 0);
c = fgetc(fp);
while (c != EOF)
{
if (c == ' ' || c == ' ')
{
s[i] = '';
i = 0;
if (strcmp(s, str) == 0)
{
while (c = fgetc(fp) != ' ')
{
fseek(fp, -2L, 1);
d = ftell(fp);
}
while ((c = fgetc(fp)) != ' ')
{
fputc(c, fp1);
printf("The value is ",fp1);
}
}
}
else
{
s[i] = c;
i++;
}
c = fgetc(fp);
}
return 1;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.