0

I want to give input as line number and get output as the corresponding text for that line number in a text file.

Sample text file:

Hi this is Stefen  
Hi How are you

Example input:

Enter the line number:2

Expected Output:

Hi How are you

My program is:

#include <stdio.h>
#include <stdlib.h>

int main() {
    FILE *fp;
    fp = fopen("sample.txt", "r");
    if (fp == NULL) {
        perror("Unable to open the file\n");
        exit(1);
    }
    char buf[256];
    
    while (fgets(buf, sizeof(buf), fp) != NULL) {
        printf("%s\n", buf);
        print("~~~~\n");
    }
            
    fclose(fp);
            
    return 0;
}

Output I got:(The entire file with the separator ~~~~ below each line)

Hi this is Stefen
~~~~
Hi How are you
~~~~

Can anyone please tell me how to do this?

chqrlie
  • 131,814
  • 10
  • 121
  • 189
harini
  • 3
  • 3
  • 3
    add a `count` variable initialized to `0` and incremented inside the loop. For the first version of your program assume the loop reads one full line each time. Print only when `counter` is the desired line number. – pmg Jun 03 '21 at 09:34

3 Answers3

1

Best to use a second file

check if you're at \n that means new line and increment a variable like "line"

    printf(" \n Enter line number of the line to be deleted:");
    scanf("%d", &delete_line);
    //open new file in write mode
    ptr2 = fopen("c:\\CTEMP\\newfile.txt", "w");
     if(ptr2==NULL)
    printf("second error opening newfile");
    while (!feof(ptr1))
    {
        ch = fgetc(ptr1);
        if (ch == '\n')
        {
            temp++;
        }
        //except the line to be deleted
        if (temp != delete_line)
        {
            //copy all lines in file newfile.c
            fputc(ch, ptr2);
        }
    }
    
    fclose(ptr1);
    fclose(ptr2);

"detele_line" variable is for the user to inter.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Marwane
  • 333
  • 3
  • 13
  • Don't do this: `while (!feof(ptr1))`: https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong – chqrlie Jun 04 '21 at 10:26
  • Also, your code fragment attempts to do exactly the opposite of the OP's goal: *I want to give input as line number and get output as the corresponding text for that line number in a text file.* – chqrlie Jun 04 '21 at 10:27
1

As pmg suggests, would you please try the following:

#include <stdio.h>
#include <stdlib.h>
#define INFILE "sample.txt"

int main()
{
    FILE *fp;
    char buf[BUFSIZ];
    int count = 0, n;

    fp = fopen(INFILE, "r");
    if (fp == NULL) {
        perror(INFILE);
        exit(1);
    }

    printf("Enter the line number: ");
    fgets(buf, sizeof buf, stdin);
    n = (int)strtol(buf, (char **)NULL, 10);

    while (fgets(buf, sizeof buf , fp) != NULL){
        if (++count == n) {
            printf("%s", buf);
            break;
        }
    }

    fclose(fp);
    return EXIT_SUCCESS;
}
tshiono
  • 21,248
  • 2
  • 14
  • 22
0

The easiest way is using array to save the lines, then print the certain line.

#include <stdio.h>
 
#define M 10010
#define N 256
char buf[M][N];
 
int main(){
    FILE *file;
    char fileName[50] = "sample.txt";
 
    file = fopen(fileName, "r");
    if(file == NULL)
        return 1;
    int n = 0;
    while(fgets(buf[n], N, file) != NULL){
        n++;
    }
    fclose(file);
 
    int i, x;
    printf("Example input:\nEnter the line number:");
    scanf("%d", &x);
    printf("Expected Output:\n%s", buf[x-1]);
 
    return 0;
}
LinconFive
  • 1,718
  • 1
  • 19
  • 24