Batch file launched in an exe doesn't work - c++

Ok so I have an executable which launches my batch file and the batch file then should create an ODBC data source to my SQL server. If I run the batch file by itself it runs perfectly and creates the data source, but when I run it through my exe it runs, returns no errors but doesn't create the data source.
my entire exe code is
#include <windows.h>
#include <iostream>
#include <dos.h>
#include <conio.h>
using namespace std;
int main ( int argc, char *argv[] )
{
cout << system("find2.bat");
system("PAUSE");
return 0;
}
and my batch command is
ODBCCONF.exe CONFIGSYSDSN "SQL Server" "DSN=test_DSN | Description=test Data Source | SERVER=(local) | Trusted_Connection=Yes"
I also tried to output any errors from the batch file but it just created a blank text file so I guess no errors.
So does anybody have any ideas what could be causing this?

Are you sure the batch file is running? Since you're not building an absolute path, I would guess it's not being found.

You need to modify the call according to this:
system("cmd /c C:\\path\\to\\find2.bat");

Related

Exe file not opening

I am trying to learn C++ now and created a Hello World program. When I compile it on Linux using g++ and it works perfectly fine. When I compile it on Windows using the Build tools, it still compiles the code into machine code, but I can't open the executable. I used the Microsoft build tools as a compiler. The code was:
#include <iostream>
using namespace std;
int main() {
cout << "Hello, World!";
}
The output should be: Hello, World!
**Question already answered:
The program closes because it is not run in cmd. To prevent the program from crashing add
```system("pause");```
at the end**
The executable is showing the correct output on the terminal, but that terminal closes that fast that you don't even realise it.
I'd advise you to open a command prompt, go to the directory where the executable is located and launch it over there. You'll see the desired output.
it possibility because
the app closes immediately after ouputing
add system("pause");
on the end
Its not that you can't open that exe file.
Once you click on that file, it opens up and does its work and closes.
To prevent your exe file from getting closed after doing its operation, you can add following line at the end of the main function :
getchar();
And now once you open your exe file, it wont close automatically, you will need to press "enter" to close it.
#include<iostream>
using namespace std;
int main(){
cout<<"hello world";
getchar();
}

vs2008,vs2015 cannot read files: weird behaviour

I'm having a very weird behaviour in Windows 10 enviroment using both VS2008 and VS2015 when I try to read a file using ifstream.
I'm using the following simple code:
#include "stdafx.h"
#include <fstream>
#include <iostream>
#include <string>
int main()
{
std::ifstream ifs("C:\\Users\\dd\\Desktop\\test.txt");
if(ifs.is_open())
{
std::string line;
while(std::getline(ifs,line))
{
std::cout << line << "\n";
}
}
else
{
std::cout << std::strerror(errno) << "\n";
}
return 0;
}
Obviously the file exists.
When I try to read the file .txt I get:
Permission denied
I run Visual Studio as Administrator and I have the permission to read the file because I am able to open it using Notepad++.
If I change the file extension, for instance .test, I am able to read the file content correctly and everything works as expected.
I uninstalled and then reinstalled VS2008, VS2015 and all C++ Redistributable, but nothing changed. Any help is greatly appreciated!
Perhaps your file isn't named test.txt. Did you check that your file is really named test.txt and not test.txt.txt (with a hidden extension)?
Try to list files in command prompt to see what the real file name is:
cmd>
cd C:\Users\dd\Desktop\
dir | findstr test
Onother possibilities:
test.txt could possibly use similarly looking letters from another
language (in your code or in actual file name).
not properly quoted slashes in your code (e.g. C:\Users\\ instead of C:\\Users\\dd\\). Try to change to forward slashes.

Getting Environment Variable $PATH in Linux using C++ in Eclipse

I am trying to get the value of environment variable $PATH in Linux using a simple C++program as bellow:
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
int main ()
{
// get PATH using pipe
FILE* pip = popen("exec bash -c 'echo $PATH'", "r");
if (!pip)
{
printf("can not open pipe!");
return 1;
}
char lineversion[600];
memset (lineversion, 0, sizeof(lineversion));
if (!fgets(lineversion, sizeof(lineversion), pip))
{
printf("fgets error!");
return 1;
}
std::cout << lineversion << std::endl;
// get PATH using getenv
char* pPath = getenv ("PATH");
std::cout << pPath << std::endl;
}
I used two different methods: using pipe and using getenv method. They both output this:
/opt/texbin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/local/netbeans-7.3.1/bin
Interestingly the actual value of PATH is something different!
Why my C++ program is showing a different value for PATH?
Edit 1: I am running my C++ program in Eclipse IDE.
Edit 2: Compiling the program directly (without Eclipse IDE) shows the correct PATH value!
Edit 3: I found the answer in here too.
A process inherits the environment from the process that created it.
This is how Linux works, along with many other operating systems.
If you launch a program from Eclipse, that program inherits Eclipse's environment.
If you launch a program from the shell, that program inherits the shell's environment, including the modifications to PATH you have in your init files.
Since Eclipse has inherited its environment from whichever process launched it, you should see the expected output if you launch Eclipse from the shell instead of through your desktop GUI.

Execute CMD commands using C++

In my project I want to execute some CMD commands. What is the syntax for doing that using C++.
You can execute Windows Command prompt commands using a C++ function called system();. For safer standards you are recommended to use Windows specific API'S like ShellExecute or ShellExecuteEx. Here is how to run CMD command using system() function.
You should place the CMD command like shown below in the program source code:
system("CMD_COMMAND");
Here is a program which executes the DATE command in CMD to find the date:
#include <iostream>
using namespace std;
int main() {
system("DATE");
return 0;
}
Use Windows specific APIs:
ShellExecute or ShellExecuteEx
CreateProcess
See this also.
I suppose you could always do:
#include <iostream>
#include <windows.h>
using namespace
int main()
{
WinExec("cmd", 1);
return 0;
}
This however, automatically sets the path to the folder your file is in. Just type cd\ to return to base file.

ofstream outputs in debug but not run

I have a very simple C++ program with a very simple project setup, but when I run the program I get no output. If I run the program in debug mode, it works perfectly. I am using Eclipse Kepler CDT 32 bit on windows with MinGW. I am somewhat new to eclipse, so it's probably something I did wrong.
The program is:
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
ofstream outfile("testdata.txt");
int main()
{
outfile << "Program Start\n";
cout << "Program Start\n";
return 0;
}
Help!
If the problem is that the program quickly opens and then closes before you can see the output on the screen, then you can just run your program from any shell (CMD on Windows, bash on Linux, etc.). That way, it won't exit once your program ends and you can see the results.
Make sure also that you flush/close your ofstream before your program exits.
The problem is rather not releted to c++ itself. You should check if via "cmd" typying it in "launch menu" after you click start. Find the path of your program, then run it.
For the very beginning it is recommended to spend a few hours with terminal(cmd). To know how things works. After that you will be independent - you will be able to write the code in any IDE. Also simple trick to make it working is to use std::cin.get() . It is prefered to system("pause").
You open the testdata.txt file using the relative path.
And the created file may be created in the project binary output path, where the executable located in.
You can use everything software to check whether a file is created and its created path.
everything
For example, you can type your output file name testdata.txt into everything software to see where output file created.And check if the testdata.txt created in a wrong path or directory.