6

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;
}
bqui56
  • 2,091
  • 8
  • 22
  • 40

4 Answers4

10

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

wallyk
  • 56,922
  • 16
  • 83
  • 148
  • If you want to compress everything into the initializer: `FILE * const fp = fopen(argc >= 2 ? argv[1] : "file.txt", "w");` :-) – Kerrek SB Dec 24 '11 at 16:56
  • 1
    @KerrekSB: That's not an improvement. The natural development of processing `argc` is more cases to handle each argument, perhaps with case statement fall-throughs. – wallyk Dec 24 '11 at 17:09
  • Hmmm just so I understand, in your program, when is the string that is entered into command prompt actually stored in argv[1]? Do you need to type a command before it is entered in prompt? – bqui56 Dec 24 '11 at 17:33
  • @stariz77: That happens before the program starts running. – wallyk Dec 24 '11 at 19:12
  • @stariz77: ... and no, you don't have to type a command: the arguments are passed to the program as normal operating system processing. – wallyk Dec 24 '11 at 20:18
  • why is it `argv[1]` not `argv[0]`? – Alexander Mills Jun 05 '19 at 04:04
  • 1
    @AlexanderMills: `argv[0]` contains the program name. It is analogous to having the whole command line in the *argv* array. – wallyk Jun 05 '19 at 04:45
1

Use argc and argv

#include <stdio.h>

int main(int argc, char **argv)
{
    char c;
    FILE *fp;
    if(argc < 2){
        printf("Usage : ./a.out <filename>");
        exit(0);
    }

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

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

    return 0;
}
Community
  • 1
  • 1
Pheonix
  • 6,049
  • 6
  • 30
  • 48
0

Define main like this:

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

And then use argv: an array of the command-line arguments. argv[0] is the name of the command as entered at the command line, argv[1] is the first argument.

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

You probably want to check that argc > 1 to avoid an out-of-bounds array access.

0

There are many functions to read in strings; fgets and scanf, for example. The problem is that you need to know the max number of characters that you want to read in before-hand. If this is ok for you, then use one of those. If not, then you'll have to write your own function to read in a dynamic string, like here.

Community
  • 1
  • 1
Christopher Neylan
  • 8,018
  • 3
  • 38
  • 51