How can I run .exe file from C? - c

I have created wit_func.exe using Matlab. Now I am trying to run the program using C.
I have written the following code:
#include <stdio.h>
#include<stdlib.h>
int main(int argc, char **argv)
{
int r;
r=system("C:\\Users\\mails\\OneDrive\\Documents\\MATLAB\\Testing for the executable files\\wit_func.exe");
printf("%d",r);
getchar();
return 0;
}
I provided the path to that .exe file. I getting this following output:
'C:\Users\mails\OneDrive\Documents\MATLAB\Testing' is not recognized as an internal or external command,
operable program or batch file.
1
I also need to pass some integer arguments to this executable file. Can somebody help me run this program properly?

Related

Execute hello.c file by using file handlers in C

I'm trying my luck with C lately and I came across to this question where I'm stuck.
I've a hello.c file
CODE 1
#include<stdio.h>
#include<stdlib.h>
int main(){
printf("Hello World");
return 0;
}
I open this file and display the content using the following C program (CODE 2)
CODE 2
#include<fcntl.h>
#include<stdio.h>
int main() {
FILE *fd;
char ch;
fd = fopen("/home/hello.c","r");
if( fd != NULL ) {
while((ch = getc( fd )) != EOF){
putchar(ch);
}
}
return 0;
}
However, I want the output of this code to be Hello World, i.e output of the hello.c file which is read.
How can that be done?
In order to run a c file, first you need to compile it into machine code then execute it.
To compile it: run gcc source-file -o executable-file
To run, execute: executable-file
In order to to the same things in C, use system() function from <stdlib.h>
const char* tempFile = "./tempfile";
const char* sourceFile = "hello.c";
const char compileCommand[255];
sprintf(compileCommand, "gcc %s -o %s", sourceFile, tempFile);
system(compileCommand);
system(tempFile);
This code hasn't been tested.
Currently, in the second program, you are reading hello.c file. So the output of CODE2 will be the contents of hello.c. i.e. #include<stdio.h>...
For what you need, in CODE1, you need to write the output of the program into a separate file (say a.txt) and then read a.txt in CODE2.
Hope this is a sufficient hint for you to solve further.
Your "CODE 2" would have to invoke a C-compiler to compile "CODE 1" and then run it using system() or a function provided by your operating system.
BTW: It is either int main(void) or int main(int argc, char** argv), NOT int main().
As general solution, you may try also to have a look to a C interpreter, like Cling, and try to include it in your project.

Using the system function to run another .cpp file [duplicate]

This question already has an answer here:
Compiling a source file using system(), 'main referenced from implicit entry/start for main executable [closed]
(1 answer)
Closed 6 years ago.
This program is a'grader' program, where I simply request the user to enter the name of a txt file and a .cpp source file which processes the txt file and gets its info. I then compile the source file along with the txt file, which outputs another text file. This new textile is then compared to the output expected(which I have been given as well.).
The system function allows users to run UNIX commands from a C program. When I am trying to compile the source file the user provides
I am getting an error saying that
"_main", referenced from: implicit entry/start for main executable.
clang: error: linker command failed with exit code 1 (use -v to see invocation)
sh: ./myProg: No such file or directory
The source file that I am compiling provided by my professor has one function which looks like this :
#include <stdio.h>
#include <stdlib.h>
#define MAX_VALUES 3
#define OUTPUT_LINES 5
int notmain(int argc, char **argv)
{
/*
* argv is just the file name
*/
//printf(argv[1]);
int values[MAX_VALUES];
int i, j;
FILE *inputFile;
char name [20]="input.txt"; // I have included this piece of code to see if there is a correct output from the source file provided by the user.
if ( (inputFile = fopen(name, "r") ) == NULL) {
printf("Error opening input file.\n\n");
exit(1);
}
for(i = 0; i < MAX_VALUES; i++)
fscanf(inputFile, "%d", &values[i]);
for(i = 0; i < OUTPUT_LINES; i++){
for (j=0; j < MAX_VALUES; j++)
printf("%d ", values[j]*(i+1) + j);
printf("\n");
}
return 0;
}
The code that I have written can be seen below: This code takes the information from the user and then compiles it.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NUM_LINES 5
int main(){
char srcfile[200];
char inpfile[200];
char resultfile[200];
printf("Please enter the name of the source file: \n");
scanf("%s",srcfile);
printf("Please enter the name of the input file: \n");
scanf("%s",inpfile);
printf("Please enter the name of the expected result file: \n");
scanf("%s",resultfile);
char test1 [100]="gcc -o myProg ";
char test2 [100]="./myProg ";
strcat(test2,inpfile);
strcat(test2," > ");
strcat(test2,resultfile);
strcat(test1,srcfile);
printf("%s\n",test1); //these are just tests
printf("%s",test2); //these are just tests
if (system(test1)) {
printf("There is an error compiling the program ");
}
if (system(test2)!= 0) {
printf("There is an error running the executable");
}
return 0;
}
If you are looking for the solution I have posted it in the answers
The file you're trying to compile doesn't have a main function, which is the entry point for C programs. This means that it's actually not possible to build that file alone into an executable.
If the name of the function is supposed to be notmain then you'll have to write another source file that has a main function and calls notmain. This second main would belong to the executable your program is compiling, not to your program. You would have three source files:
Your grader program, which handles compilation.
A sort of wrapper file that effectively does:
int main(int argc, char *argv[]) {
notmain(argc, argv);
}
And finally the program to be graded.
You'll also either need to extern the notmain function or provide a header to share it. Your grader program would then compile the wrapper main and the source file to be graded together.
The question: Can you run two c programs with 2 main functions? The answer: Yes . In order to do this you have to use the terminal to compile the program with the two main functions separately. However if they interact with one another Im afraid I don't have a solution to that Now in this specific case here is how I did it. I went to the terminal and wrote. In this case I run one program which runs another program using the system function
gcc -c main.c (this compiles the main function).
Then after that i wrote gcc -o Myprogram main.o
This will create an executable named Myprogram which you can run by writing
./Myprogram
In this case my main method is compiling another source file so I don't need to compile that program as well in the terminal. When i compiled this program it created an output.txt file in the same directory the executable and the source files are in.

