我在循环中出现问题吗? [英] problem while I was in the loop?

查看:122
本文介绍了我在循环中出现问题吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

FILE *fp;
fp = fopen(ImeFajla->Text.c_str(),"a+");
if (fp == NULL)
    Label1->Caption="Neuspjesno ovaranje fajla...";
Label1->Caption="Ucitan sadrzaj fajla";
char s[101];
while(fgets(s,100,fp) != NULL)         
// *problem Project Project11.exe raised exception class EAccessViolation 
// with message ''Access violation at address 3286ED28 in module 
// ''CC3280MT.DLL''. Read of address F85BE2DC''.
{
    ListBox1->Items->Add(s);
    n++;
}
fclose(fp);
}

推荐答案

fp是否为NULL?

当您检查fp是否为NULL时,即使它为NULL,也要继续尝试使用它.
Is fp NULL?

While you do check if fp is NULL, you also continue and attempt to use it even if it is NULL. This is not appropriate.


stavor写道:

if(fp == NULL)
Label1-> Caption ="Neuspjesno ovaranje fajla ...";
Label1-> Caption ="Ucitan sadrzaj fajla";

if (fp == NULL)
Label1->Caption="Neuspjesno ovaranje fajla...";
Label1->Caption="Ucitan sadrzaj fajla";



显示错误或类似内容,如果fopen没有返回有效的文件指针,则从该函数返回控制.

就像其他张贴者所说的那样,您正在检查此错误情况,但忽略了可能发生的任何可能的错误!



Show an error or something, and return control from the function if fopen doesn''t return a valid file pointer.

Like the other poster said, you''re checking for this error condition, but are ignoring any possible error that could occur!


这篇关于我在循环中出现问题吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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