Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Getting the file size

Posted on 2006-11-22
4
Medium Priority
?
223 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 22

Accepted Solution

by:
grg99 earned 225 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

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…
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 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.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

721 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