libtestc.c

来自「Thinking in C++ 2nd edition source code 」· C语言 代码 · 共 45 行

C
45
字号
/*: C04:Libtestc.c 
// From Thinking in C++, 2nd Edition
// at http://www.BruceEckel.com
// (c) Bruce Eckel 1999
// Copyright notice in Copyright.txt
//{L} Lib
Test demonstration library */
#include <stdio.h>
#include <assert.h>
#include "Lib.h"
#define BUFSIZE 80

int main() {
  stash intStash, stringStash;
  int i;
  FILE* file;
  char buf[BUFSIZE];
  char* cp;
  /* .... */
  initialize(&intStash, sizeof(int));
  for(i = 0; i < 100; i++)
    add(&intStash, &i);
  /* Holds 80-character strings: */
  initialize(&stringStash,
             sizeof(char) * BUFSIZE);
  file = fopen("Libtestc.c", "r");
  assert(file);
  while(fgets(buf, BUFSIZE, file))
    add(&stringStash, buf);
  fclose(file);

  for(i = 0; i < count(&intStash); i++)
    printf("fetch(&intStash, %d) = %d\n", i,
           *(int*)fetch(&intStash, i));

  i = 0;
  while((cp = (char*)fetch(&stringStash,i++))!=0)
    printf("fetch(&stringStash, %d) = %s",
           i - 1, cp);
  putchar('\n');
  cleanup(&intStash);
  cleanup(&stringStash);
  return 0;
} /* ///:~ */

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?