1

I am trying to fully grasp how these functions work.

From the Reference50 website:

fseek

FILE* out = fopen("out.txt", "w+");

fputs("Team Bowden Fever!", out);

char buffer[50];

fseek(out, 0, SEEK_SET);

fgets(buffer, 50, out);
printf("%s\n", buffer);

fclose(out);

fgets:

FILE* fp = fopen("example.txt", "r");

if (fp == NULL)
{
    printf("File does not exist.\n");
    return 1;
}

char s[2]; //wanted to try out what happens if I change the value from 50 to 2. 

while(fgets(s, 2, fp) != NULL)
{
    printf("%s", s);
}

fclose(fp);

fseek

In the first snippet of code a string of text is outputted to out.txt. Then an array of 50 chars is defined to contain the string. fgets then reads in, at most, 50 characters from file out.txt, storing them temporarily in buffer buffer which is then printed out to the screen by means of the printf function.

  • What does fseek do in this case?

There's an offest of 0 bytes and SEEK_SET changes the location of the pointer out to the beginning of the file.

  • Why does nothing get printed out if I omit the fseek function or even change SEEK_SET to SEEK_CUR or to SEEK_END?

  • What's the current position of the file pointer before fseek?

fgets

Assuming there's a file called example.txt in my directory which contains the string: hello world!

  • why does it still get printed out integrally even though I changed the numbers of characters to be read from the file? (from 50 to 2 i.e.)
1

Fseek works by moving the file pointer, indicated by the FILE pointer in our case, forward or backward. So whenever you use functions that work with file streams, you should keep in mind where the file position pointer. So when you make changes to the output file using fputs, you are moving the file pointer. Clearly, you can see why when you try to read and print buffer, you get some weird symbols. That's why you call fseek first before you read from your file. The following snippet does the same thing too. But instead of using an offset of 0, we use negative number to notify the function to move backwards. `

#include <stdio.h>
#include <string.h>

int main()
{
    FILE* out = fopen("out.txt", "w+");

    fputs("Team Bowden Fever!", out);

    char buffer[50];

    fseek(out, -(strlen("Team Bowden Fever!")), SEEK_CUR);

    fgets(buffer, 50, out);
    printf("%s\n", buffer);

    fclose(out);
    return 0;
}

SEEK_END, SEEK_CUR and SEEK_SET tell the function where to start from. Your second question is not clear. Did you change the second parameter of fgets from 50 to 2 or you buffer size from 50 to 2. These two scenarios have different outcomes, for instance, if you had a buffer of size 2 bytes but you try to read 50 bytes, i.e fgets(s, 50, fp), then you'll get a segmentation fault because you are trying to write 50 bytes to a location in memory where you're limited to only 2 bytes. The only fix is to change to size you read. In the second scenario, you will not get an error since you're not overflowing the memory area you have access to.

I hope this answers your question about fseek and fgets.

| improve this answer | |
  • Thank you Natnael for taking the time to answer my questions. Now it's clearer. If I omit the fseek function in the first example, nothing gets printed out because the file pointer is at the end. Is it correct? In the second example I changed the second parameter of fgets from 50 to 2. But why does it keep printing out the entire string of text contained in example.txt? Shouldn't it read a maximum of 2 characters? – Alberto May 31 '16 at 21:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .