FCLOSE的问题 [英] Problem with FCLOSE

查看:78
本文介绍了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);
}

推荐答案

Marcus Jacobs写道:
Marcus Jacobs wrote:
[...]
char ch,frad_name [80 ],finc_name [] =" all.inc",fpov_name [80],
[...]
获取(finc_name);
[...]
char ch, frad_name[80], finc_name[] = "all.inc", fpov_name[80] ,
[...]
gets(finc_name);




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

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

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

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

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


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

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

http://www.eskimo.com/~scs/C-faq/top.html


-
Er ********* @ sun.com


Marcus Jacobs< ma ******* @ hotmail.com>写道:
Marcus Jacobs <ma*******@hotmail.com> wrote:
我遇到了fclose的问题,我想知道是否有人可以向我提供一些关于这个问题的见解。目前,我正在开展一个小型的个人项目,要求我将一些现有的代码改为程序。在运行未经修改的代码时,我不断遇到一个应用程序(异常未知软件异常(0xc00000fd)等等等...等...我正在运行W2K Pro)。
通过一些调试,我挑出了问题出现的地方。当它试图关闭之前已打开的文件时,它正在发生。在修补这个问题的同时,我注意到当我手动将文件名称输入源代码时关闭时没有发生错误。只有当用户必须从命令行键入文件名时才会出现错误(这是我希望
程序运行的方式)。是什么导致了这个问题?下面我已经为导致问题的函数提供了一些源代码。这是没有错误发生的源版本。我已将
字符串finc_name设置为文件名all.inc。通常,数组设置为大小为80的默认值,类似于fpov_name。我还注释了代码中提示用户输入文件名的部分。


#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 main(void)


因为main()应该返回一个int。

