69,336
社区成员
发帖
与我相关
我的任务
分享
代码1
my_file = fopen("11.txt", "w");
fprintf(my_file,"start!\n");
//fclose(my_file);//在此处不关闭文件
my_file = fopen("11.txt", "a");
fprintf(my_file,"my file\n");
fclose(my_file);
代码2
my_file = fopen("11.txt", "w");
fprintf(my_file,"start!\n");
fclose(my_file);//在此处不关闭文件
my_file = fopen("11.txt", "a");
fprintf(my_file,"my file\n");
//fclose(my_file);
代码3
my_file = fopen("11.txt", "w");
fprintf(my_file,"start!\n");
//fclose(my_file);//在此处不关闭文件
my_file = fopen("11.txt", "a");
fprintf(my_file,"my file\n");
//fclose(my_file);
代码4
my_file = fopen("11.txt", "a");//此处也已追加方式
fprintf(my_file,"start!\n");
//fclose(my_file);//在此处不关闭文件
my_file = fopen("11.txt", "a");
fprintf(my_file,"my file\n");
fclose(my_file);
gcc和VC中:
struct _iobuf {
char *_ptr; //文件输入的下一个位置
int _cnt; //当前缓冲区的相对位置
char *_base; //指基础位置(即是文件的其始位置)
int _flag; //文件标志
int _file; //文件的有效性验证
int _charbuf; //检查缓冲区状况,如果无缓冲区则不读取
int _bufsiz; //文件的大小
char *_tmpfname; //临时文件名
};
typedef struct _iobuf FILE;
TC中:
typedef struct {
short level; /* fill/empty level of buffer */
unsigned flags; /* File status flags */
char fd; /* File descriptor */
unsigned char hold; /* Ungetc char if no buffer */
short bsize; /* Buffer size */
unsigned char *buffer; /* Data transfer buffer */
unsigned char *curp; /* Current active pointer */
unsigned istemp; /* Temporary file indicator */
short token; /* Used for validity checking */
} FILE; /* This is the FILE object */
虽然各个编译器对FILE的定义不大相同,但是FILE结构通常会包含:
一个文件位置指示器,指向流的当前位置,错误指示器,文件结尾指示器,
一个指向缓冲区起始处的指针,一个文件标识符和一个记录实际复制到缓冲
区中的字节数的计数器等。
代码1
my_file = fopen("11.txt", "w");
fprintf(my_file,"start!\n");
//fclose(my_file);//在此处不关闭文件
my_file = fopen("11.txt", "a");
fprintf(my_file,"my file\n");
fclose(my_file);
比如上面的代码为什么得到的是
start!
打开文件和关闭文件都是比较耗时的。
如果可以不关闭也没有多大关系的!
只要你在下次对文件进行操作的时候注意文件流句柄的位置就好,
防止覆写或者读到空白数据就好!