0

I have a text file and I wanted to extract only a specific part of it at a particular time.For that ,I used ftell() while writing to note the start and end positions and then use fseek() to jump to that particular location.

     int main()
    {
       FILE *fp=fopen("myt","w+");
       char s[80];
       printf ( "\nEnter a few lines of text:\n" ) ;
       while ( strlen ( gets ( s ) ) > 0 )  //user inputs random data
      {                                     //till enter is pressed
       fputs ( s, fp ) ;
       fputs ( "\n", fp ) ;
      }

     long int a=ftell(fp);
     fputs("this line is supposed to be printed only ",fp);//line to be 
                                                           // displayed
     fputs("\n",fp);
     long int b=ftell(fp);
     printf("start is %ld",a);
     printf("\nend is %ld",b);
     printf("here is the data...\n");
     rewind(fp);
     fseek(fp,a,SEEK_CUR);   //move to the starting position of text to be 
                            //displayed
     char x[1000];
     fgets(x,b-a,SEEK_CUR);
     printf("%s",x);
     return 1;
  }

I tried this but face a unexpected abnormal termination of program.Please guide me as to how correctly implement my task.

  • 1
    Check if `fp` is `NULL` right after the call to `fopen`. If it's `NULL`, the file could not be opened for some reason (most likely because it doesn't exist, or it doesn't exist in the current directory of the program), in that case you should print an error messaqge and abort the program. – Jabberwocky Nov 14 '17 at 14:07
  • 2
    The third argument to `fgets()` has to be the file stream `fp`, not `SEEK_CUR` – Ctx Nov 14 '17 at 14:08
  • 2
    Also compile with all warnings enabled and include the required header files (at least stdio.h and string.h), and don't use `gets` it has been deprecated 20 years ago. – Jabberwocky Nov 14 '17 at 14:09
  • 3
    Never use `gets`. Just don't. – klutt Nov 14 '17 at 14:17
  • _"I tried this but face a unexpected abnormal termination of program."_ When? What was the exact timing, message, etc.? Have you tried using a debugger to figure out why that happened? – underscore_d Nov 14 '17 at 14:17
  • +underscore_d I tried this on codeblocks.The terminal hangs and force closes. – SARTHAK MEHRA Nov 14 '17 at 14:21
  • What should I use in case of fgets? suggest an alternate with syntax – SARTHAK MEHRA Nov 14 '17 at 14:22

1 Answers1

1

You want this:

Comments starting with //// are mine

#include <stdio.h>     //// include required header files
#include <string.h>

int main()
{
  FILE *fp = fopen("myt", "w+");

  if (fp == NULL)     //// test if file has been opened sucessfully
  {
    printf("Can't open file\n");
    return 1;         //// return 1 in case of failure
  }

  char s[80];
  printf("\nEnter a few lines of text:\n");
  while (strlen(gets(s)) > 0)  //user inputs random data
  {                                     //till enter is pressed
    fputs(s, fp);
    fputs("\n", fp);
  }

  long int a = ftell(fp);
  fputs("this line is supposed to be printed only ", fp);//line to be 
                                                         // displayed
  fputs("\n", fp);
  long int b = ftell(fp);
  printf("start is %ld", a);
  printf("\nend is %ld", b);
  printf("here is the data...\n");
  rewind(fp);
  fseek(fp, a, SEEK_CUR);   //move to the starting position of text to be 
                            //displayed
  char x[1000];
  fgets(x, sizeof(x), fp); //// the usage of fgets was totally wrong
  printf("%s", x);

  return 0;   //// return 0 in case of success, no one
}

Disclaimer: The first part reading the strings using gets is still sloppy, you should never use gets, it's an old deprecated function. Use fgets instead.

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
  • How do I print a text which is exactly in between two positions of the file.Your fgets(x,sizeof(x),fp) will print all the data after that position. – SARTHAK MEHRA Nov 14 '17 at 14:35
  • 1
    I let you find out this as an exercise. A piece of paper and a pencil can help here, using your debugger too.This answer answers your question (that is it resolves the unexpected abnormal termination problem). – Jabberwocky Nov 14 '17 at 14:41
  • can we find the difference between the two file positions and use the value to create a char array to store the data? – SARTHAK MEHRA Nov 14 '17 at 14:49
  • Whoa! It worked.I suppose the explicit declaration of no of characters to read in long int type was causing a problem. – SARTHAK MEHRA Nov 14 '17 at 15:15