Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8725683
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T08:05:29+00:00 2026-06-13T08:05:29+00:00

I’m new to C and I’d like to ask about running a C program

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T08:05:30+00:00Added an answer on June 13, 2026 at 8:05 am

    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;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to run a str_replace or preg_replace which looks for certain words
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I would like to count the length of a string with PHP. The string
I would like my Web page http://www.gmarks.org/math_in_e-mail.txt on my Apache 2.2.14 server to display
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I am reading a book about Javascript and jQuery and using one of the
I am trying to render a haml file in a javascript response like so:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.