bsc

Comprehensive codebase and cou...
Log | Files | Refs | Activity | README | LICENSE

root / semester_1 / letusc / luc092.c

luc092.c (1146B)


      1 /* Write a program to store names in a file. Display the n-th name in the list, where n is read from the keyboard.
      2 */
      3 /* Let Us C, Chap- 19 (File Input/Output), Qn No.: B(h) */
      4 
      5 /* This file is auto-generated by a bot. */
      6 /* This code is not compiled; it is for reference only. */
      7 
      8 
      9 #include <stdio.h>
     10 #include <stdlib.h>
     11 #include <string.h>
     12 #include <ctype.h>
     13 
     14 void create_name_file();
     15 
     16 int main()
     17 {
     18     FILE *fp;
     19     char name[50];
     20     int n, current = 0, found = 0;
     21 
     22     create_name_file();
     23 
     24     printf("Enter value of n to find n-th name: ");
     25     scanf("%d", &n);
     26 
     27     fp = fopen("names.txt", "r");
     28     if (!fp) exit(1);
     29 
     30     // Assuming one name per line
     31     while (fgets(name, sizeof(name), fp) != NULL)
     32     {
     33         current++;
     34         if (current == n)
     35         {
     36             printf("The %d-th name is: %s", n, name);
     37             found = 1;
     38             break;
     39         }
     40     }
     41 
     42     if (!found)
     43         printf("Record not found (Only %d names exist).\n", current);
     44 
     45     fclose(fp);
     46     return 0;
     47 }
     48 
     49 void create_name_file()
     50 {
     51     FILE *f = fopen("names.txt", "w");
     52     fprintf(f, "Alice\nBob\nCharlie\nDavid\nEve\nFrank\n");
     53     fclose(f);
     54 }
© notamitgamer • Site Built: 2026-09-05 01:53:16 UTC • git-mirror commit: c170d72 [view raw info]
Originally created with stagit • modified by notamitgamer
Forked from github.com/notamitgamer/git-mirror