Solved

Getting the file size

Posted on 2006-11-22
4
217 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Using Delphi code in a Builder project that uses a DLL 3 110
Converting to C# also VS2015 5 117
Need some help with listview limit text 20 98
returning a dereferenced pts in C++ 10 141
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

910 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

21 Experts available now in Live!

Get 1:1 Help Now