问题写在C文件 [英] Problem with writing to file in C

查看:194
本文介绍了问题写在C文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着写一些信息在C文件,并从来没有运行到一个问题之前。然而,现在看来,写一个变量的内容到文件时打破。下面是我。

I'm trying to write some information to a file in C and was never running into a problem before. However, now it seems to break when writing a variable's contents to file. Here is what I have.

    int i, count = 0;
    FILE *f;
    int _x, _y, _z, _x2, _y2, _z2;

    for (i = 0; i < HEIGHT * WIDTH*3; i+= 3)
    {
        if (buffer1[i/3] < MAGIC_VALUE)
        {
            count++;
        }

        if (buffer2[i/3] < MAGIC_VALUE)
        {
            count++;
        }
    }

    printf("Count = %d\n", count); // prints correctly...
    f = fopen("file.abc", "w");
    fprintf(f, "lots\n of\n text\n");

    fprintf(f, "count: %d\ntext \ntext y\ntext text text", count); // crashes here
    fprintf(f, "\nend");

    fclose(f);

这是为什么行崩溃?在这一点上说,该行结束了在dbghook.c _debugger_hook_dummy = 0;

打印计数来的文件时,碰撞正在发生,但如果我拿出的打印,将打印最后陈述时崩溃。第一个似乎是印刷精美,但..

The crash is occurring when printing count to the file, but if I take out that print, it will crash when printing the last statement. The first one seems to be printing fine, though..

当我打印错误,我得到打开的文件太多

When I print the error, I get "Too many open files"

推荐答案

第1步:替换


f = fopen( path, mode );


f = fopen( path, mode );
if( f == NULL ) {
    perror( path );
    exit( EXIT_FAILURE );
}

这篇关于问题写在C文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