Solved

Getting the file size

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

830 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