【发布时间】:2017-09-20 15:40:09
【问题描述】:
以下是我的代码的一部分,它在文件处理方面存在问题。该文件可以使用 fopen 正常打开,但是当我尝试读取或关闭该文件时,我的程序会退出而不会出现错误。我试图独立运行这段代码,它工作正常。如果有人能帮助我指出我做错了什么,我将不胜感激。
int ctrlSend(char *etherPort, uint8_t *inPayload, int payloadLen, int vlanID)
{
char intName [10]; // Interface name from file
int intVlan; // Interface VLAN from file
printf("In ctrlSend\n");
FILE * pFile; // File pointer
pFile = fopen ("vlan.conf","r");
while(!feof(pFile))
{
fscanf(pFile,"%s %d",intName,&intVlan)
printf("In ctrlSend while loop");
}
fclose (pFile);
return 0;
}
UPDATE1:更新了上面的代码
UPDATE2:下面有相同问题的备用代码。
int ctrlSend(char *etherPort, uint8_t *inPayload, int payloadLen, int vlanID)
{
printf("In ctrlSend\n");
char intName [10]; // Interface name from file
int intVlan; // Interface VLAN from file
FILE * pFile; // File pointer
pFile = fopen ("vlan.conf","r");
while (fscanf (pFile,"%s %d",intName,&intVlan) == 2)
{
printf("In ctrlSend while loop");
}
fclose (pFile);
return 0;
}
UPDATE3:好像文件没有打开,正在查看。
【问题讨论】:
-
while 循环不会因非空文件而终止。看起来与您所描述的问题相反。
-
@EugeneSh。它不会为 any 文件终止,即使是空的。
-
使用前没有测试
fopen()的返回值,你怎么知道文件“打开正常”? -
你怎么知道对
fscanf()的add-by-a-recent-edit 调用有效?您没有检查 任何内容 是否有错误。
标签: c file scanf fopen file-handling