| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2078 人关注过本帖
标题:为什么更新模式写入二进制文件不用setvbuf设置缓存
取消只看楼主 加入收藏
foxeer
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:107
专家分:136
注 册:2015-12-29
结帖率:90%
收藏
已结贴  问题点数:20 回复次数:3 
为什么更新模式写入二进制文件不用setvbuf设置缓存
一般在打开文件后,要进行缓存操作,但是下面更新没有用,是否说明更新不需要设置缓存,能否详细帮忙从原理上解答一下
#define __STDC_WANT_LIB_EXT1__ 1
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>

#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]
搜索更多相关主题的帖子: include 二进制 file 
2016-08-29 12:09
foxeer
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:107
专家分:136
注 册:2015-12-29
收藏
得分:0 
回复 2楼 rjsp
你看看我贴上的程序,并没有setvbuf,难道fopen有默认缓存吗???
2016-08-29 12:17
foxeer
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:107
专家分:136
注 册:2015-12-29
收藏
得分:0 
回复 2楼 rjsp
能否将上述程序解释一下,谢谢
2016-08-29 12:18
foxeer
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:107
专家分:136
注 册:2015-12-29
收藏
得分:0 
回复 5楼 ehszt
吧fflush(stdin)换成rewind(stdin)
2016-08-29 13:39
快速回复:为什么更新模式写入二进制文件不用setvbuf设置缓存
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.061661 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved