blockio.c

来自「关于LDPC编/译码的方针平台。能随机产生信源和模拟高斯信道。」· C语言 代码 · 共 78 行

C
78
字号
/* BLOCKIO.C - Routines to read/write blocks of bits from/to a text file. *//* Copyright (c) 2000 by Radford M. Neal  * * Permission is granted for anyone to copy, use, or modify this program  * for purposes of research or education, provided this copyright notice  * is retained, and note is made of any changes that have been made.  * * This program is distributed without any warranty, express or implied. * As this program was written for research purposes only, it has not been * tested to the degree that would be advisable in any important application. * All use of this program is entirely at the user's own risk. */#include <stdlib.h>#include <stdio.h>#include "blockio.h"/* READ A BLOCK OF BITS.  The bits must be given as '0' or '1' characters,   with whitespace allowed (but not required) between bits.  Returns 0 if   a block is read successfully, and EOF if eof or an error occurs.  If   EOF is returned, a warning will be printed if a partial block had already   been read. */int blockio_read( FILE *f,    /* File to read from */  char *b,    /* Place to store bits read */  int l       /* Length of block */){  int i, c;  for (i = 0; i<l; i++)  {     do    { c = getc(f);      if (c==EOF)       { if (i>0)        { fprintf(stderr,           "Warning: Short block (%d long) at end of input file ignored\n",i);        }        return EOF;      }    } while (c==' ' || c=='\t' || c=='\n' || c=='\r');    if (c!='0' && c!='1')    { fprintf(stderr,"Bad character in binary file (not '0' or '1')\n");      exit(1);    }    b[i] = c=='1';  }  return 0;}/* WRITE A BLOCK OF BITS.  Bits are written as '0' and '1' characters, with   no spaces between them, followed by a newline. */void blockio_write( FILE *f,     /* File to write to */  char *b,     /* Block of bits to write */  int l        /* Length of block */){   int i;  for (i = 0; i<l; i++)  { if (b[i]!=0 && b[i]!=1) abort();    putc("01"[b[i]],f);  }  putc('\n',f);}

⌨️ 快捷键说明

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