Solved

Getting the file size

Posted on 2006-11-22
4
215 Views
Last Modified: 2010-04-01
I am trying to get the file size of every file in a list. I was using fopen and ftell, however, that only works if I have read permissions on the file. How can I get the size of a file when I don't have read permissions? he directories have read and execute permissions.

This has to be possible, since I can cd to the directory and do 'ls -l' and see the sizes manually. I do not need to open, inspect, or in any other way bypass the lack of read permission, simply get the size.

Thanks for your help
0
Comment
Question by:steveo225
4 Comments
 
LVL 22

Accepted Solution

by:
grg99 earned 75 total points
Comment Utility
use stat() or fstat() or similar.
0
 
LVL 9

Expert Comment

by:DrAske
Comment Utility

#include <stat.h>

ifstream sourcefile;
string filename = "C:\\file.txt"; // file path
sourefile.open(filename.c_str());
if(!sourefile.is_open())
  // terminate or return statment

struct stat st;

stat ( filename.c_str (), &st);

long Size = st.st_size;

regards,Ahmad;
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
Comment Utility
The sample code of Ahmad might get improved by

#include <sys/stat.h>

   struct stat st;
   if (stat("C::\\file.txt", &st) == 0)
   {
        ifstream sourcefile("C::\file.txt");
        if (!sourcefile)
            return -2;  // open error
        char* buf = new char[st.st_size+1];
        int bytesRead = sourcefile.read(buf, st.st_size);
        if (!sourcefile || bytesRead < st.st_size/2)
             return -3;    // read error

        ...
   }
   else
        return -1;  // error file doesn't exist


Note, when reading text files on Windows platform, all CRLF pairs (== 0x0D0A) in the file get turned to single linefeed chars (== 0x0A == '\n'). Because fo that, bytesRead normally is less than st.st_size (if the textfile contains 2 text lines or more).

You could omit that by opening the file in binary mode.

        ifstream sourcefile("C::\file.txt", ios::in | ios::binary);


Regards, Alex
0
 
LVL 4

Author Comment

by:steveo225
Comment Utility
thanks grg99, I did not know of that function previously. I came up with the following function that does not require the file to be opened or read from:

function fileSize(string filename) {
  struct stat s;
  if(stat(filename.c_str(), &s) != 0)
    return s.st_size;
  else
    return 0;
}
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now