Code::Blocks command line argument issues

Using:
Code::Blocks Software
Teach Yourself C book
None of "command line argument" example programs work. They either crash or execute with all variables with 0 value or show similar results to the program below.
#include <stdio.h>
int main(int argc, char *argv[])
{
return 0;
}
The simplest of the example program is below.
#include <stdio.h>
int main(int argc, char *argv[])
{
int i;
for(i=1; i<argc; i++) printf("%s ", arv[i]);
return 0;
}
With a bit of googling I have found that I need to have a Project as a Console Application and then use Project -> set programs arguments, but I have no idea of what to do in the window that pops up.
If you have compiled your project as a console application, you can pass arguments by calling the program from the console (cmd.exe in Windows, terminal in Linux).
The window Project -> set programs arguments simply asks you what arguments do you want to pass to the program when you run the program from Code::Blocks (using the green arrow).
Simply add your arguments in the "Program arguments" text box.

MSVS command line arguments

#include "hmap.h"
int main(char* argv[], int argc)
{
printf("%s", argv[0]); <---- fails here
system("pause");
fileOpen(argv[1]);
return 0;
}
I am using MSVS 2012. I'm wondering if I'm using the command line arguments wrong. The text file is in the same folder. All my header file has is the #include libraries I will using, some #define's I'll be using, and extern function prototypes.
When I run the program it says "expand.exe has stopped working...."
I usually program in a Linux environment using GCC but I'm trying to learn MSVS environment. Getting a little frustrated on how much of a hassle to input command line arguments :.
I think the arguments for main() are around the wrong way.
That is, the first argument should be the argument count (argv), and the second one the argument vector (argv).
int main(int argc, char* argv[]) {}
It fails because a subscript should be used only with an array or pointer.

How to control the execution flow of c program through a config file. This file is passed using redirection operator (<)

I have a c program running. I want to make the program sleep for certain period say 5 sec. I want this sleep to be induced from a text file containing command "sleep(5)". I want to pass this file through redirection operator (<) while executing the program
say ./a.out < samplefile.txt
This samplefile.txt contains sleep(5) command in it. I tried the above scenario but the c program is reading it as stream of characters like s,l,e,e,p which is not our intention.
could some one please help me on this issue.
You may make your program read the commands from the text file, parse them and behave as the commands say.
Instead of parsing, you may use the environment variables.
In your program, call to getenv in any place requires configuration. When calling to the program, make sure the environment is set with the required variables, or use this notation:
VAR1=VALUE1 VAR2=VALUE2 ./a.out
I'm not very clear what you want to achieve, but this may be a prompt.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
char buf[512];
scanf("%s", buf);
if (!strcmp(buf, "sleep(5)")) {
printf("sleep...\n");
sleep(5);
}
printf("over\n");
return 0;
}

Resources