readline.cpp

来自「eC++编译器源码」· C++ 代码 · 共 55 行

CPP
55
字号
//This module reads strings from a file a line at a time
#pragma ReadLine
#pragma qualified
#include <InOut.h>
#include <stdio.h>

void Open(Lines &line, stdio.FILE *file)  //an open file
{
  line.file = file;
  line.max = 0;
};

int Read(Lines &line, char &output[]) //-1 on eof; otherwise chars in output
{
  unsigned int count, temp;
  char c;

  count = 0;
  if (line.file==NULL) {  //stdin case
    for ( ;; ) {
       InOut.Read(c);
       if (!InOut.Done) {
         if (count==0) return -1;
         output[count] = '\n';
         return count+1;
       };
       output[count] = c;
       INC(count);
       if (c=='\n') return count;
    };
  };
  do {
    if (line.max==0) {    //if no characters, fill buffer
      temp = stdio.fread(line.buffer, 1, 512, line.file);
      if (temp == 0) {
        if (count == 0) return -1;
        output[count] = '\n';   //handle case where end-of-file has no newline
        output[count+1] = '\0'; //follow dtring representation
        return count+1;         //include \n always
      };
      line.pointer = 0;
      line.max = temp;
    };
    c = line.buffer[line.pointer];
    output[count] = c;
    INC(count);  INC(line.pointer);  DEC(line.max);
  } while (c!='\n');
  output[count] = '\0';
  return count;
};

void ReadLine(void)
{
};

⌨️ 快捷键说明

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