【问题标题】:Reading .txt File Input not Working读取 .txt 文件输入不起作用
【发布时间】:2016-09-26 03:29:41
【问题描述】:

所以我是 C 的初学者,我试图打开和读取一个文件,将文件的每个元素存储在一个数组中。下面的代码似乎应该在实践中工作,但是当输出给我时

50 2500

无论出于何种原因。如果有人可以就我需要解决的问题提供任何帮助,将不胜感激

...

【问题讨论】:

  • 我刚刚编辑了我的帖子以包含输入文件 people
  • 另外,尝试使用“从 C 中的文本文件读取 int 值”中提出的解决方案不起作用 - 可能是因为我的 .txt 文件有逗号,而其他帖子都是整数。跨度>
  • magicSquareArray[0] 是文件中的第一个字符 - 您希望它包含什么?
  • @immibis 我期待它包含 3,但是当我 printf 第一个元素时它会打印 50

标签: c io


【解决方案1】:

当您使用 fgets() 时,您将使用换行符作为分隔符从文件中提取字符串(字符数组)。 fscanf() 更适合从文件中提取整数。

你的程序打印 50 的原因是因为在 while 循环之后 magicSquareArray[0] 的值是 '2',而不是 2,而 '2' 的 ascii 值是 50。

【讨论】:

    【解决方案2】:

    这样的例子

    #include <stdio.h>
    
    int main(int argc, char *argv[]){
        char *input = argv[1];//if(argc > 1)
        FILE *inputFile = fopen(input, "r");
    
        if (inputFile == NULL){
            printf("Cannot open file for reading!\n");
            return -1;
        }
    
        int n;
        fscanf(inputFile, "%d", &n);//if(1==
        int squareArray[n][n];
        int r, c, v;
        char comma;
        r = c = 0;
        while(2==fscanf(inputFile, "%d%c", &v, &comma)){
            if(comma == ',' || comma == '\n' && c == n-1){//comma == ',' && c < n-1
                squareArray[r][c] = v;
                if(++c == n){
                    c = 0;
                    ++r;
                }
            } else {
                fprintf(stderr, "input file invalid format\n");
                return -2;
            }
        }
        fclose(inputFile);
    
        if(r != n || c != 0){
            fprintf(stderr, "It could not be read correctly.\n");
            return -3;
        }
    
        for(r = 0; r < n; ++r){
            for(c = 0; c < n; ++c){
                printf("%d ", squareArray[r][c]);
            }
            puts("");
        }
        return 0;
    }
    

    【讨论】:

      【解决方案3】:

      你可以使用这个功能:

      strucT *ReadFromFile (const char *fileName)
      {
          printf ("\n***Start function ReadFromFile for file name: %s *** \n" , fileName );   
          strucT *myfilePtr = (strucT*) malloc ( sizeof (strucT) );
      
          strcpy( myfilePtr->fileName, fileName );    
          myfilePtr->file = fopen ( myfilePtr->fileName , "r");
          int i = 0;
      
          fscanf( myfilePtr->file , "%d", &myfilePtr->rows);
          printf("rowSize is: %d\n" , myfilePtr->rows);
      
          fscanf( myfilePtr->file , "%d", &myfilePtr->colus);
          printf("culSize is: %d\n" , myfilePtr->colus);
      
          myfilePtr->elementNumber = (myfilePtr->rows) * (myfilePtr->colus);
          printf("arraySize is %ld\n", myfilePtr-> elementNumber);
      
      //  myfilePtr->array = (unsigned char*)malloc(sizeof(unsigned char) * (myfilePtr->elementNumber));
          for (i = 0 ; i < myfilePtr->elementNumber; i++)
                   {
                      fscanf(myfilePtr->file, "%1d", (int*) &myfilePtr->array[i]);    
                      //printf("%d" , myfilePtr.array[0]);
                  }
      // FOR DEBUG    
      for (i = 0 ; i < 12 ; i++)
              printf ("%d" , myfilePtr->array[i]);
          printf ("\n");
      
          fclose(myfilePtr->file);
          printf ("\n***Finish function ReadFromFile for file name: %s *** \n" , fileName );  
          return strucT;
      }
      

      此函数读取第一个和第二个 int 并保存它们。 在此之后,她读取数组值(每个值是 1 个字符)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多