Find directory space details using boost [duplicate] - c++

This question already has answers here:
Size of a directory [duplicate]
(4 answers)
Closed 5 years ago.
I have just started exploring 'Boost' library. My main motive is to find the space details of a directory. I know this can be done using 'boost:: filesystem'.
I am trying to write platform independent code. For windows, it is working fine. And gives proper output for given inputs. But, for a Linux(ubuntu) I can see weird outputs. As I am passing input through the command line, for every input it is showing the same output. I don't know what's wrong here. Your help will be appreciated. Thanks in advance. Also look into image.
code i have written:
int main(int argc, char*argv[])
{
#ifdef BOOST_WINDOWS_API
cout<<"\n Running on windows";
#else
cout<<"\n Running on linux";
#endif
if(argc !=2 )
{
cout<<"\n Invalid inputs";
return 0;
}
fs::path p(argv[1]);
fs::space_info sp = space(p);
cout<<"\n directory capacity is: "<<sp.capacity;
cout<<"\n directory free is: "<<((sp.free;
cout<<"\n directory available is: "<<sp.available;'
cout<<"\n directory capacity is: "<<((sp.capacity/1024)/1024);
cout<<"\n directory free is: "<<((sp.free)/1024);
cout<<"\n directory available is: "<<((sp.available/1024)/1024);
}

For every input it is showing the same output as it should be, if the paths you do supply are on the same file system (same device and partition).
capacity -- total size of the filesystem, in bytes
free -- free space on the filesystem, in bytes
available -- free space available to a non-privileged process (may be equal or less than free)
If you had it different on Windows, then you either used several partitions or pointed at restricted folders.
On POSIX systems all directories on partition by default share space on that partition . If you want find how much space is used by files in that directory, you have to iterate all files in directory and summarize sizes (that's what windows' dir command or linux ls utility does).

Related

How to give more number with cin.get() in c++?

I want to get a very large number from the user and put the each individual digits of that number in rows of an array, respectively.
That's why I wrote this code in c++.
But when I running code and copy that big number and paste in windows Cmd it only receives 4094 digits and does not allow to write more numbers.
How to fix this?
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
int size=0;
int *a=NULL;
int *b=NULL;
int count=0;
char x='0';
a=new int[size];
x=cin.get(); //input by user
while(isdigit(x)!=0)
{
if(count>=size)
{
b=new int[size+1];
for(int i=0;i<size;i++)
{
b[i]=a[i];
}
delete []a;
a=b;
size++;
}
a[count++]=x-'0';
x=cin.get(); //input by user
}
cout<<size;
}
Experimentation has shown me that the Windows cmd.exe has a maximum command line length of approximately 4094 * 2. On my Windows 10 64bit machine I am able to enter a maximum of 8189 characters before it stops allowing me to enter more. This means when I enter a sequence of digits separated by spaces, the most I can possibly enter in a single prompt is 4095 individual digits.
Here's the official Microsoft documentation on the subject:
Command prompt (Cmd. exe) command-line string limitation
Which states:
On computers running Microsoft Windows XP or later, the maximum length of the string that you can use at the command prompt is 8191 characters. On computers running Microsoft Windows 2000 or Windows NT 4.0, the maximum length of the string that you can use at the command prompt is 2047 characters.
This limitation applies to the command line, individual environment variables (such as the PATH variable) that are inherited by other processes, and all environment variable expansions. If you use Command Prompt to run batch files, this limitation also applies to batch file processing.
Microsoft even offers some guidance on how to work around this.
Modify programs that require long command lines so that they use a file that contains the parameter information, and then include the name of the file in the command line.
In your case you're using cin, but the same limitation seems to hold.
What this indicates is your problem lies in the method of entry to the particular prompt. There's a limit to how much can be entered at once.
Related question: Maximum Length of Command Line String

Get the running file name: argv[0] vs boost::filesystem::current_path()

I am trying to get the current running file name in C++. I wrote a simple code that uses both argv[0] and boost current_path() method. The file is compiled into executable file mainWindow.
#include "boost/filesystem.hpp"
int main(int argc, char* argv[])
{
boost::filesystem::path full_path( boost::filesystem::current_path() );
std::cout << full_path.string() << "\n";
std::cout << argv[0] << "\n\n";
return 0;
}
Interestingly the output of each method is different.
argv[0] output is:
../VENTOS/src/loggingWindow/mainWindow
current_path() output is:
/home/mani/Desktop/VENTOS_Redpine
I am not running my program (mainWindow) directly from the terminal. Another application is calling my program and I guess that's why the output is different. Am I right on this?
My second question is: Without using the argv[0] option and relying only on the boost filesystem methods, how can I get the same result as argv[0]?
argv[0] only contains the command used to execute the the program. This may contain the path. It may contain a relative path. It may contain no path at all. It might not even contain the executable name thanks to symlinks etc.... It might even be empty if the hosting system chooses to provide nothing. It can't be trusted, so you don't want to use this as a basis for evaluating other methods.
boost::filesystem::current_path fails you because it only returns the current working directory. This may or may not be the location of the executable because it depends on the directory from which the program was run and whether or not the working directory has been changed by the program. To be honest I'm not sure if there is a sure-fire way to get the process name and path from Boost. There wasn't a few years ago, but time has a way of marching on, you know?
There are a slew of questions covering how to get the executable and path (Finding current executable's path without /proc/self/exe looks promising but is stale. That time marching thing again.) but all are platform-specific you may have to do some ifdef or linker wizardry to make this work.

Windows up one directory from current path

I have been searching for hours but cant find a solution to this as yet. Apologies it is probably really simple.
My program is using CreateDirectory to create a new directory and then set the path to it to receive a number of data files:
if (CreateDirectory(dateTime.c_str(), NULL) || ERROR_ALREADY_EXISTS == GetLastError())
{
SetCurrentDirectory(dateTime.c_str());
}
Once all the data files have been generated I would like to move back up one directory without specifying the absolute path. Something equivalent to cd.. or ../ Does anyone know the best way to do this?
One possible approach is to get the current directory (GetCurrentDirectory) before changing to a new one and once complete, then change back the desired directory; akin to a push/pop.
In the sample I've left out error checking and buffer size requirements for simplicity.
TCHAR resetDir[1024] = {};
GetCurrentDirectory(1024, resetDir);
//... Do some work, change directories etc...
// Reset the directory
SetCurrentDirectory(resetDir);
Side note: the current directory when the process is launched is not necessarily the same as the directory the process image is in (the exe path).
Relative changes can be done with a simple
SetCurrentDirectory(_T(".."));
Although basing the relative from the current directory would also work (and may be preferable);
SetCurrentDirectory((currentDir + _T("\\..")).c_str());
Internally, cd command ends using SetCurrentDirectory. So to get something equivalent to cd.. or cd ../ you can simply use:
cr = ::SetCurrentDirectory("..");
cr should be non zero if it succeded and 0 if it failed. In the latter case use GetLastError to get further information.

Getting disk label in Linux in C/C++ [duplicate]

This question already has an answer here:
Closed 11 years ago.
Possible Duplicate:
How to get drive label in Linux using C from userspace
How can I obtain label of a disk by its file name (/dev/sda1, e.g.) in a program written in C/C++?
You can code the C or C++ equivalent of this command:
find -L /dev/disk/by-label -inum $(stat -c %i /dev/sda1) -print
That is, stat() the device file you care about and remember its inode number. Iterate over all of the files in /dev/disk/by-label, and stat() each of them. When the inode number matches, then the name of the matched file is the label of that disk.
If it were me, I'd code the above algorithm in C++, using Boost.Filesystem.

How to reference files relatively in Xcode 4 (C++)?

I have a c++ project, add a text file to the project, now I can't read from it I remember in XCode 3 there was property in GetInfo of the Project node, to set the build relative to the project directory and that made it work.
But in XCode 4 I can't find such setting or find a substitute I can only reference files if they are with absolute paths.
#include <iostream>
using namespace std;
int main (int argc, const char * argv[])
{
//This works
freopen("/Volumes/MyHD/XProjects/Test/File.txt", "r", stdin);
//This doesn't
freopen("File.txt", "r", stdin);
int i;
cin>>i;cout<<i;
return 0;
}
Are you referring to the working directory the executable uses when run? If so, you can set the working directory by editing the scheme.
Choose Edit Scheme from the Schemes popup, then choose the Run action from the side bar. Working Directory controls are near the bottom. Check the "Use custom working directory" box then click the little window/folder/card-looking icon (I have no idea what symbol they're going for there) in the right side of the text field and choose your path.
To determine the working directory programmatically works the same as it always has, regardless of the IDE.
Xcode 5: menu bar > Product > Scheme > Edit Scheme...
Note: This Answer is in reply to the following comment because I don't
have enough reputation to reply to comment above:
Where is the "Scehemes Popup"? – aaronsnoswell Nov 28 '13 at 1:51