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

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.

Related

Find directory space details using boost [duplicate]

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).

Text from URL to String - C++ [duplicate]

This question already has answers here:
Downloading HTTP URLs asynchronously in C++
(4 answers)
Closed 8 years ago.
What I want my program to do is to download data from URL(this will be some text, code, whatever) and save it to string in C++. How do I do this? I googled a lot and found absolutely nothing, except using some classes that exist only on Linux. I'm working on Windows so I don't accept answers such as: use curl.h . Thanks
There are some POCO libraries that make it very easy. Here is some code that reads a single line of text from a website:
#include "Poco/URIStreamOpener.h"
#include "Poco/Net/HTTPStreamFactory.h"
Poco::Net::HTTPStreamFactory::registerFactory();
Poco::URIStreamOpener& opener = Poco::URIStreamOpener::defaultOpener();
try
{
std::auto_ptr<std::istream> reader(opener.open(myURL));
reader->getline(line,sizeof(line));
}
catch(...)
{
}
And that's all there is to it. The try...catch is there in case of a bad Internet connection.

Run a command with parameter from c++ program [duplicate]

This question already has an answer here:
how to run a batch file using c++?
(1 answer)
Closed 8 years ago.
I want to run an .exe file from my c++ program.
but I also want to pipe an input file and take output of that file into another file.
I know that this can be done from command line as:
c:> my_program.exe <"input.txt"> "output.txt"
with this command, my_program takes all standard input from input.txt and gives standard output to output.txt
Now I want this should happen from my C++ program.
my my_program.exe is in D: drive. also input.txt is in D: drive.
Please tell me how can I accomplish my goal.
You need to handle input and output pipes inside your c++ program, and read/write data to files accordingly. See MSDN for example.
The question was basically how to redirect stdin and stdout from the inside of C++, which as been answered here.
Just change your directory to D:
cd D:\
D:>my_program.exe <"input.txt">"output.txt"

Execute file in another directory [duplicate]

This question already has answers here:
system("c:\\sample\\startAll.bat") cannot run because of working directory?
(4 answers)
Closed 9 years ago.
Consider the following:
I have a c++ program in C:\Documents\myProgram.exe
With this code in it:
system("start C:\\somefolder\\start.bat");
That will start the target file (start.bat) in C:\Documents\ instead of `C:\somefolder\'.
My question is, how do I execute the file in it's own directory instead of myProgram's directory?
In theory this is what I want to accomplish using c++:
cd C:\somefolder\,
start start.bat
If you're on windows anyway, use ShellExecute, you can set more things and launch even documents, links etc.
To do this you can do one of two things (that I found).
A) You can use chdir() in unistd.h; see http://pubs.opengroup.org/onlinepubs/7908799/xsh/unistd.h.html
or
B) You can use something called the File System Interface, from the GNU library, for more advanced stuff; see http://www.gnu.org/software/libc/manual/html_node/File-System-Interface.html#File-System-Interface.
Anyway, best of luck, I hope you find something that will work!

execute script with C++ [duplicate]

This question already has answers here:
How do I execute a command and get the output of the command within C++ using POSIX?
(12 answers)
How to assign shell command output to a variable in C language
(4 answers)
Closed 9 years ago.
I want to execute a script through a c++ program and get its output. Presently I am doing
system("./script.sh > out.txt");
But I need a command that get the output to a string, some thing like:
out = system("./script.sh");
printf(out);
I can't read the file out.txt after execute the script because I don't have permission to that. I deployed my c++ program at other framework (boinc) that doesn't give me this permission.
Does anybody have a hint?
Thanks in advance!
Felipe
you can use popen() and then get the output of the command from the pipe opened by popen()
FILE *fp;
fp=popen("./script.sh","r");
and to get your output. you can use fgets() or fread() to read from pipe like you read from a file