-2

I am trying to extract and print a specific portion of text from a file at a given time.I used ftell() and fseek() to achieve this.

     #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);

    fputs("this line is also to be printed\n",fp);         //line to be 
                                                           //displayed
    fputs("\n",fp);

    long int b = ftell(fp);

    fputs("this is scrap line",fp);
    fputs("\n",fp);

    rewind(fp);
    fseek(fp, a, SEEK_CUR);  //move to the starting position of text to be
                             //displayed

    long int c=b-a;          //no of characters to be read
    char x[c];
    fgets(x, sizeof(x), fp); 
    printf("%s", x);

   fclose(fp);

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

I tried using this approach but the program just prints the first line.The output is as follows:

         this line is supposed to be printed only

I want to print both the lines intended to be printed.Please suggest an approach.

  • `fgets()` reads **a single line** (stops at newline). Either call `fgets()` multiple times or just read a given amount of data with `fread()`. –  Nov 20 '17 at 08:44
  • As this is unrelated to your actual question, I'm voting to close this as a "simple typo". –  Nov 20 '17 at 08:44
  • Can someone help me out with a bit of a code? I am a newbie and need some help – SARTHAK MEHRA Nov 20 '17 at 08:48
  • +Felix Palmen I have corrected the title.Kindly take down your vote – SARTHAK MEHRA Nov 20 '17 at 08:49
  • I just explained you what your problem is, it's an incorrect assumption about `fgets()`. Again, `fgets()` **stops reading** at the first newline character. What else do you need to fix this? :o –  Nov 20 '17 at 08:50
  • Yes I have understood that.I am just requesting as to how I should modify my implementation to fix this.I would be obliged if you could provide me with some working code. – SARTHAK MEHRA Nov 20 '17 at 08:58
  • I think you should write this code yourself, given the hints you get here. Look at the manual for [`fread()`](https://linux.die.net/man/3/fread), it's the function you're most likely looking for here. Check the return value to see whether you actually read the amout of bytes you expect. Take care to 0-terminate the result if you intend to use it with functions expecting a **string** (like `printf()`). –  Nov 20 '17 at 09:00
  • Why the down vote?It was a genuine problem which I could not find anywhere.. BTW I have seen stupid hello world programs getting up votes here. – SARTHAK MEHRA Nov 21 '17 at 15:16

1 Answers1

1

I think your intent for the reading portion was

rewind(fp);
fseek(fp, a, SEEK_CUR);  //move to the starting position of text to be
                         //displayed

long int c=b-a;          //no of characters to be read
char x[c+1];

int used = 0;
while(ftell(fp) < b)
{
  fgets(x+used, sizeof(x)-used, fp);
  used = strlen(x);
}
printf("%s", x);

Notes:

  • I added +1 to the allocation of your buffer x because fgets adds null termination.

  • I'm not 100% sure you don't want fflush(fp) between the writes and the reads.

lockcmpxchg8b
  • 2,205
  • 10
  • 16
  • Here, an approach with `fread()` would be simpler and more efficient. –  Nov 20 '17 at 08:57
  • Agree with one caveat: he opened the file in text mode. IIRC, the relationship between ftell and fread in text mode is tenuous. (I think ftell tracks all bytes, fread counts after collapsing CRLF) – lockcmpxchg8b Nov 20 '17 at 08:59
  • Thet's in general a problem with calculating *sizes* from `ftell()` results on a file opened in text mode. –  Nov 20 '17 at 09:03
  • 1
    The "safe" thing here would be (if it **must** be a text file) to not calculate the size at all and use `fgets()`, counting the number of lines instead... –  Nov 20 '17 at 09:04
  • agree that that's a much nicer solution – lockcmpxchg8b Nov 20 '17 at 09:05