【问题标题】:A loop to pass a string to STD in将字符串传递给 STD 的循环
【发布时间】:2013-05-23 18:45:11
【问题描述】:

我是一个编程技能非常糟糕的菜鸟。

我有一个这样的文本文件:

(括号中的大字符串)整数值

(括号中的大字符串)整数值

(括号中的大字符串)整数值

(括号中的大字符串)整数值

我有一个程序可以读取标准输入上的字符串并返回一个值。

我需要某种方法将字符串(并且只有字符串)传递给程序,并将它返回的值与文本文件中的值进行比较。

我尝试使用 Bash 脚本执行此操作,但我感到非常困惑和沮丧,所以我想尝试使用 C 来执行此操作。

int main(void)
{
int x;          
FILE *fp;
fp = fopen("vectors.lst", "r");

if (fp == NULL) {
     printf("Failed to open\n");
     exit(0);
  }

    for(i = 1; i<10400; i++){


        //This is where I am confused

            while(fscanf(fp, "%s %d",passedstring,correctvalue) != EOF){

        printf("%s",passedstring);


            }
}
exit(0);
}

我的问题是:

我不知道如何只遍历 for 循环中的字符串,而仍然只读取到文件末尾。我开始认为使用 C 语言是一个糟糕的主意。

谁能给点建议?如果有一种简单的方法来实现它,我仍然愿意使用脚本。

【问题讨论】:

    标签: c scripting stdin


    【解决方案1】:

    既然您知道字符串将以 ')' 结尾,为什么不可以使用 getchar() 来读取字符串。使用类似的东西:

      do {
        c=getchar();
        //process input
      } while (c != ')');
    

    【讨论】:

    • 我可以,这需要我重新编程主程序(实际读取字符串的程序)。如果可能的话,我想将整个括号和所有内容传递给它。我基本上只想一次将文本文件中的每个字符串传递给该程序。
    • 如果你在 main() 函数中得到字符串,你可以让这个函数遍历它。类似于: void func(char* str) { int i = 0; do { //一些代码 i++; } while(str[i] != ')'); }
    【解决方案2】:

    有些东西我不太了解,但我觉得很喜欢。

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main(void){
        int x;
        char buff[4096];
        FILE *fp;
    
    
        fp = fopen("vectors.lst", "r");
    
        if (fp == NULL) {
            printf("Failed to open\n");
            return 1;
        }
    
        printf("search value: ");
        scanf("%d", &x);
    
        while(fgets(buff, sizeof(buff), fp)){
            char *p;
            p=strrchr(buff, ' ');//(....) integer\n
            if(atoi(p+1) == x){//found! (correctvalue : from p+1)
                *p ='\0';
                printf("%s\n", buff);//copy
                break;
            }
        }
        fclose(fp);
    
        return 0;
    }
    

    【讨论】:

      【解决方案3】:

      嗯,已经有一段时间没有使用 &lt;stdio.h&gt; 库了。前段时间,我为此写了一篇文档。它实际上仍然在网上here 上流传。我重读了一遍,还是不错的。 (看起来有人甚至在几年前发表了一条我没有回应的评论......哎呀)。不过,您可能也想阅读它。

      使用 C stdio 库,您几乎可以做任何事情,但由于缓冲区溢出和错误处理处理不当,您可能会遇到危险。但可以做到(但我个人的偏好是尽可能使用 perl)。

      您对此并没有真正具体说明,但我会试一试。我认为是这样的:

      #include <stdio.h>
      #define STRINGIZE_(x) #x
      #define STRINGIZE(x) STRINGIZE_(x)
      
      int main()
      {
        FILE *fp;
        fp = fopen("vectors.lst", "r");
      
        if (fp == NULL) {
          printf("Failed to open\n");
          /* Exit return of non-zero to indicate an error to calling process */
          return 1;
        }
      
      #define MAX_BUFFER 1024
        char passedstring[MAX_BUFFER+1];
        passedstring[MAX_BUFFER] = 0;
        int correctvalue = 0;
        int position;
      
        position = 0;
        /* " " - ignore any leading whitespace if any
         * "(" - match open parenthesis
         * "%" STRINGIZE(MAX_BUFFER) "[^)]" - store up to MAX_BUFFER chars 
         *       that are not ')'
         * ")" - match close parenthesis
         * " " - match any whitespace if any
         * "%d" - store int value
         * "%n" - store current read byte from fscanf(), if parse fails, store
         *        doesn't occur.  Ensures that everything has been read in.
         **/
        while(fscanf(fp, " (%" STRINGIZE(MAX_BUFFER) "[^)]) %d%n"
              , passedstring, &correctvalue, &position) != EOF
            && position > 0)
        {
          printf("%s, %d\n", passedstring, correctvalue);
          position = 0;
        }
        printf("End %d", position);
      
        /* Exit code returned here.  Not advised to use exit() for something like this. */
        return 0;
      }
      #undef MAX_BUFFER
      

      不确定您的外部 for 循环是做什么用的。这只是读取直到它到达文件的末尾,或者如果一个字符串太大以至于它比缓冲区大。如果你想做一些恢复代码,我会留给你。

      我通过编译器运行它。现在已验证可以正常工作。

      【讨论】:

        猜你喜欢
        • 2016-01-12
        • 1970-01-01
        • 2015-03-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-03-31
        • 1970-01-01
        • 2012-06-13
        相关资源
        最近更新 更多