【问题标题】:Cant scan file in C无法在C中扫描文件
【发布时间】:2015-06-27 04:10:50
【问题描述】:

我似乎无法让我的简单脚本读取文件。我要做的就是将字母读入数组。我只是得到随机字符,这让我发疯。我在 Debug 文件夹中有文件,还有什么问题?我百分百确定一切都是正确的。

这是我的代码:

FILE * ifp;
   ifp = fopen("letters.txt", "r");


   int i;
   int bound = 20;
   char data[20];


    for(i =0; i<bound; i++){
        fscanf(ifp, "%s", &data[1]);
        if (ifp == NULL){
            return;
        }
        printf("Data %d = %c\n", i, data[i]);

    }

【问题讨论】:

  • 您的代码输入固定数组元素,如 &data[1]
  • 首先,如果您尝试“阅读字母”,为什么要使用%s 说明符,它读取字符串(单词)?其次,为什么要反复读入固定的&amp;data[1] 位置?第三,当循环中没有可能更改ifp的代码时,为什么要反复检查ifp是否为空?
  • 好的,我把它改成了 data[i] ,它仍然做同样的事情,哈哈!还将char类型更改为“c”,那是看它是否读取文件...不是哈哈
  • 始终检查 (!=NULL) fopen() 的返回值以确保操作成功 始终检查 fscanf() 的返回值(不是参数值)以确保操作成功。 fscanf(),如果成功,使用 %s 时将始终使用输入缓冲区中的两个位置,因为始终附加一个 NUL 字节,并且使用 %s,如果字母是连续的,将在第一次调用 fscanf() 时读取它们输入缓冲区限制为 20 个字符,因此允许的最大值为 19 个字符。为 19 的 %s 添加长度修饰符
  • 好的,我增加了我的数组大小,检查了 null 并收到了我的错误消息。什么会影响这个程序不读取文件?

标签: c file scanf


【解决方案1】:

以下代码

1) checks for errors
2) compiles cleanly
3) works on a test.txt file I produced.

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

#define bound (20)

int main( void )
{
    FILE * ifp;

    if( NULL == (ifp = fopen("letters.txt", "r") ) )
    { // then, fopen failed
        perror( "fopen for letters.txt failed");
        exit( EXIT_FAILURE );
    }

    // implied else, fopen successful


    int i;

    char data[bound];


    for(i =0; i<bound; i++)
    {
        if( 1 != fscanf(ifp, "%c", &data[i]) )
        { // then fscanf failed
            perror( "fscanf failed" );
            exit( EXIT_FAILURE );
        }

        // implied else, fscanf successful

        printf("Data %d = %c\n", i, data[i]);
    }
    return(0);
} // end function: main

【讨论】:

    【解决方案2】:

    嗯,可能是因为

     &data[1]
    

    应该只是

    data
    

    或同等的

    &data[0]
    

    这很可能是你想要的。

    ?

    我能想到的唯一其他原因是,您正在阅读的行超过 20 个字符并且它溢出了数据数组。

    看看这个例子:http://www.tutorialspoint.com/c_standard_library/c_function_fscanf.htm

    【讨论】:

      【解决方案3】:

      这是一个例子:

      #include <stdio.h>
      
      #define MAX_LINES 10
      #define MAX_LINE  80
      #define FILE_NAME "lines.txt"
      
      int
      main (int argc, char *argv[])
      {
        char lines[MAX_LINES][MAX_LINE];
        FILE *fp;
        int i = 0;
      
        if (!(fp = fopen(FILE_NAME, "r"))) {
          perror ("file open error");
          return 1;
        }
      
        while (i < MAX_LINES) {
          if (fgets (lines[i], MAX_LINE, fp)) {
            printf ("Line[%d]=%s", i, lines[i]);
            i++;
          } else {
            printf ("End of file: closing %s\n", FILE_NAME);
            break;
          }
        }
        fclose (fp);
        return 0;
      }
      

      样本输出:

      $ gcc -o x1 -Wall -pedantic -g x1.c
      $ ./x1
      Line[0]=Come and listen to my story
      Line[1]='Bout a man named Jed
      Line[2]=Poor mountaineer,barely kept his fam'ly fed
      End of file: closing lines.txt
      

      注意:

      1. 我们一次读一行

      2. 我们将每一行读入一个数组

      3. 我们提前定义了数组中的#/元素(MAX_LINES)和每个字符串的最大大小(MAX_LINE)

      4. 我们检查“文件打开错误”(并相应地返回“非零”)

      5. 我们使用“fgets()”来安全地读取一行(没有缓冲区溢出的风险)。

      如果你想一次读取一个字符,程序可以很简单:

       char string[MAX_LINE], *c;
       ...
       if (!(fp = fopen(FILE_NAME, "r"))) {
          perror ("file open error");
          return 1;
       }
       ...     
       c = string;
       while ((*c = fgetc (fp)) != EOF)
          c++;
      

      第二个例子的一个问题是它不检查字符串长度>> MAX_LINE。但希望它可以帮助您提供一些想法...

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-04-21
        • 2016-01-02
        • 1970-01-01
        • 1970-01-01
        • 2019-07-22
        • 2020-07-30
        相关资源
        最近更新 更多