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?
Related
I have tried to write a program that run in ubuntu terminal .Program will open a new gnome terminal and run command in that new terminal to open new abcd.txt using vim.And then when i Ctrl+C in the first terminal which run the program ,new gnome terminal will shut vim down and have an announcement in the first terminal
I have tried system("`gnome-terminal`<< vim abcd.txt");
and this system("vim abcd.txt>>`gnome-terminal`");
but the new one terminal cannot recieve command
My full code
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <pthread.h>
int loop=1;
void DEF()
{
system("kill -9 pidof vim");
loop=0;
}
void *subthreads(void *threadid)
{
loop=1;
long tid;
tid=(long)threadid;
system("`gnome-terminal`<< vim abcd.txt");
signal(SIGINT,DEF);
while(loop){}
pthread_exit(NULL);
}
void main()
{
int loop=1;
pthread_t threads;
int check;
long tID;
check= pthread_create(&threads,NULL,&subthreads,(void*)tID);
while(loop){}
printf("Ctrl+C is pressed!\n");
}
Not sure what you are trying to achieve in the end. But here are a few ideas, starting from your code:
The terminal command (in system()) should be something like Mark Setchell pointed out, like for example system("gnome-terminal -e vim file.txt");
The system() command is blocking further execution of your code, so the call to signal() is not happening until you terminate the system() call.
pidof is not working on my Linux system. I would use pkill <program>. Still, that would kill all running instances of , for example vim or your terminal.
You are declaring the variable loop in the global scope first and then redeclaring it in main(). If you really want to use it as a global variable, it should just be loop=1 in main().
You are not using the variable tid for anything.
Here is an improved version of your program, with additional printf calls to explain to the user what is happening. I also used xterm and nano because I don't have gnome-terminal, and I didn't want to interfere with my running instance of vim. But it still is maybe not exactly what you are trying to do. The main problem is that system("xterm -e sh &") is blocking and when you press Ctrl-C, that system call will terminate xterm so that the def() function will do nothing when it is called later.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <pthread.h>
int loop = 1;
void def()
{
printf("In def\n");
system("pkill xterm");
loop=0;
}
void *subthreads(void *threadid)
{
printf("Starting subthread\n");
loop = 1;
long tid;
tid = (long)threadid;
signal(SIGINT, def);
system("xterm -e sh -c nano &"); // Note: xterm will still exit when you press Ctrl-C
printf("Terminal exited in subthread\n");
while (loop);
printf("Exited loop in subthread\n");
pthread_exit(NULL);
}
void main()
{
pthread_t threads;
int check;
long tID;
check = pthread_create(&threads, NULL, &subthreads, (void*)tID);
printf("In main after thread creation\n");
while (loop);
printf("Ctrl+C is pressed!\n");
}
Another option is to use fork() instead of pthread to split into a separate process. (Note that processes are like separate applications while threads are processor threads in the same application.)
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
void def()
{
system("pkill nano");
printf("def(): Killed nano\n");
}
int subprocess()
{
signal(SIGINT, def);
pid_t parent_id = getpid(); // Get process ID of main process
fork(); // Fork into two identical copies of the running app.
if (getpid() != parent_id) { // The part in the if block is only done in the second process!
system("xterm -e sh -c nano &");
printf("subprocess(): system call ended in forked process\n");
exit(0);
}
}
int main()
{
subprocess();
printf("Entering while loop in main process\n");
while (1);
printf("Exited main thread\n");
}
The one flaw with this version is the same as the previous one: when Ctrl-C is pressed, xterm/nano is killed and def() will subsequently do nothing except catch any Ctrl-C done afterwards.
If you explain further what your final goal is, maybe I can give some suggestions.
Like, why do you want to start vim in a terminal from a C application and then kill vim? Do you want to kill the whole terminal or only vim?
A method to auto-update the directory of a file using C++ ?
I have a program which aims to first input the password from the user, and once the password matches, the program will open a file using the ShellExecute() function.
This file is a C++ executable file in .exe format.
The program needs to automatically update the directory in the ShellExecute() function instead of having the programmer or other users manually change it in the code each time the executable file's location is changed. What is the best approach to do so ? I have gone through some of these links, but to no avail :
[1] Finding the last created FILE in the directory, C++
[2] How do I make a file self-update (Native C++)
[3] https://www.daniweb.com/programming/software-development/threads/242600/finding-the-most-recently-created-file-in-a-folder
Please feel free to browse my code provided below :
#include<iostream>
#include<conio.h>
#include<string.h>
#include<unistd.h>
#include<windows.h>
using namespace std;
int main(void)
{
string pw="";
char ch;
int attempts=3;
Initiation:
system("cls");
cout<<"Password ?\n--> ";
ch=_getch();
while(ch!=13)
{
pw.push_back(ch);
cout<<'*';
ch=_getch();
}
if(pw=="a32bx#$123")
{
ShellExecute(NULL, "open", "C:\\Users\\agm\\Documents\\easypeasy.exe", NULL, NULL, SW_SHOWDEFAULT);
}
else if(pw!="a32bx#$123")
{
attempts--;
if(attempts>0)
{
cout<<"\n\nIncorrect password. You now have "<<attempts<<" attempts remaining. Loading back the main screen...";
sleep(3); //This gives time for the user to read the line before it moves to the label named Initiation.
goto Initiation;
}
if(attempts==0)
cout<<"\n\nAborting now !";
}
} //END OF CODE
Note : I'm also open to any suggestions on how to improve the overall code.
The selection cannot be launched, and there are no recent launches.
At the end of this windows there is an error message about PATH. Please take a look:
MAKE PATH
The Program what I tried to run:
#include <iostream>
int main ()
cout<<"Hello World!";
return 0;
}
When I click on the 'Play' button/ Run command, it keeps showing this disgusting-annoying error message.
What could be the solution?
I check another articles where they stated that it has a problem with 'main ()'
but it is not missing from my code.
Now the full code looks like this:
#include <iostream>
using namespace std;
int main ()
cout << "Hello World!";
return 0;
}
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?
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);