Solved

Getting the file size

Posted on 2006-11-22
4
219 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
ID: 17996829
use stat() or fstat() or similar.
0
 
LVL 9

Expert Comment

by:DrAske
ID: 17997353

#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
ID: 17997992
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
ID: 18019247
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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
how to understand recursion 12 226
PDF library for Delphi 2 121
FMX StringGrid1->Canvas->FillRect Problem 3 153
show out valin of json for debugging in visaul c++ 1 33
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

803 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