c语言setvbuf不是NULL会怎样,为什么更新模式写入二进制文件不用setvbuf设置缓存...

已结贴√

问题点数:20 回复次数:5

ca56232b3bbedf9a539d07f37fffb99a.gif

3144d8b7615c79d9f638db40d5689d26.gif

a218af6549b45ee526caf607ebff1358.gif

0f8df0e29816ae721419de940fb833d1.gif

为什么更新模式写入二进制文件不用setvbuf设置缓存

一般在打开文件后,要进行缓存操作,但是下面更新没有用,是否说明更新不需要设置缓存,能否详细帮忙从原理上解答一下

#define __STDC_WANT_LIB_EXT1__ 1

#include

#include

#include

#include

#define MAXLEN  50                                    // Size of name buffer

void listfile(const char *filename);                  // List the file contents

int main(void)

{

const char *filename = "mypeople.bin";

char name[MAXLEN];                                  // Stores a name

size_t length = 0;                                  // Length of a name

int age = 0;                                        // Person's age

char answer = 'y';

FILE *pfile = NULL;

if(fopen_s(&pfile, filename, "wb+"))

{

printf_s("Failed to create file %s.\n", filename);

exit(1);

}

do

{

fflush(stdin);                                    // Remove whitespace

printf_s("Enter a name less than %d characters: ", MAXLEN);

gets_s(name, sizeof(name));                       // Read the name

printf_s("Enter the age of %s: ", name);

scanf_s(" %d", &age);                             // Read the age

// Write the name & age to file

length = strnlen_s(name, sizeof(name));           // Get name length

fwrite(&length, sizeof(length), 1, pfile);        // Write name length

fwrite(name, sizeof(char), length, pfile);        // then the name

fwrite(&age, sizeof(age), 1, pfile);              // then the age

printf_s("Do you want to enter another(y or n)?  " );

scanf_s("\n%c", &answer, sizeof(answer));

} while(tolower(answer) == 'y');

fclose(pfile);                                      // Close the file

listfile(filename);                                 // List the contents

return 0;

}

// List the contents of the binary file

void listfile(const char *filename)

{

size_t length = 0;                                  // Name length

char name[MAXLEN];                                  // Stores a name

int age = 0;

char format[20];                                    // Format string

FILE *pfile = NULL;

// Create format string for names up to MAXLEN characters

sprintf_s(format, sizeof(format), "%%-%ds Age:%%4d\n", MAXLEN);

if(fopen_s(&pfile, filename, "rb"))                 // Open to read

{

printf_s("Failed to open file %s to read it.\n", filename);

exit(1);

}

printf_s("\nThe folks recorded in the %s file are:\n", filename);

// Read records as long as we read a length value

while(fread(&length, sizeof(length), 1, pfile) == 1)

{

if(length+1>MAXLEN)

{

printf_s("Name too long.\n");

exit(1);

}

fread(name, sizeof(char), length, pfile);         // Read the name

name[length] = '\0';                              // Append terminator

fread(&age, sizeof(age), 1, pfile);               // Read the age

printf_s(format, name, age);                      // Output the record

}

fclose(pfile);

}

[/code]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值