C++ - System findstr "FINDSTR Cannot open..." - c++

I'm trying to use a system call to print out my IP address, instead of calling a batch script. My current code is:
#include <iostream>
#include <string>
void main(){
system("#echo off");
system("ipconfig | findstr /R /C:'IPv4 Address'");
system("PAUSE");
return;
}
However when ran I get this error "FINDSTR: Cannot open Address'". What am I doing wrong?

Related

ACE_OS:execlp get result - stdout

After several hours of googling I'm still failing to understand how to obtain result from ACE_OS::execlp command. Here I need to obtain not the status itself but the output result. For instance if I call some bash script and it produces its stdout/stderr.
Can anybody help me how to obtain it?
Thank you!
I am afraid this function seems not implemented: according to the github (https://github.com/DOCGroup/ACE_TAO/blob/master/ACE/ace/OS_NS_unistd.cpp)
and the code :
int
ACE_OS::execlp (const char * /* file */, const char * /* arg0 */, ...)
{
ACE_OS_TRACE ("ACE_OS::execlp");
ACE_NOTSUP_RETURN (-1);
// Need to write this code.
// ACE_OSCALL_RETURN (::execvp (file, argv), int, -1);
}
Alternatively you could use the <cstdlib> (if supported by your compiler chain) and a code like :
#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();
}
as seen at http://en.cppreference.com/w/cpp/utility/program/system

Updating a variable from a file in loop

Sorry for my grammar\spelling. I'm Romanian.
I am using Code::Blocks to edit in C++ and Notepad to edit in Batch.
I can't find a better title for this.
I'm trying to use this program:
#include <iostream>
#include <fstream>
#include "windows.h"
#include <string>
#include "G:\other.h"
using namespace std;
char message[500],from[20];
int interm;
int main()
{
ofstream wait1("errorlevel.f"); wait1<<0; wait1.close();
fstream wait2("errorlevel.f",ios::in); wait2>>interm;
cout<<"Another window is waiting for you...";
system("start options_load-new.bat");
while (interm==0) { wait2>>interm; Sleep(10); }
st();
//and program continues...
}
where st(); function is declared in other.h as
void st()
{
system("cls");
}
and the options_load-new.bat file is this:
#echo off
echo What do you want to do?
echo [L] Load a pre-made message
echo [N] Make a new one
choice /c ln
echo %errorlevel% > errorlevel.f
exit
but -
When I run the program, is opens, starts the .bat file, that file sets errorlevel.f to 1 or 2, and closes. After that, the main program remains to Another windws is waiting for you... without any action.
What should I do to make the main program continue his operations?

Executing bash script from C++ and read its output ubuntu sdk

I have a ubuntu application and I'm trying to execute bash scripts from it but it doesn't seem to be working. I tried doing this with system()
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
// tried both
system("./script.sh");
// system ("script.sh")
}
Also, i've tried researching this but did not find a solution; is it possible to also read the output and display in textbox.
Use popen().
FILE *script;
char line[LINESIZE];
script = popen("./script.sh", "r");
while (fgets(line, sizeof(line), script)) {
...
}
pclose(script);
It's not relevant that you're running a script. This will work with any shell command.
For anyone looking to do this in QT, here's what i did:
QProcess proc;
proc.start("gnome-terminal", QIODevice::ReadWrite);
if (proc.waitForStarted() == false) {
qDebug() << "Error starting terminal process";
qDebug() << proc.errorString();
return (-1);
}

System command failing on Linux C++

In my program I am copying an executable file from one location to another, and then execute the copied file. When the copied file is executed I get a "permission denied" error. But if I restart my program then the file gets executed without a problem. Can someone please help me with the problem? The code below is simple, but demonstrates the problem.
void copyFile(string _from, string _to)
{
std::ifstream src(_from.c_str());
std::ofstream dst(_to.c_str());
dst << src.rdbuf();
}
int main()
{
string original("./exe_file");
string dest_file("./exe_dir/exefile");
system("./exe_dir/exefile"); //Fails on first run because exe_dir does not exist.
//mkdir and copy the file.
mkdir("./exe_dir",S_IRWXO | S_IRWXU | S_IRWXG);
copyFile(original, dest_file);
//Open the file and close it again to flush the attribute cache.
int fd = open(dest_file.c_str(),O_RDONLY);
close(fd);
//The line below fails with system error code 2 (Permission denied) on exefile.
return system("./exe_dir/exefile");
{
I used 'chmod 777 exe_file' on the original file before executing the program, and after running this program the destination also has the same access rights. I can execute it manually just fine. And every subsequent run of the program is successful. Why does it fail on the first run?
You should close file you've created.
See cplusplus.com: std::ifstream::close
Coderz, no idea what problems you are experiencing with your IDE but this works fine for me.
#include <iostream>
#include <fstream>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <cstdlib>
using namespace std;
void copyFile(string _from, string _to)
{
std::ifstream src(_from.c_str());
std::ofstream dst(_to.c_str());
dst << src.rdbuf();
}
int main()
{
string original("./exe_file");
string dest_file("./exe_dir/exefile");
system("./exe_dir/exefile");
if (mkdir("./exe_dir", S_IRWXO | S_IRWXU | S_IRWXG))
perror("mkdir");
copyFile(original, dest_file);
if (chmod("./exe_dir/exefile", S_IRWXU | S_IRWXG | S_IRWXO) == -1)
perror("chmod");
return system("./exe_dir/exefile");
}
Note that exe_file is a simple Hello World binary and the results are
sh: 1: ./exe_dir/exefile: not found
Hello World
where the copied file is
-rwxrwxrwx 1 duck duck 18969 May 9 19:51 exefile
within directory
drwxrwxr-x 2 duck duck 4096 May 9 19:51 exe_dir

Redirection in Linux with dup2() and create() inside a loop

I am running the code below and I cannot redirect to a file. The file is made, but nothing is put into it. If I remove the last dup2(saveout,1) statement, I can create and write into the file, but I cannot get back to the terminal, which is important. As soon as I put the dup2(saveout,1) back in my code, the redirection stops working, but I can get back to the terminal. I do not understand why this is happening. I would like to redirect and go back into the terminal.
main.cpp
#include <cstdlib>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string>
#include <iostream>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
using namespace std;
void printmessage() {
printf("this is the message\n");
}
int main(int argc, char** argv) {
int saveout;
int fd;
saveout = dup(1);
for (int i = 0; i < 10; i++) {
fd = creat("/home/carl/example.txt",O_CREAT|O_APPEND);
dup2(fd, 1);
close(fd);
printf("Testing the message");
printmessage();
dup2(saveout,1);
close(saveout);
}
return 0;
}
This is a file rights issue, you should read the man pages of the functions you are using.
creat() takes as first argument the filename, and as second the file creation rights, not its opening mode.
The creat() functions is a simple open() call, with some particular flags, so that you'll just have to set up the rights.
if you want to open your file, and create it if he doesn't exists, use
open(filename, O_CREAT | O_RDWR | O_APPEND, 0600) for example, or
creat(filename, 0600),
which is mostly its equivalent, but you wont be able to append text, as "creat() is equivalent to open() with flags equal to O_CREAT | O_WRONLY | O_TRUNC"
The second dup2(saveout,1); will fail because you closed saveout.
printf is buffered by default. (line-by-line for output to a tty, perhaps differently for output to something else). Before both your calls to dup2(..., 1), you should flush with fflush:
fflush(stdout);