{
int m = 1,n,j;
char chtext,finish;
char * inc_word;
int isize;
int j2;
/ *
printf("输入POV包含文件名(* .inc):");


你最好加在这里


fflush(stdout);


或者用户可能不会看到请求。

得到(finc_name);


永远不要使用gets()。您永远无法确定

用户输入的文件名不会超过您提供的

缓冲区的长度。使用fgets()。

printf(" \ n");
* /
finc = fopen(finc_name," r");
chtext = getc(finc);

if(finc == NULL)
{/> printf(不能打开%s \ n,finc);
退出(EXIT_FAILURE);
}


您需要在调用getc()之前检查fopen()是否成功*。

你必须让''chtext''成为一个int,而不是一个char。否则你就不会有b $ b能够检查EOF。 EOF是getc()的返回值,它不能存储在char中。
while(chtext!= EOF)
{
finish =''f'';
if((inc_word = malloc(25))== NULL)
{
printf(Out of memory);
exit(EXIT_FAILURE);;
}
while(isspace(chtext))chtext = getc(finc);


当getc()返回EOF时应该发生什么,例如:因为

输入文件坏了,看起来不像你期望的那样?

j = 0;
while(chtext!= EOF&& isspace( chtext)== 0)
{
inc_word [j ++] = chtext;
chtext = getc(finc);
}


你永远不会检查你连续不超过24个字符。

如果发生这种情况你写过你分配的内存的末尾。

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

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;


''texture''被声明为''Word''的数组(不管是什么'

')。除非''Word''是某种数组(chars?)而不仅仅是指向数组的指针,这看起来很可疑,至少可以这么说。即使假设

''Word''是'typedef''是一系列字符你也不会检查

你不会写过它结束。如果你做了什么可以发生。


最后,既然你从m = 1开始,为什么你不想使用

第一个元素''texture''?
chtext = getc(finc);
}
texture [m] [n] =''\''';
/ * printf(" texture =%s",texture [m]); * /
m ++;
finish =''t'';
}


而且你还没有确定你没有读到比你声明的2600''Word'更多元素到

''texture'' 。

免费(inc_word);
}

fclose(finc);


你可能已经写入了一些你不拥有的内存,

覆盖文件指针的一些重要结构。并且

现在你得到了你应得的东西;-)你可能运气不好

当你注释掉东西时会出现错误

来自用户的文件名,因为可执行文件的布局不同。但这并不意味着你的程序是正确的。

}
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.
#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 main( void )

since main() is supposed to return an int.
{
int m=1, n, j;
char chtext, finish;
char *inc_word;
int isize; int j2; /*
printf("Enter POV include file name ( *.inc ) : ");
You better add here

fflush( stdout );

or the user may not see the request.
gets(finc_name);
Never, ever use gets(). You will never be able to ascertain that the
user does not enter a file name that''s longer than the length of the
buffer you supply. Use fgets().
printf("\n");
*/ finc=fopen(finc_name, "r");
chtext = getc(finc);

if (finc==NULL)
{
printf(" Can''t open %s\n", finc);
exit(EXIT_FAILURE);
}
You need to check if fopen() was successful *before* you call getc().
And you must make ''chtext'' an int, not a char. Otherwise you won''t be
able to check for EOF. EOF is a return value of getc() that can''t be
stored in a char.
while (chtext != EOF )
{
finish =''f'';
if ( ( inc_word = malloc(25)) == NULL )
{
printf("Out of memory");
exit(EXIT_FAILURE);;
}
while (isspace(chtext)) chtext = getc(finc);
What is supposed to happen when getc() returns EOF, e.g. because the
input file is broken and does not look like you expect it?
j=0;
while (chtext != EOF && isspace(chtext) == 0)
{
inc_word[j++] = chtext;
chtext = getc(finc);
}
You never check that you don''t get more than 24 characters in a row.
If this happens you write past the end of the memory you allocated.
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;
''texture'' is declared as an array of ''Word'' (whatever that''s supposed
to be). Unless ''Word'' is some kind of array (of chars?) and not just a
pointer to an array this looks fishy, to say the least. Even assuming
that ''Word'' is typedef''ed to be an array of chars you don''t check that
you don''t write past its end. And if you do anything can happen.

Finally, since you start with m = 1, why don''t you want to use the
first element of ''texture''?
chtext = getc(finc);
}
texture[m][n] = ''\0'';
/* printf(" texture = %s ", texture[m]);*/
m++;
finish = ''t'';
}
And you also don''t make sure that you don''t read more elements into
''texture'' than the 2600 ''Word''s you declared.
free(inc_word);
}

fclose(finc);
You probably have been written into some memory you don''t own,
overwriting some important structure for the file pointer. And
now you get what you deserve ;-) You might be unlucky in not
getting an error when you comment out the stuff for getting
the file name from the user because of a different layout of
the executable. But that does not mean that your program is
correct.
}




最后你是缺少返回int。


问候,Jens

-

_ _____ _____

| || _ _ || _ _ | Je *********** @ physik.fu-berlin.de

_ | | | | | |

| | _ | | | | | | http://www.physik.fu-berlin.de/~toerring

\ ___ / 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


2003年8月5日14:53:20 -0700
ma*******@hotmail.com (Marcus Jacobs)写道:
On 5 Aug 2003 14:53:20 -0700
ma*******@hotmail.com (Marcus Jacobs) wrote:
亲爱的小组

我遇到了fclose的问题,我想知道是否有人可以向我提供有关此问题的一些见解。目前,我正在开展一个小型的个人项目,要求我将一些现有的代码改为程序。在运行未经修改的代码时,我不断遇到一个应用程序(异常未知软件异常(0xc00000fd)等等等...等...我正在运行W2K Pro)。
通过一些调试,我挑出了问题出现的地方。当它试图关闭之前已打开的文件时,它正在发生。在修补这个问题的同时,我注意到当我手动将文件名称输入源代码时关闭时没有发生错误。只有当用户必须从命令行键入文件名时才会出现错误(这是我希望
程序运行的方式)。是什么导致了这个问题?下面我已经为导致问题的函数提供了一些源代码。这是没有错误发生的源版本。我已将
字符串finc_name设置为文件名all.inc。通常,数组设置为大小为80的默认值,类似于fpov_name。我还注释了部分代码,提示用户输入文件名。

谢谢

Marcus D.雅各布斯

#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];
你还没有向我们展示Word的定义,所以我们不知道

纹理是什么类型。

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 main(void)

{
int m = 1,n,j;
char chtext,finish;
char * inc_word;
int isize;

int j2;

/ *
printf("输入POV包含文件名(* .inc):") ;
获取(finc_name);
如果取消注释,如果用户输入的文件名长于

缓冲区,那么你刚刚炸毁了你的程序。

printf(" \ n");
* /

finc = fopen(finc_name," r");
chtext = getc(finc);
当然这应该是在你检查了开放工作之后?否则

* BANG *

if(finc == NULL)
{printf("不能打开%s \ n,finc) ;
退出(EXIT_FAILURE);
}
while(chtext!= EOF)
EOF可能不适合char,因此chtext应该被声明为

int而不是char。

{
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] =''\''';
如果输入了一个25个字符(或更长)的单词,你只需要再次烧掉
。您可能想要限制上述循环或使用realloc。

if(strcmp(inc_word," texture")== 0&& finish ==''f'')
{while(isspace(chtext))chtext = getc(finc);
n = 0;
chtext = getc(finc);
while(chtext!=''}'' )
{
纹理[m] [n ++] = chtext;
chtext = getc(finc);
}
再次,不检查是否通过了结束数组。另外你不是在
检查getc是否成功。

texture [m] [n] =''\''';
/ * printf (" texture =%s",texture [m]); * /
m ++;
finish =''t'';
}
free(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]; You have not shown us the definition of Word, so we don''t know what type
texture is.

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 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); When this is uncommented, if the user enters a file name longer than the
buffer you have just blown up your program.
printf("\n");
*/

finc=fopen(finc_name, "r");
chtext = getc(finc); Surely this should be after you''ve checked the open worked? Otherwise
*BANG*
if (finc==NULL)
{ printf(" Can''t open %s\n", finc);
exit(EXIT_FAILURE);
}
while (chtext != EOF ) EOF might not fit in a char so chtext should have been declared as an
int rather than a char.
{
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 a 25 character (or longer) word was entered you''ve just blown things
up again. You might want to either limit the above loop or use realloc.
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);
} Again, not check on going passed the end of the array. Also you are not
checking to see if getc succeeds.
texture[m][n] = ''\0'';
/* printf(" texture = %s ", texture[m]);*/
m++;
finish = ''t'';
}
free(inc_word);
}

fclose(finc);
}




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

检查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的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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