c++ using system command problem - c++

hi every body i have a strange problem i write a code in c++ complied it successfully and run it successfully. i compiled with following command
g++ 1.c -o abc
to run program i use ./abc
now my problem is that i write a another code in c++ like
#include <fstream>
#include<iostream>
using namespace std;
int main()
{
ofstream SaveFile("/home/hadoop/project/hadoop-0.20.0/conf/core-site2.xml");
SaveFile <<"<configuration>";
SaveFile<<endl;
SaveFile<<"<property>";
SaveFile<<endl;
savefile.close();
return 0;
}
now i want to run abc in this code how to do this ?
how to use or run abc in this file?
how to use ./abc in this program ?

Actually, your question title ("... using system ...") says it all. Use:
system ("./abc");
to run the ./abc program.
There are other ways to run programs from within a program (which usually depend on platform-specific features) but this is the most portable.
A full sample program, testprog.cpp, to show what I mean:
#include <cstdlib>
int main (void) {
std::system ("ls -ald m*");
return 0;
}
Compiling this with:
g++ -Wall -Wextra -pedantic -o testprog testprog.cpp
and running the resultant executable testprog, this outputs (on my Ubuntu 10.04 box):
drwxr-xr-x 2 pax pax 4096 2010-12-14 09:33 myfolder
In other words, it runs the ls -ald m* command from within the program itself.

Related

C++ error when using std::vector on mingw and powershell

I have come across some strage behaviour when executing a simple program that uses std::vector with powershell
#include <vector>
#include <iostream>
int main() {
auto v = std::vector<int>{};
v.push_back(0);
std::cout << "Hello, World!\n";
return 0;
}
g++ -v returns gcc version 11.2.0 (Rev10, Built by MSYS2 project)
I then compile the program with g++ main.cpp -o main.exe and i get an output executable.
When I run .\main.exe with powershell I get no console output but when i run the same executable with git bash I get Hello, World! printed to the console.
Ive tested both Powershell 7 and Windows PowerShell.
When I remove the both lines that have to do wiht std::vector it works in both shells.
When I run the executbale from 'cmd' I get the following error message

C++, Nothing in the output [duplicate]

This question already has answers here:
Why do I get no output for very simple Hello World program?
(7 answers)
Closed 6 months ago.
I am new to C++ and coded my first main.cpp but I got an error, not exactly an error, it is a logical error, I guess because I wrote the following code:
#include <iostream> // including the iostream
using namespace std; // using the std namespace
int main() { // starting the main function
cout << "Hello World!" << endl; // My favorite line of code in all the of languages
return 0; // returning 0 to stop the function execution
}
In the terminal I expected
C:\Users\DELL\Desktop> g++ main.cpp
Hello World!
C:\Users\DELL\Desktop>
But it is just showing:
C:\Users\DELL\Desktop> g++ main.cpp
C:\Users\DELL\Desktop>
No output is coming, but when I try in Code::Blocks, it is working just fine! In vscode terminal, the same problem but when I run the file, it is working again! Why is it not working? I tried it in Command Prompt, installed the Windows Terminal, and tried it in that also (keeping the terminal as Command Prompt, cause I don't know what PowerShell is and how to use it) but any method is not working.
Please tell me what to do, I am new to c++ and know only some things amount it.
The command g++ main.cpp creates the file a.out or a.exe. After that file is created you need to run it by the command ./a.out on Linux and Mac or a.exe on Windows.
It's pretty simple, after g++ "file.cpp", you get an output file in your current working directory, which is the executable. C++ is compiled, not interpreted.
That output file is the executable, which by default will be "a.out", with gcc/g++. You can use the option "-o" to specify the output directory.
g++ main.cpp will compile the file and produce a.exe, you just need to type a.exe in the terminal and it will print Hello World!.

C++ code run in debug console rather in terminal in vs-code

I wanted to execute code in terminal not in debugger
After downloading all the c/c++ extension in vs-code.
code
#include<iostream>
using namespace std;
int main()
{ int UserInputOccur;
cout<<"helloworld";
cin>>UserInputOccur;
cout<<UserInputOccur;
return 0;
}
All the compilation and debugging done in debug console only till
( First user input i.e When first cin>>UserInputOccur; and then the code in terminated with helloworld1020=thread-exited,id="3",group-id="i1"
If you are using Linux you can write your code in a file, such as main.cpp.
Then you can open terminal -> go to the directory of the file -> run command g++ main.cpp -o main.
Now you can run your code on terminal with command ./main

Cannot run g++ compiled program Ubuntu

I created a simple program
#include <cstdio>
int main(){
printf("Hello world!");
return 0;
}
and compiled it with CodeBlocks 13.12 on Ubuntu 16.04, successfully
I am trying to run it from bash nothing happens
What am i doing wrong ?
test is an intrinsic bash shell command, so instead of calling your executable program you're calling this one.
To call you own executable test refer to the local binary in the directory it was created. Either ./test, or ~/Programs/test/bin/Release/test.

Hello World C++

Every time I try and run the Simple Hello World program in C++, I build it and it says nothing to build for FirstProject. The code looks correct, I'm using MinGW as my compiler, etc. Every time I try to run the program, instead of printing the output, it just terminates. Anyone have a clue?
#include <iostream>
using namespace std;
int main() {
cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!
return 0;
}
try these commands:
g++ -o hello_world.exe hello_world.cpp
./hello_world.exe
MinGW works same as gcc on linux so all commands which work on linux should work on MinGW
In cygwin, the following steps should work.
Save the contents to file HelloWorld.cc.
Go to the directory where you saved the file.
Execute make HelloWorld
Execute ./HelloWorld.exe
If that doesn't work, something is really not right.
Are you running it from the command line or an IDE? Sometimes an IDE will open a terminal and close it too fast for you to see, or you'll just see a flash. Try running it from the command line so you'll be able to see if it printed anything before exiting the program.
Tried and tested using MinGW in windows 10 command prompt.
#include <iostream>
using namespace std;
int main()
{
cout<<"Hello, World!"<<endl;
system("pause");
return 0;
}
Image of command prompt output
Save the contents to file HelloWorld.cpp
Go to the directory where you saved the file.
Execute make HelloWorld
Execute ./HelloWorld.exe