从C中的命令行参数打开文件

时间:2021-10-22 23:17:11

I want my C program to ask the user to type the name of the file they want to open and print the contents of that file to the screen. I am working from the C tutorial and have the following code so far. But when I execute it, it doesn't actually allow me to enter the file name. (I get the 'press any button to continue', I am using codeblocks)

我希望我的C程序要求用户键入要打开的文件的名称,并将该文件的内容打印到屏幕上。我正在使用C教程,到目前为止有以下代码。但是当我执行它时,它实际上不允许我输入文件名。 (我按'按任意按钮继续',我正在使用代码块)

What am I doing wrong here?

我在这做错了什么?

#include <stdio.h>

int main ( int argc, char *argv[] )
{
    printf("Enter the file name: \n");
    //scanf
    if ( argc != 2 ) /* argc should be 2 for correct execution */
    {
        /* We print argv[0] assuming it is the program name */
        printf( "usage: %s filename", argv[0] );
    }
    else
    {
        // We assume argv[1] is a filename to open
        FILE *file = fopen( argv[1], "r" );

        /* fopen returns 0, the NULL pointer, on failure */
        if ( file == 0 )
        {
            printf( "Could not open file\n" );
        }
        else
        {
            int x;
            /* Read one character at a time from file, stopping at EOF, which
               indicates the end of the file. Note that the idiom of "assign
               to a variable, check the value" used below works because
               the assignment statement evaluates to the value assigned. */
            while  ( ( x = fgetc( file ) ) != EOF )
            {
                printf( "%c", x );
            }
            fclose( file );
        }
    }
    return 0;
}

4 个解决方案

#1


4  

If you want to read user input from a prompt, you would use the scanf() function. To parse command line parameters, you would type them at the command line, as in:

如果要从提示中读取用户输入,可以使用scanf()函数。要解析命令行参数,可以在命令行中键入它们,如下所示:

myprogram myfilename

rather than just typing

而不仅仅是打字

myprogram

and expecting to be prompted. myfilename would be in the argv array when your program starts.

并期望得到提示。程序启动时,myfilename将在argv数组中。

So, start by removing the printf( "Enter the file name:" ) prompt. The filename would be in argv[ 1 ] assuming you entered it as the first parameter after myprogram on your command line.

因此,首先删除printf(“输入文件名:”)提示。假设您在命令行上的myprogram之后将其作为第一个参数输入,则文件名将在argv [1]中。

#2


5  

This will read from stdin the filename. Perhaps you only want to do this if the filename is not supplied as a part of the command line.

这将从stdin读取文件名。如果文件名不是作为命令行的一部分提供的,也许您只想这样做。

int main ( int argc, char *argv[] ) 
{ 
    char filename[100];
    printf("Enter the file name: \n"); 
    scanf("%s", filename);

    ...
    FILE *file = fopen( filename, "r" );  

#3


4  

You are mixing up command line arguments with user input.

您正在将命令行参数与用户输入混合在一起。

When you use command line arguments, you execute the program and pass the arguments at the same time. For example:

使用命令行参数时,执行程序并同时传递参数。例如:

ShowContents MyFile.txt

In contrast, when you read user input, you first execute the program, then provide the file name:

相反,当您读取用户输入时,首先执行程序,然后提供文件名:

ShowContents
Enter the file name: MyFile.Ttxt

Your program already reads the first argument argv[1] and treats it as the name of the file to open. To have the program read user input, do something like this:

您的程序已经读取了第一个参数argv [1]并将其视为要打开的文件的名称。要让程序读取用户输入,请执行以下操作:

char str[50] = {0};
scanf("Enter file name:%s", str); 

Then the file name will be in str, instead of argv[1].

然后文件名将在str中,而不是argv [1]。

#4


2  

It's because your IDE is not passing the filename argument to your program. Take a look at this question on *.

这是因为您的IDE没有将filename参数传递给您的程序。在*上查看这个问题。

#1


4  

If you want to read user input from a prompt, you would use the scanf() function. To parse command line parameters, you would type them at the command line, as in:

如果要从提示中读取用户输入,可以使用scanf()函数。要解析命令行参数,可以在命令行中键入它们,如下所示:

myprogram myfilename

rather than just typing

而不仅仅是打字

myprogram

and expecting to be prompted. myfilename would be in the argv array when your program starts.

并期望得到提示。程序启动时,myfilename将在argv数组中。

So, start by removing the printf( "Enter the file name:" ) prompt. The filename would be in argv[ 1 ] assuming you entered it as the first parameter after myprogram on your command line.

因此,首先删除printf(“输入文件名:”)提示。假设您在命令行上的myprogram之后将其作为第一个参数输入,则文件名将在argv [1]中。

#2


5  

This will read from stdin the filename. Perhaps you only want to do this if the filename is not supplied as a part of the command line.

这将从stdin读取文件名。如果文件名不是作为命令行的一部分提供的,也许您只想这样做。

int main ( int argc, char *argv[] ) 
{ 
    char filename[100];
    printf("Enter the file name: \n"); 
    scanf("%s", filename);

    ...
    FILE *file = fopen( filename, "r" );  

#3


4  

You are mixing up command line arguments with user input.

您正在将命令行参数与用户输入混合在一起。

When you use command line arguments, you execute the program and pass the arguments at the same time. For example:

使用命令行参数时,执行程序并同时传递参数。例如:

ShowContents MyFile.txt

In contrast, when you read user input, you first execute the program, then provide the file name:

相反,当您读取用户输入时,首先执行程序,然后提供文件名:

ShowContents
Enter the file name: MyFile.Ttxt

Your program already reads the first argument argv[1] and treats it as the name of the file to open. To have the program read user input, do something like this:

您的程序已经读取了第一个参数argv [1]并将其视为要打开的文件的名称。要让程序读取用户输入,请执行以下操作:

char str[50] = {0};
scanf("Enter file name:%s", str); 

Then the file name will be in str, instead of argv[1].

然后文件名将在str中,而不是argv [1]。

#4


2  

It's because your IDE is not passing the filename argument to your program. Take a look at this question on *.

这是因为您的IDE没有将filename参数传递给您的程序。在*上查看这个问题。