【问题标题】:Opening a file from command line arguments in C从 C 中的命令行参数打开文件
【发布时间】:2012-03-15 23:56:52
【问题描述】:

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

我在这里做错了什么?

#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;
}

【问题讨论】:

  • +1。这是如何提出关于家庭作业的问题。 “我已经走了这么远,但遇到了障碍”而不是“为我编写这个程序”。

标签: c fopen


【解决方案1】:

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

myprogram myfilename

而不仅仅是输入

myprogram

并期望得到提示。当您的程序启动时,myfilename 将位于 argv 数组中。

所以,首先删除printf( "Enter the file name:" ) 提示符。文件名将在argv[ 1 ] 中,假设您在命令行中将其作为myprogram 之后的第一个参数输入。

【讨论】:

    【解决方案2】:

    这将从标准输入读取文件名。如果文件名未作为命令行的一部分提供,您可能只想这样做。

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

    【讨论】:

      【解决方案3】:

      您将命令行参数与用户输入混为一谈。

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

      ShowContents MyFile.txt
      

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

      ShowContents
      Enter the file name: MyFile.Ttxt
      

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

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

      那么文件名将在str,而不是argv[1]

      【讨论】:

      • 啊!你是对的,我把它们混在一起了。为清除它而欢呼!
      【解决方案4】:

      这是因为您的 IDE 没有将文件名参数传递给您的程序。看看这个question on stackoverflow

      【讨论】:

        【解决方案5】:
        #include<stdio.h>
        #include<stdlib.h>
        #include<string.h>
        int main(int argc, char *argv[] )
        {
            char filename[50];
            printf("Enter the Filename : ");
            scanf("%s",&filename);
            char line[50];
            FILE *fp = fopen( filename, "r");
           
            if (fp == NULL)
            {
                printf("File opening Unsuccessful\n");
                exit(1);
            }
            while (fgets(line , 30 , fp) != NULL)
            {
                strrev(line);
                printf(line);
            }
           fclose(fp) ;
        
           return 0;
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-11-08
          • 2017-03-24
          • 1970-01-01
          • 2018-03-20
          • 2012-02-21
          • 1970-01-01
          • 2020-03-22
          • 2012-02-12
          相关资源
          最近更新 更多