3

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

1
  • Your question is not clear. Can you post a complete code? filename1 is a C-string(char[] / char*). Commented Oct 21, 2012 at 22:04

4 Answers 4

6
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

Sign up to request clarification or add additional context in comments.

Comments

2

You can't do that, if you use redirection (i.e. "< 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;
}

Comments

1

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

7 Comments

Yea I tried the command line argument thing and put in the ./fileopener < filename1 but argv[1] is fileopener and not filename1
@user1763861 and what happened?
For the purpose of what im doing it HAS to have the < there
The pipe could also be passed in on the command line and processed in your code.
@user1763861 You are really asking a question about the operating system that launches programs, not about C. If you are using Unix and a standard Unix shell to launch your program, and if you have to have the < there, then your program has no way to know that it was invoked on filename1, because the shell does not tell it.
|
0

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;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.