Go Premium for a chance to win a PS4. Enter to Win

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 59508
  • Last Modified:

File Size Function / Reading txt File Problem

Dear All,

I'm trying to find out file size for my input text files.Here are steps that I followed.

1. A text file at location c:\temp\input_files.txt , this file has list of files for which I've to find file size.
    List includes -   c:\temp\test1.txt
Logic Followed -

1. I open file c:\temp\input_files.txt
2. Read first line i.e file for which I want file size.
3. Store this file in an array
4. Pass this file to function which returns File Size.
5. Output file size.
Problem -

My Program always returns file size only for last line in input_files.txt, for rest of them it returns 0, but if I pass in file name having "C:\temp\test1.txt"
format, instead of reading it from input file, it returns correct value.

thanks & regards

Code -

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <ctype.h>
#include <malloc.h>

#define INPUT_FIL "C:\\temp\\input_files.txt"

long get_filesize(char *FileName);

void main()

      int j=0;
      char inp_file[256];
      char my_file[50][256];
      FILE *fp;
      fp = fopen(INPUT_FIL,"r");

      while(fgets(inp_file,256,fp) != NULL)

             long file_size1 = get_filesize(my_file[0]);
             long file_size2 = get_filesize(my_file[1]);
      long file_size3 = get_filesize(my_file[2]);


long get_filesize(char *FileName)
    struct stat file;
         return file.st_size;

      return 0;
1 Solution
fgets() reads the entire line, including the newline character into the buffer.
stat() is failing because the filename passed contains the newline character,

while(fgets(inp_file,sizeof(inp_file),fp) != NULL)
  int len = strlen(inp_file)-1;
  if (inp_file[len] == '\n')
    (inp_file[len] = '\0';
fgets  appends the  new line character to the string it reads .To resolve this problem find the new line character in the string and replace it
by null


char * line ,*temp;
// fgets the line
temp = strchr(line,'\n');
if(temp)  *temp = '\0';
To add something more

U should use getline() instead of fgets() because of the following reason

Here's the help of fgets()

Function: char * fgets (char *s, int count, FILE *stream)

The fgets function reads characters from the stream stream up to and including a newline character and stores them in the string s, adding a null character to mark the end of the string.  You must supply count characters worth of space in s, but the number of characters read is at most count - 1.  The extra character space is used to hold the null character at the end of the string.

If the system is already at end of file when you call fgets, then the contents of the array s are unchanged and a null pointer is returned.  A null pointer is also returned if a read error occurs.  Otherwise, the return value is the pointer s.

If the input data has a null character, you can't tell.  So don't use fgets unless you know
the data cannot contain a null.  Don't use it to read files edited by the user because, if the user inserts a null character, you should either handle it properly or print a clear error message.  We recommend using getline instead of fgets.

getline stores the newline as well as the terminating NULL character in the buffer.  getline () also takes care of if your target buffer is small i.e. If the char * you are using is not allocated the amount of memory required, realloc is used to add the extra amount

When getline is successful, it returns the number of characters read (including the newline, but not including the terminating null).  This value enables you to distinguish null characters that are part of the line from the null character inserted as a terminator.

Function: ssize_t getline (char **lineptr, size_t *n, FILE *stream)
If you set *lineptr to a null pointer, and *n to zero, before the call, then getline allocates the initial buffer for you by calling malloc.

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

I prefer to use fgets() over getline() because fgets() is in the ANSI-C spec and is portable.
For this particular application, fgets() is more than adequate. In nearly all operating systems,
filenames cannot have embedded NUL bytes*.  The maximum filename size is also defined by
FILENAME_MAX in stdio.h, so changing char inp_file[256] to char inp_file[FILENAME_MAX+2]
should solve the input buffer too small problem.

* There are two instances where I have encountered embedded NUL bytes in filenames:
1) UNICODE filename support - although neither fgets, nor getline will handle that unless UTF-8
encoding is used.
2) Classic Mac OS on HFS filesystems.  I have seen cases where third party apps use a leading
NUL byte in a filename to create a hidden file.  This worked because the Mac OS Toolbox used
Pascal string syntax, which allows embedded NULs.  That stuff wreaked havoc with Mac OS X.
Hi s_more,

The problem is that fgets is obtaining a character 10 (carriage return) after the name of your files. Drop this character, as below:

   while(fgets(inp_file,256,fp) != NULL)


        my_file[0][strlen(my_file[0])-1] = 0;
        my_file[1][strlen(my_file[1])-1] = 0;
        my_file[2][strlen(my_file[2])-1] = 0;

        long file_size1 = get_filesize(my_file[0]);
        long file_size2 = get_filesize(my_file[1]);
        long file_size3 = get_filesize(my_file[2]);

       printf("file %s size %d\n", my_file[0], file_size1);
       printf("file %s size %d\n", my_file[1], file_size2);
       printf("file %s size %d\n", my_file[2], file_size3);

and also, change the printf part as above.



some feedback??

s_moreAuthor Commented:
Dear All,

Thankyou for your replies,it did help me solve the problem.


Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now