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

int main()
{
   long int count; 
   FILE *file=NULL;

   file=fopen("sample.txt","r+");
   if(file==NULL)
   {
      printf("file open fail\n");
      return;
   }
   printf("file open succesfull\n");

   if(0!=fseek(file,1,SEEK_END))
   {
      printf("seek failed\n");
      return;
   }
   printf("seek successful\n");

   count=ftell(file);

   printf("%lu", count);

   return 0;
}

Output

file open succesfull
seek successful
3

My smaple.txt file has only one char and that is q. Why it is showing 3 here ? Also when I am having the file empty, then ftell() is returning 1, what is that?
Working on ubuntu 12.04

trojanfoe
  • 116,099
  • 18
  • 197
  • 233
user2799508
  • 816
  • 11
  • 36

3 Answers3

4

Your fseek(file, 1, SEEK_END) places the position one character beyond the end of the file. That explains why you observe count as one for the empty file. I guess that your file, that contains just a q, also contains a carriage return consisting of actually two characters. On character behind the end is 3, what you observed.

Engineer
  • 7,543
  • 7
  • 57
  • 96
Helmut Grohne
  • 5,442
  • 20
  • 51
2

You use fseek() incorrectly to determine the file's size via ftell().

From man fseek() (italics by me):

int fseek(FILE *stream, long offset, int whence);

[...] The new position, measured in bytes, is obtained by adding offset bytes to the position specified by whence.

This line:

if(0!=fseek(file,1,SEEK_END))

positions the file pointer 1 byte after the end of the file.

To fix this do:

if (0 != fseek(file, 0, SEEK_END))
alk
  • 66,653
  • 10
  • 83
  • 219
  • The file has multiple characters. Please see comments on OPs question. – Ben Crowhurst Jan 14 '14 at 12:05
  • 2
    I know it has, exactly 2. @Corvusoft What do you want to point me to, please? – alk Jan 14 '14 at 12:07
  • I do not understand what you want to express with your 1st comment. And if you downvoted my answer, you might like to explain why. @Corvusoft – alk Jan 14 '14 at 12:13
0

You have misinterpreted the ftell & fseek functions. Less coffee more rest :p

ftell manpage

long ftell(FILE *stream);

The ftell() function obtains the current value of the file position indicator for the stream pointed to by stream.

fseek manpage

int fseek(FILE *stream, long offset, int whence);

The fseek() function sets the file position indicator for the stream pointed to by stream. The new position, measured in bytes, is obtained by adding offset bytes to the position spec‐ ified by whence. If whence is set to SEEK_SET, SEEK_CUR, or SEEK_END, the offset is relative to the start of the file, the current position indicator, or end-of-file, respectively. A successful call to the fseek() function clears the end-of-file indicator for the stream and undoes any effects of the ungetc(3) function on the same stream.

Create sample.txt

echo -n 'q' > sample.txt

File Seek Example

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

int main( int argc, char** argv )
{
    FILE* file = fopen( "sample.txt", "r" );

    if( NULL == file )
    {
        perror("Failed to open file");
        return EXIT_FAILURE;
    }

    printf("File successfully opened\n");

    long position = ftell( file );

    printf( "Position before seek: %lu\n", position );

    int status = fseek( file, 1L, SEEK_SET );

    if( 0 != status )
    {
         perror("Failed to seek");
         return EXIT_FAILURE;
    }

    printf("File seek successful\n");

    position = ftell( file );

    printf( "Position after seek: %lu\n", position );

    return EXIT_SUCCESS;
}

File Size Example

#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>

int main( int argc, char** argv )
{
    struct stat file_status = { 0 };

    int status = stat( "sample.txt", &file_status );

    if ( 0 != status )
    {
        perror("Failed to read file status");
        return EXIT_FAILURE;
    }

    printf( "File size: %li\n", file_status.st_size );

    return EXIT_SUCCESS;
}

Build

gcc -o example example.c

Ben Crowhurst
  • 7,042
  • 5
  • 40
  • 72