【问题标题】:C - If (No File Found) { Use standard input }C - If (No File Found) { 使用标准输入 }
【发布时间】:2015-05-13 02:48:59
【问题描述】:

我有一个从文件读取的程序,但是如果参数数组中没有声明文件,那么我想从终端中的标准输入读取:

 ./program.out test.txt

从 test.txt 读取

 ./program.out

从标准输入读取:

这是我的一些上下文代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

FILE *fr;
char *line;
char *word;
size_t len =256;
int i=0;
int sum=0;
char *vali;
const char delim = ' ';
int flag=0;

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

line = (char *)malloc(len);
word = (char *)malloc(len);


/*
line = (char *)malloc(sizeof(&len));
word = (char *)malloc(sizeof(&len));
vali = (char *)malloc(sizeof(&len));
*/
    fr = fopen(argv[1], "r");
    if(fr==NULL){
        //fr="/dev/stdin"; <-- Piece of code I need for this stackoverflow question
    }

        while (getline(&line, &len, fr) != -1){
            /* printf("%s", line ); */
            if(strlen(line) != 1){
                sscanf(line,"%s%*[^\n]",word);
                 printf("%-10s ", word); 
                char *scores = line + strlen(word) + 1;         
    /*          printf("scores: %s", scores); */



                vali=strtok(scores, &delim);
                while(vali != NULL){
                    sum=sum+atoi(vali);

                    vali = strtok(NULL, &delim);
                }

                printf("%3d\n", sum);
                sum=0;
            }
        }
        fclose(fr);

    free(line);
//   free(word);
//  free(vali);
    return 0;
}

【问题讨论】:

    标签: c file input stdin


    【解决方案1】:

    更改这些行:

    fr = fopen(argv[1], "r");
    if(fr==NULL){
        //fr="/dev/stdin"; <-- Piece of code I need for this stackoverflow question
    }
    

    if ( argc > 1 )
    {
        // A file was passed in as the first argument.
        // Try to open it.
        fr = fopen(argv[1], "r");
        if(fr==NULL){
           // Deal with the error of not being able to open the file.
        }
    }
    else
    {
        // Nothing was passed to the program.
        // use stdin.
        fr = stdin;
    }
    

    【讨论】:

    • 完美,谢谢。我会在 10 分钟内接受作为答案
    【解决方案2】:

    怎么样:

    if(fr==NULL){
        fr=stdin;
    

    毕竟它们都是文件指针。

    【讨论】:

      【解决方案3】:
      #include <stdio.h>
      #include <stdlib.h>
      #include <string.h>
      
      FILE *fr;
      char *line;
      char *word;
      size_t len =256;
      int i=0;
      int sum=0;
      

      这些已经是 0. 40 美元罚款了。

      char *vali;
      

      非英文变量名。罚款 50 美元。

      const char delim = ' ';
      int flag=0;
      

      无缘无故使用全局变量将被罚款高达每字节 10 美元。不一致的间距是额外的 20 美元。

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

      “*”的位置不一致。

      line = (char *)malloc(len);
      word = (char *)malloc(len);
      

      铸造 malloc 是 50 美元。 http://www.c-faq.com/malloc/mallocnocast.html

      /*
      line = (char *)malloc(sizeof(&len));
      word = (char *)malloc(sizeof(&len));
      vali = (char *)malloc(sizeof(&len));
      */
      

      请考虑 #if 0 ... #endif。

          fr = fopen(argv[1], "r");
      

      缩进不一致。

      首先没有检查是否提供了参数。

      如果“-”作为名称传递,标准行为是打开标准输入。

          if(fr==NULL){
              //fr="/dev/stdin"; <-- Piece of code I need for this stackoverflow question
      

      其原理不正确。如果程序无法打开传递的文件,则退回到标准输入是一种刑事犯罪,考虑到没有给出任何指示,更是如此。

      你应该检查你的论点。如果提供了文件但打开失败,打印错误

      最后,有很多 unix 命令行工具默认为标准输入并且有可用的源(例如 cat),所以你可以检查它们的作用。有趣的是,您可以在这里使用一个名为 stdin 的 FILE 指针。

          }
      
              while (getline(&line, &len, fr) != -1){
      

      甚至更不一致的缩进。

                  /* printf("%s", line ); */
                  if(strlen(line) != 1){
      

      为什么? 'len' 没有吗?

                      sscanf(line,"%s%*[^\n]",word);
      

      什么?这真的可以做得更好。你知道换行必须在最后并且长度是已知的。

                       printf("%-10s ", word); 
                      char *scores = line + strlen(word) + 1;         
          /*          printf("scores: %s", scores); */
      
      
      
                      vali=strtok(scores, &delim);
                      while(vali != NULL){
                          sum=sum+atoi(vali);
      
                          vali = strtok(NULL, &delim);
                      }
      
                      printf("%3d\n", sum);
                      sum=0;
      

      不是很好的人。至少做 sum = 0;就在循环中修改它之前。只是流浪 sum=0 让人想知道这里发生了什么。 10 美元。

                  }
              }
              fclose(fr);
      

      最后,如果结果是标准输入,则不应执行。

          free(line);
      //   free(word);
      //  free(vali);
      

      好吧,你分配了单词和行,但不是 vali 所以注释掉的部分是不正确的。

          return 0;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-11-18
        • 2013-07-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-01-13
        相关资源
        最近更新 更多