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 6965483
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:04:02+00:00 2026-05-27T16:04:02+00:00

I have the following program that writes a user’s input to a file. Currently

  • 0

I have the following program that writes a user’s input to a file. Currently the file to be written to is predefined; however, I was wanting allow the user to define the file name from command prompt. What would be the best way to add this functionality? I am having trouble getting my head around how I would make the string entered by the user an argument in the fopen() function. Should I use scanf() or create another getchar() while loop store the chars in an array and then create a variable as the argument of fopen() or something?

#include <stdio.h>

int main()
{
    char c;
    FILE *fp;

    fp = fopen("file.txt", "w");

    while ((c = getchar()) != EOF)
    {
        putc(c, fp);
    }

    return 0;
}
  • 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-05-27T16:04:03+00:00Added an answer on May 27, 2026 at 4:04 pm

    That’s what the arguments to main are for:

    #include <stdio.h>
    
    int main(int argc, char **argv)
    {
        char c;
        FILE *fp;
    
        if (argc >= 2)
             fp = fopen(argv[1], "w");
        else fp = fopen("file.txt", "w");
    
        while ((c = getchar()) != EOF)
        {
            putc(c, fp);
        }
    
        return 0;
    }
    

    If you followed this, you might wonder what is in argv[0]. That’s where the program name is. Some operating system environments put the full path to the executable file there. Others put only the program name. Others still put what was typed.

    For the command ../../bin/someprogram

    on Windows, argv[0] is "C:\\Documents and Settings\\User\bin\\someprogram.exe"
    on Linux/bash, argv[0] is ../../bin/someprogram
    on Ultrix/csh, (I think) argv[0] is /home/username/bin/someprogram

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a program that looks like the following: double[4][4] startMatrix; double[4][4] inverseMatrix; initialize(startMatrix)
I have a program that makes use of the following method to get a
I have a python program that reads floating point values using the following regular
Assume that we have the following code: class Program { static volatile bool flag1;
I'm needing to write a program (C#) that will allow the user to create
I have a code that creates file(s) in user-specified directory. User can point to
I have the following program: ~/test> cat test.cc int main() { int i =
I have the following program to open lot's of sockets, and hold them open
I have the following program to browse all virtual directories and their sub directories
Hi I have the following program. When I compile on the terminal gcc main.c

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.