【问题标题】:fgetc starting point for cc 的 fgetc 起点
【发布时间】:2014-03-29 00:28:35
【问题描述】:

因此,我目前的代码出现分段错误,并试图缩小可能的范围。我不确定fgetc 函数的起点是否与fprintfscanf 的定位相同。

即,如果我在文件上使用了scanf,然后使用fgetc,它会从一开始就开始,还是会从scanf 停止的地方继续?如果是前者,我将如何操作起点?

//reads the second line of the input file in order to grab the ten numbers in the line
int numRead = fscanf(Data, "\n %d %d %d %d %d %d %d %d %d %d", &zero, &one,
        &two, &three, &four, &five, &six, &seven, &eight, &nine); 

while(EOF != (c = fgetc(Data)))
{
  message[i] = c;
  i++;
}

输入文件:

 0  1  2  3  4  5  6  7  8  9  
 6  7  8  0  1  9  5  2  3  4  
If I were reincarnated, I'd want to come back a  
buzzard. Nothing hates him or envies him or wants  
him or needs him. He is never bothered or in  
danger, and he can eat anything.  
-- Mark Twain  

【问题讨论】:

  • 它将在 scanf 停止的地方继续。但是:有时 scanf 与 fgetc、fgets 等混合使用时会非常讨厌。无论如何,显示您的代码,没有我们找不到任何段错误。
  • 使用 valgrind,它可能会查明您的问题。
  • 另外,编译时启用编译器可以提供的所有警告。准确了解它发出警告的原因并解决问题。
  • 确保cint。你读到的第一个字符可能是nine 之后的换行符。

标签: c fgetc


【解决方案1】:

循环中的fgetc 调用将在fscanf 完成的位置开始,但您应该注意,此点将位于扫描最后一个项目之后的下一个字符处。假设它工作正常,那将是第二行末尾的 \n 字符(假设您事先位于该行的开头,这似乎是您的代码 cmets 的情况)。

因此,第一个fgetc 将为您提供上述\n,下一个将为您提供第三行开头的'I',依此类推。

如果您遇到崩溃,我会立即检查几件事。

首先是cint 类型而不是char。这是必需的,以便您可以从中接收任何有效的char 类型加上 EOF 指示符。

第二个是message足够大,可以容纳数据。

第三个是i被初始化为零。

为了安全起见,您可能还应该检查您的扫描是否可以读取十个数字。

查看以下完整程序,了解如何执行此操作。您会注意到我还在检查以确保缓冲区不会因为文件中的数据过多而溢出:

#include<stdio.h>

int main(void)
{
    // Open file for reading.

    FILE *Data = fopen ("qq.in", "r");
    if (Data == NULL) {
        puts ("Cannot open qq.in");
        return 1;
    }

    // Skip first and second line (twenty numbers).

    int zero, one, two, three, four, five, six, seven, eight, nine;

    int numRead = fscanf(Data, "%d %d %d %d %d %d %d %d %d %d", &zero, &one,
        &two, &three, &four, &five, &six, &seven, &eight, &nine);
    if (numRead != 10) {
        puts ("Could not read first ten integers");
        fclose (Data);
        return 1;
    }

    numRead = fscanf(Data, "%d %d %d %d %d %d %d %d %d %d", &zero, &one,
        &two, &three, &four, &five, &six, &seven, &eight, &nine);
    if (numRead != 10) {
        puts ("Could not read second ten integers");
        fclose (Data);
        return 1;
    }

    // Loop for reading rest of text (note initial newline here).

    int c, i = 0;
    char message[1000];

    while(EOF != (c = fgetc(Data))) {
        if (i >= sizeof(message)) {
            puts ("Too much data");
            fclose (Data);
            return 1;
        }
        message[i++] = c;
    }

    fclose (Data);

    printf ("[%*.*s]\n", i, i, message);

    return 0;
}

运行时,会产生:

[
If I were reincarnated, I'd want to come back a
buzzard. Nothing hates him or envies him or wants
him or needs him. He is never bothered or in
danger, and he can eat anything.
-- Mark Twain
]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-13
    • 2014-05-13
    • 1970-01-01
    • 2015-12-14
    • 2021-05-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多