So I'm pretty new to C++ and programming in general, and I'm trying to figure out how I can use code from this github program inside my own program. How do I write a function that calls the program and returns the results?
Here is the reference to std::system. With this you can run any command on a POSIX system.
#include <cstdlib>
#include <fstream>
#include <iostream>
int main()
{
std::system("ls -l >test.txt"); // execute the UNIX command "ls -l >test.txt"
std::cout << std::ifstream("test.txt").rdbuf();
}
If you need a other platform (e.g. Windows) take a look at boost process.
This is done by asking the system to create a new process, so your solution will depend on the system you are on.
You can use directly the system interfaces to create the process, or use a cross-platform third party wrapper such as Qt or boost.
Related
I want to use std::experimental::barrier in my cpp multi-threaded code. But even if I write a code like this:
#include <iostream>
#include <thread>
#include <experimental/barrier>
int main () {
return 0;
}
the compiler throws an error saying that:
experimental/barrier: No such file or directory
#include <experimental/barrier>
^`
I am using g++ version 6.3.0 on my Ubuntu machine.
This is the command I am trying:
g++ -pthread -std=c++11 top.cpp -o top_new
Currently this library is not yet available.
Maybe this will be usefull:
The GNU C++ Library Manual -> Part III. Extensions -> 30. Concurrency
The file <ext/concurrence.h> contains all the higher-level constructs for playing with threads. In contrast to the atomics layer, the concurrence layer consists largely of types. All types are defined within namespace __gnu_cxx.
...
In addition, there are two macros
_GLIBCXX_READ_MEM_BARRIER
_GLIBCXX_WRITE_MEM_BARRIER
Which expand to the appropriate write and read barrier required by the host hardware and operating system.
I want to open an additional program with c++ on XCode.
It is Firefox.
But if I make
Shell Execute("file://localhost/Applications/Firefox.app");
There is an error 'ShellExecute' was not declared in this scope
In other forum there was a clue to include windows.h and shellapi.h
#include <shellapi.h>
#include <windows.h>
but that makes other errors
shellapi.h: No such file or directory
windows.h: No such file or directory
What should I do? I want to open frefox with c++ in XCode on Mac?
Try running this in Terminal to open Firefox:
open -a Firefox http://www.ibm.com
If that does what you want, you need to wrap it in system() like this:
#include <cstdlib>
#include <fstream>
#include <iostream>
int main()
{
std::system("open -a Firefox");
}
ShellExecute() is only available through the Windows API. You don't have a Windows system.
You can simply use the (more portable) system() function, or one of the exec() functions available on POSIX compliant systems.
I tried the same with chrome and I had to set it in inverted commas:
int main() {
system("open -a 'Google Chrome'");
return 0;
}
with the apostrophe it worked!
I am a novice in C++ and I am struggling to make my program to wait a few minutes before executing a function.
I know there are lots of topics about it but I have a problem with my compiler. I can't seem to use the boost library nor the thread library. And since I can't use the thread library, I can't use the chrono library either.
I am using GNU GCC Compiler. I have MinGW installed. Is it outdated or something? What is the best compiler to code in C++?
My OS is Windows.
You could use this
#include <unistd.h>
...
usleep(1000); // Time in microseconds
or
#include "stdafx.h"
#include "windows.h"
#include "iostream"
using namespace std;
int main(){
int sleepTime = 1000;
Sleep(sleepTime);
return 0;
}
<thread> is only available starting with C++11.
It's likely you don't have the proper flags to tell GCC you want to enable C++11 support, which is disabled by default.
The command line parameter is -std=c++11.
Then, you can use std::this_thread::sleep_for() for cause your program to fall asleep. Note that if you only have one thread in your program, it will probably stop responding to user actions during that time.
Can someone tell me if there is a library built in C++ that I can use to get a list of files and directories? I've looked around and I see people using dirent.h but I need to download it(i think).
Thanks
P.S. I've looked at the fstream, but thats only for reading and outputting files as far as i know.
FORGOT TO MENTION. I DO NOT WANT TO DOWNLOAD ANYTHING, I JUST WANT TO SEE IF THERE IS A LIBRARY THAT IS BUILT WITHIN C++ THAT I CAN USE STRAIGHT OFF THE BAT. THIS IS FOR WINDOWS ASWELL
How about Boost::Filesystem? Supports directory iteration and is portable.
You can use Boost Filesystem library.
http://www.boost.org/doc/libs/1_31_0/libs/filesystem/doc/index.htm
Some nice samples are also provided at the link.
EDIT:
Without downloading a 3rd party library, there is no portable way to do it. For windows you can use CFileFind class from MFC.
Using namespace std::filesystem is available in visual studio 2017 you have in #include
#include <iostream>
#include <iostream>
#include <fstream>
#include <filesystem>
#include <chrono>
#include <thread>
#include <functional>
namespace fs = std::filesystem;
void Files_in_Directory();
fs::path path_Copy_Directory = "E:\Folder";
fs::path path_Paste_Directory = "E:\Folder1";
int main()
{
Files_in_Directory()
return 0;
}
void Files_in_Directory()
{
for (const auto & entry : fs::directory_iterator(path_Copy_Directory))
{
std::cout << entry.path() << std::endl;
}
}
Since others already mentioned boost::filesystem there are also other alternatives. Almost every C++ framework has some way to list directories and files. Like wxWidgets or poco and there are many more.
Regarding dirent.h. It is a standard C Posix library so on Posix compatible systems it should be available. For Windows you can also get it here and it includes instructions on how to use it.
After your edit:
On Windows you can use things like FindFirstFile (example here) and then you don't have to download anything. But it only works on Windows. It is not build in C++.
I am trying to use windows dll functionality in Linux.
My current solution is a compilation of a separate wine application, that uses dll and transfer requests/responses between dll and main application over IPC.
This works, but is a real overhead comparing to a simple dll calls.
I see that wine-compiled program usually is a bootstrapping-script and some .so, which (according to file utility) is normal linux dynamically linked library.
Are there any way to link that .so directly to my application? Are there any manual?
You may be able to use Winelib to write a Linux app that can use Windows DLLs.
EDIT:
For future reference:
libtest.c:
#include <stdio.h>
#include <windows.h>
int main(int argc, char* argv[])
{
HMODULE h;
h = LoadLibrary("cards.dll");
printf("%d\n", h);
}
Execution:
$ winegcc -m32 libtest.c
$ ./a.out
536936448