相关疑难解决方法(0)

为什么"while(!feof(file))"总是错的?

我看到人们最近在很多帖子中试图读取这样的文件.

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

int
main(int argc, char **argv)
{
    char *path = argc > 1 ? argv[1] : "input.txt";

    FILE *fp = fopen(path, "r");
    if( fp == NULL ) {
        perror(path);
        return EXIT_FAILURE;
    }

    while( !feof(fp) ) {  /* THIS IS WRONG */
        /* Read and process data from file… */
    }
    if( fclose(fp) == 0 ) {
        return EXIT_SUCCESS;
    } else {
        perror(path);
        return EXIT_FAILURE;
    }
}
Run Code Online (Sandbox Code Playgroud)

这个__CODE__循环有什么问题?

c file while-loop feof

550
推荐指数
4
解决办法
21万
查看次数

声明大型数组时出现堆栈溢出异常

以下代码为我生成堆栈溢出错误

int main(int argc, char* argv[])
{
    int sieve[2000000];
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我该如何解决这个问题?我正在使用Turbo C++,但我想将我的代码保存在C中

编辑:

感谢您的建议.上面的代码只是例如,我实际上在函数中声明了数组而不是在sub main中.此外,我需要将数组初始化为零,所以当我使用Google搜索时,我发现calloc非常适合我的目的.

Malloc/calloc还具有优于堆栈分配的优势,允许我使用变量声明大小.

c memory arrays stack allocation

39
推荐指数
2
解决办法
1万
查看次数

标签 统计

c ×2

allocation ×1

arrays ×1

feof ×1

file ×1

memory ×1

stack ×1

while-loop ×1