FCLOSE的问题

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

问题描述

亲爱的小组


我遇到了fclose的问题,我想知道是否有人

可以为我提供一些关于这个问题的见解。目前,我在一个小型的个人项目上工作,需要我将一些现有的代码更改为一个程序。在运行未经修改的代码时,我不断遇到一个应用程序(异常未知软件

异常(0xc00000fd)等等等等...等等...我是运行W2K Pro)。

通过一些调试,我挑出了问题所在的地方

发生了。当它试图关闭之前已经打开的
的文件时,它就会发生。在修补这个问题时,我注意到

,当我手动将源文件中的
名称输入源代码时关闭时没有发生错误。只有当用户必须从命令行键入文件名时才会出现错误(这就是我希望运行

程序的方式)。是什么导致了这个问题?下面我已经为导致问题的函数提供了一些源代码。这是

没有错误发生的源版本。我已将

字符串finc_name设置为文件名all.inc。通常,数组设置为大小为80的默认值,类似于fpov_name。我还要

注释掉代码部分,提示用户输入

文件名。


谢谢

Marcus D. Jacobs


#include< stdio.h>

#include< stdlib .h>

#include< ctype.h>

#include< string.h>

#include" numtypes.h" ;

/ * #include" numtable.h" * /


typedef struct

{

Word x,r,g,b;

} colorinfo;

FILE * finc,* fpov,* frad;

char ch ,frad_name [80],finc_name [] =" all.inc",fpov_name [80],

is_num;

char * pov_word;

char chpov,colorend =''f'';

colorinfo颜色;

Word纹理[2600];

void reads_triangle(Word pov_num);

void reads_smoothtriangle(Word pov_num);

void reads_inc(void);

void reads_color(void);

void reads_texture(void);


main(无效)

{

int m = 1,n,j;

char chtext ,完成;

char * inc_word;

int isize;


int j2;


/ *

printf("输入POV包含文件名(* .inc):");

得到(finc_name);

printf(" \ n");

* /


finc = fopen(finc_name," r");

chtext = getc(finc);


if(finc == NULL)

{printf("不能打开%s \ n",finc);

退出(EXIT_FAILURE);

}

while(chtext!= EOF)

{

finish =''f'';

if((inc_word = malloc(25))== NULL)

{printf(" Out of memory);

退出(EXIT_FAILURE);;

}

而(isspace(chtext))chtext = getc(finc);

j = 0;

while(chtext!= EOF&& isspace(chtext)== 0)

{inc_word [j ++] = chtext;

chtext = getc(finc);

}


inc_word [j] =''\ 0'';


if(strcmp(inc_word," texture")== 0&& finish =='' f'')

{while(isspace(chtext))chtext = getc(finc);

n = 0;

chtext = getc( finc);

while(chtext!=''}'')

{

texture [m] [n ++] = chtext;

chtext = getc(finc);

}

texture [m] [n] =''\''';

/ * printf(" texture =%s",texture [m]); * /

m ++;

finish =''t'';

}

免费(inc_word);

}


fclose(finc);

}

Dear Group

I have encountered a problem with fclose and I am wondering if anyone
could provide some insight about this problem to me. Currently, I am
working on a small personal project that is requiring for me to alter
some existing code to a program. While running the unaltered code, I
kept encountering an application (The exception unknown software
exception(0xc00000fd) etc. .. etc... etc... I am running W2K Pro).
Through a bit of debugging I singled out where the problem is
occuring. It is occuring when it attemps to close a file that had
previously been open. While tinkering with this problem, I noticed
that no error occured while closing when I manually entered the file
name into the source code. The error only occured when the user has to
key-in the file name from a command line (this is how I wish for the
program to run). What is causing this problem? Below I have provided
some source code to the function that is causing the problem. Here is
the version of the source in which no error occurs. I have set the
string, finc_name, to the file name "all.inc". Normally, the array is
set to a default of size 80 similar to fpov_name. I have also
commented out the portion of the code which prompts the user to key-in
the file name.

Thanks
Marcus D. Jacobs


#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include "numtypes.h"
/* #include "numtable.h"*/

typedef struct
{
Word x,r,g,b;
} colorinfo;
FILE *finc, *fpov, *frad;
char ch, frad_name[80], finc_name[] = "all.inc", fpov_name[80] ,
is_num;
char *pov_word;
char chpov , colorend=''f'';
colorinfo color;
Word texture[2600];
void reads_triangle(Word pov_num);
void reads_smoothtriangle(Word pov_num);
void reads_inc(void);
void reads_color(void);
void reads_texture(void);

main(void)
{
int m=1, n, j;
char chtext, finish;
char *inc_word;
int isize;

int j2;

/*
printf("Enter POV include file name ( *.inc ) : ");
gets(finc_name);
printf("\n");
*/

finc=fopen(finc_name, "r");
chtext = getc(finc);

if (finc==NULL)
{ printf(" Can''t open %s\n", finc);
exit(EXIT_FAILURE);
}
while (chtext != EOF )
{
finish =''f'';
if ( ( inc_word = malloc(25)) == NULL )
{ printf("Out of memory");
exit(EXIT_FAILURE);;
}
while (isspace(chtext)) chtext = getc(finc);
j=0;
while (chtext != EOF && isspace(chtext) == 0)
{ inc_word[j++] = chtext;
chtext = getc(finc);
}

inc_word[j] = ''\0'';

if (strcmp(inc_word, "texture") == 0 && finish == ''f'')
{ while (isspace(chtext)) chtext = getc(finc);
n=0;
chtext = getc(finc);
while ( chtext != ''}'' )
{
texture[m][n++] = chtext;
chtext = getc(finc);
}
texture[m][n] = ''\0'';
/* printf(" texture = %s ", texture[m]);*/
m++;
finish = ''t'';
}
free(inc_word);
}

fclose(finc);
}

推荐答案




这里至少有一个问题;可能还有其他人。

`finc_name''数组只有八个字符长,所以它不能
持有一个字符串超过七个字符再加上

终止''\0''。如果用户键入一个更长的名字,额外的

字符将不适合数组,并且很可能会在附近的内存中踩到
。接下来发生的事情是不可预测的,

但不太可能是一种愉快的经历。


顺便说一下,不要使用gets()。请参阅

comp.lang.c中的问题12.23常见问题(FAQ)列表




-





最后你是缺少返回int。


问候,Jens

-

_ _____ _____

| || _ _ || _ _ |

_ | | | | | |

| | _ | | | | | |

\ ___ / ens | _ | homs | _ | oerring



And finally you''re missing returning an int.

Regards, Jens
--
_ _____ _____
| ||_ _||_ _| Je***********@physik.fu-berlin.de
_ | | | | | |
| |_| | | | | | http://www.physik.fu-berlin.de/~toerring
\___/ens|_|homs|_|oerring





程序可能还有其他错误,我可以因为它太糟糕了,所以不要小心地检查它。我还建议

检查comp.lang.c的常见问题解答(谷歌会找到它),以便正确处理检查文件操作结果的方法,并且可能

几乎所有程序中的其他东西。

-

Mark Gordon

支付成为极客&高级软件开发人员

虽然我的电子邮件地址是垃圾邮件,但它是真实的,我读了它。



There are probably other errors with the program, I could not be
bothered to check it carefully since it is so bad. I would also suggest
checking the FAQ for comp.lang.c (google will find it) for the correct
way to handle checking the results of file operations and probably
almost everything else in the program.
--
Mark Gordon
Paid to be a Geek & a Senior Software Developer
Although my email address says spamtrap, it is real and I read it.


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

09-05 17:45
查看更多