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

  • Home
  • SEARCH
  • 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 8649869
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T13:39:50+00:00 2026-06-12T13:39:50+00:00

At the Unix command line, I need to use input redirection to specify the

  • 0

At the Unix command line, I need to use input redirection to specify the input file:

./a.out < filename

Here is my source code:

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

            char fileName[30];

            fgets(fileName, sizeof fileName, stdin);

            printf("%s\n", fileName);
    }

The program prints an integer instead of the filename. I get the same results if I use scanf("%s", fileName);

My google-fu has failed me. What am I doing wrong?

Edit: It is required to use input redirection (<, specifically), so command line arguments are not an option. However, I do not need to actually know the file name, I just need to read newline-delimited integers from the file. It appears that I am asking the wrong question.

  • 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-12T13:39:52+00:00Added an answer on June 12, 2026 at 1:39 pm

    I just need to read newline-delimited integers from the file. It appears that I am asking the wrong question.

    The shell’s input redirection operator, <, associates the named file with the standard input of your program. Your program does not need to take any special action to open the file — it is already opened. Just read from the input as if the user were typing at the keyboard:

    /* foo.c */
    #include <stdio.h>
    int main(int ac, char **av) {
      int i;
      while(scanf("%d", &i) == 1) {
        printf("Got one!: %d\n", i);
      }
      return 0;
    }
    

    Compile your program:

    cc foo.c
    

    And run it thus:

    a.out < filename
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How do I get the correct return code from a unix command line application
I need to, using a UNIX command line script, get 11 random lines from
At a unix command line, what's the difference between executing a program by simply
I've been stuck on a little unix command line problem. I have a website
How does one create a package for a Java Project through the Unix command-line?
Unix command find doesn't do what I want to do. Here is the situation
I have a C# command-line application that I need to run in windows and
I have inherited a 20-year-old interactive command-line unix application that is no longer supported
I need to wrap the Unix command tail -f in a BufferedInputStream. I don't
I have a question about unix command line. I have many files like these:

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.