I am having an issue passing command line arguments to my program using Visual C++ Express 2010. I found the command arguments under debugging and using the following input, just the terms with white space between them. The file is in my project folder with the .c source code.
TestFile1.txt 2
The program works fine when I just statically define the char pointer under main. So at this point I'm not sure if the issue is with 2010 or the code. I haven't figured out a way to compile and execute in some other way to test command line args. It would be great if someone could compile this an see if it works on their system.
#include <stdio.h>
#include <stdlib.h>
#define BUFFER_SIZE 256
int main(char *argv[])
{
//char *argv[] = { "program", "TestFile1.txt", "2" };
char buf[BUFFER_SIZE];
FILE *inFp;
printf("%s",argv[1]);
if ((inFp = fopen (argv[1], "r")) == NULL)
{
fprintf(stderr, "Can't open file\n");
exit(EXIT_FAILURE);
}
fclose(inFp);
return 0;
}
it should be int main(int argc, char *argv[]) Other than that, I have not seen any other problem with your program.
Related
I have the following code in which I'm executing another program within a program in C using execve from unistd.h:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc, char** argv) {
// char *env_init[] = {"USER=unknown", "PATH=/tmp", NULL};
char* username = argv[1];
char* program = argv[2];
printf("username: %s\n", username);
printf("program: %s\n", program);
if (execvp(program, argv + 2) < 0)
printf("error");
return 0;
}
and it runs fine. But when I try to put in the environment in the program and run the program using execvpe like so:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc, char** argv) {
char *env_init[] = {"USER=unknown", "PATH=/tmp", NULL};
char* username = argv[1];
char* program = argv[2];
printf("username: %s\n", username);
printf("program: %s\n", program);
if (execvpe(program, argv + 2, env_init) < 0)
printf("error");
return 0;
}
I get the following error:
runas.c:18:7: error: implicit declaration of function 'execvpe' is invalid in C99
[-Werror,-Wimplicit-function-declaration]
if (execvpe(program, argv + 3, env_init) < 0)
How do I fix this?
As you can read in the man page for execvpe:
The execvpe() function is a GNU extension
It's not a POSIX function and macOS doesn't necessarily support it, nor is it portable.
If you run man execvpe you will get the message:
No manual entry for execvpe
This indicates that macOS (at least on my machine) doesn't support this extension.
You might get it by using an alternative libc to the one used natively, but it's better to simply use one of the (portable) POSIX functions.
Good Luck!
P.S.
FYI: macOS supports any of the following: execl, execle, execlp, execv, execvp, execvP
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have the image, txt file without extension. They are,
flower (image)
simple (txt)
I need to find file format for the above files using c.
currently i am working in linux.
Any one help me.
/* This program used to find the file format for without extention file */
#include <stdio.h>
#include <error.h>
#include <errno.h>
int main(int argc, char *argv[])
{
FILE *fp;
struct image image_bmp;
if (argc != 2) {
perror("Error in commandline argument\n");
exit(1);
}
fp = fopen(argv[1], "r");
if (fp == NULL)
error(1, errno, "Error in file open\n");
/* i am try to display argv[1] file format for without extention file
Ex: argv[1] is a txt file
argv[2] is a image file
*/
fclose(fp);
error(1, errno, "Error in file close\n");
return 0;
}
i know file command is solve this problem. But i am trying using c.
You can use the libmagic library (on Ubuntu, install libmagic-dev for headers). This is the library that also backs the file command; instead of using popen to run the file command you should use this library directly.
The following code shows an example on how to detect the file type of a named file (please note that error checking is omitted from function calls other than the magic_file call:
#include <magic.h>
#include <stdio.h>
int main(void) {
// open the magic library, with MAGIC_MIME_TYPE flag
// so that the mime type of the file is returned instead
// of human readable information
magic_t m = magic_open(MAGIC_MIME_TYPE);
// load the default database
magic_load(m, NULL);
// detect the mime type of a file named `a_file_without_extension`
const char *type = magic_file(m, "a_file_without_extension");
if (type) {
printf("The recognized type is %s\n", type);
}
else {
printf("An error occurred: %s\n", magic_error(m));
}
// close the database; in a long-running program you wouldn't
// need to close the handle all the time.
magic_close(m);
}
Prints out the mime type of the file named a_file_without_extension; e.g. say:
The recognized type is image/png
You can use popen() to run the file command, e.g.:
#include <stdio.h>
#include <limits.h>
int main(int argc, char *argv[])
{
char cmd[PATH_MAX];
char result[PATH_MAX];
int i;
for (i = 1; i < argc; ++i)
{
FILE *fp = NULL;
sprintf(cmd, "file '%s'", argv[i]);
fp = popen(cmd, "r");
if (fp == NULL)
{
perror("popen");
}
else
{
fgets(result, PATH_MAX, fp);
pclose(fp);
fputs(result, stdout);
}
}
return 0;
}
This works on pretty much any *nix-like OS (Linux, Mac OS X, FreeBSD, Solaris, etc).
Compile and run:
$ gcc -Wall popen_file.c
$ ./a.out a.out
a.out: Mach-O 64-bit executable x86_64
$ ./a.out foo
foo: directory
$ ./a.out chart
chart: PNG image data, 1249 x 961, 8-bit/color RGBA, non-interlaced
#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.
I'm trying to write a program that will read the first character in a text file. If I use ./a.out myfile.txt it works as intended but if I use ./a.out <myfile.txt I get Segmentation fault: 11. The reason why I'm trying to include the <is because this what is in the spec of the assignment. The below code is just a simplified example that i've made that has the same issue:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int func(int argc, char **argv){
FILE *fp;
int test = 0;
fp = fopen(argv[1], "r");
fscanf(fp, "%i", &test);
printf( "current file: %s \n", argv[1]);
}
int main(int argc, char **argv){
func(argc, argv);
}
Is there any way I can get it to accept the argument as <myfile.txt?
No, nor should you try. Files redirected this way will appear at stdin and you should use that instead (hint: check argc).
If you want to use a file if specified, but otherwise stdin, use something like:
if (argc > 1)
fp = fopen(argv[1], "r");
else
fp = stdin;
In your command ./a.out <myfile you redirect stdin to myfile. This means reading from stdin is actually reading from myfile. So, in this case your argc == 1, so argv[1] you use to open is NULL (see main spec on its arguments). fopen crashes when uses NULL name.
You may do your utility in another way: always read stdin. When you need file to input do like this: cat myfile | ./a.out. This is very nice approach and worth considering.
I have a written a C program using Visual Studio 2008. The program compares to files in binary mode and tells us if the files are same or different.
I need to execute this program on command line and need to pass 2 arguments along with it.
the first argument is for the file to be compared and 2nd is the file to which it will be compared.
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv){
int result_code;
char command_line[256];
sprintf(command_line, "FC /B %s %s > NUL:", argv[1], argv[2]);
result_code=system(command_line);
printf("%s file.\n", result_code ? "different" : "same");
return 0;
}
See this.
http://www.cprogramming.com/tutorial/print/lesson14.html
you can get plenty more from google.