简体   繁体   中英

How to run c program and give input in same line

I'm new to C and I'd like to ask about running a C program and supplying input at the same time.

What I would like to do is run a program (ex. fileOpener) and also state which file to open

./fileOpener < filename1

I've tried it already and it works fine, but what do I use to know what filename1 is? That way I can open the file with

fp = fopen(filename1, "r")

Thanks.

Edit: OK, I'll try to explain a bit more. If there wasn't a "<" then I could just use command line arguments as I have done before, but when I tried it with the <, it didn't work

Specifically: fileOpener code:

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[]){
printf("%s", argv[1]);
}

when I use ./fileOpener < filename1 the output is ./fileOpener

I used gcc -o fileOpener fileOpener.c as the compiler

int main(int argc, char *argv[])

You can name them whatever you want, but these are the normal names.

argc is non-negative. It gives the number of useful elements in argv .

If argc is positive, argv[0] contains the program name. Then argv[1] through argv[argc - 1] point to character arrays that contain the program's command line arguments.

For example, if I run a program at the command line, such as

unzip filename.zip

argc will equal 2; and argv[0] will compare equal to "unzip" ; and argv[1] will compare equal to "filename.zip" .

Source

You can't do that, if you use redirection (ie "< filename") the file is opened by the system. You could discover the name, but it's non-portable, and anyway useless since the file is already open. Just use stdin instead of fp , and you need not use fopen() (nor fclose() ):

int main()
{
   char buffer[1024];

   // fgets() reads at most 1024 characters unless it hits a newline first
   // STDIN has been already opened by the system, and assigned to data flowing
   // in from our file ( < inputFile ).
   fgets(buffer, 1024, stdin);

   printf("The first line of input was: %s", buffer);
}

A different approach is to use arguments:

int main(int argc, char **argv)
{
   FILE *fp = NULL;
   char buffer[1024];

   if (argc != 2)
   {
       fprintf(stderr, "You need to specify one argument, and only one\n");
       fprintf(stderr, "Example: %s filename\n", argv[0]);
       // Except that argv[0], this program's name, counts.
       // So 1 argument in command line means argc = 2.
       return -1;
   }
   printf("I am %s. You wanted to open %s\n", argv[0], argv[1]);

   fp = fopen(argv[1], "r");

   fgets(buffer, 1024, stdin);

   printf("The first line of input was: %s", buffer);

   fclose(fp); fp = NULL; // paranoid check

   return 0;
}

You need setup your program to take a command line argument. Here's a good tutorial that solves your exact question:

http://www.cprogramming.com/tutorial/c/lesson14.html

A program's main function in C has two arguments:

int main(int nArgs, char *pszArgs[]) {}

That first argument tells the program how many parameters were passed onto the program when you ran it. Usually, this will just be 1, because it includes the program's name.

The second argument is a table of strings, which can be accessed thus (the program below prints the parameters given to it):

int main(int nArgs, char *pszArgs[])
{
   int i = 0;
   while (i < nArgs)
   {
      printf("param %d: %s\n", i, pszArgs[i]);
      i++;
   }
   return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM