WRITE A CODE IN C TO SEARCH A FILE FROM NOTEPAD FILE.



WRITE A CODE IN C TO SEARCH A FILE FROM NOTEPAD FILE...

Answer / Somdutta Singh

Here's a simple example of C code to search for a specific string within a .txt file using the fopen(), fread(), and strstr() functions. Note that this code assumes that the file is present in the current working directory:
1. #include <stdio.h>
2. #include <string.h>
3. int main(int argc, char *argv[]) {
4. FILE *file;
5. char filename[] = "example.txt";
6. char line[100];
7. char target[] = "keyword"; // replace 'keyword' with your search term
8. if ((file = fopen(filename, "r")) != NULL) {
9. while (fgets(line, sizeof(line), file) != NULL) {
10. if (strstr(line, target) != NULL) {
11. printf("Found the keyword in the file.n");
12. break;
}
}
13. fclose(file);
14. } else {
15. printf("Unable to open the file.n");
16. }
17. return 0;
18. }

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More C Interview Questions

write a program of bubble sort using pointer?

3 Answers   TCS,


What is the difference between exit() and _exit() function?

1 Answers  


main() { char *p; p="Hello"; printf("%c\n",*&*p); }

2 Answers   ME,


Why & is used in scanf in c?

1 Answers  


ABCDCBA ABC CBA AB BA A A

4 Answers   TCS,


Write a Program to accept different goods with the number, price and date of purchase and display them

1 Answers   HDFC,


Describe newline escape sequence with a sample program?

1 Answers  


Write a program to implement a round robin scheduler and calculate the average waiting time.Arrival time, burst time, time quantum, and no. of processes should be the inputs.

1 Answers   Aspiring Minds,


what is the difference between call by value and call by reference?

5 Answers   Genpact, Global Logic, Infosys,


Explain Linker and Loader

5 Answers  


why use functions a) writing functions avoids rewriting the same code over and over b) using functions it becomes easier to write programs and keep track of what they are doing c) a & b d) none of the above

1 Answers  


Do you know the use of 'auto' keyword?

1 Answers  


Categories