ungetc()似乎在一些角色上失败了.这是一个简单的测试程序:
#include <stdio.h>
int main(void) {
int c;
printf("Type a letter and the enter key: ");
#define TRACE(x) printf("%s -> %d\n", #x, x)
TRACE(c = getc(stdin));
TRACE(ungetc(c, stdin));
TRACE(getc(stdin));
TRACE(ungetc('\xFE', stdin));
TRACE(getc(stdin));
TRACE(ungetc('\xFF', stdin));
TRACE(getc(stdin));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我在unix系统上运行它并a Enter在提示符下输入
输出是:
Type a letter and the enter key: a
c = getc(stdin) -> 97
ungetc(c, stdin) -> 97
getc(stdin) -> 97
ungetc('\xFE', stdin) -> 254
getc(stdin) -> 254
ungetc('\xFF', stdin) -> -1
getc(stdin) -> 10
Run Code Online (Sandbox Code Playgroud)
我期待这个:
Type …Run Code Online (Sandbox Code Playgroud) ungetc只能保证一个字节的后推.另一方面,我在Windows和Linux上测试过,它似乎可以使用两个字节.
是否有任何平台(例如任何当前的Unix系统)实际上只需要一个字节?
Python中的某些文件读取(readlines())函数
将文件内容复制到内存中(作为列表)
我需要处理一个太大而
无法复制到内存中的文件,因此需要使用
文件指针(一次一个字节地访问文件
) - 就像在C getc()中一样.
我的附加要求是
我想将文件指针倒回到前面的
字节,就像在C ungetc()中一样.
有没有办法在Python中执行此操作?
另外,在Python中,我可以
使用readline()一次 读取一行
有没有办法读取前一行
后退?
我需要从一个"取消读取"字符InputStreamReader.为此目的,我想用mark和reset,但markSupported返回false的InputStreamReader类,因为它没有保持字符的内部缓冲和/或队列.
我知道BufferedInputStream和PushbackInputStream,但由于它们缓冲字节的基础上,同时也不是适当的在这里我需要的字符.
Java是否提供可以解读字符的缓冲字符阅读器?实际上,让我进一步限制,我只需要取消读取单个字符(用于超前目的).我真的需要保持自己的前瞻性吗?
是否有 C++ 版本的 ungetc?
也就是说,我可以将角色放回 istream 吗?
我是C初学者,我想连续两次调用ungetc()虽然我知道在常规C中它是不允许的.有人告诉我,我可以修改Fflush()做这项工作,但我不知道该怎么做.
这是我的代码,我Fflush唯一允许的代码,我ungetc()希望它允许两次.
#define altUngetc(c, fp) ((fp)->next > (fp)->buffer && (c) != EOF ? \
*--(fp)->next = (c) : EOF)
int altGetc(ALT_FILE *fp) {
if (fp->next == fp->buffer + fp->bufSize)
altFflush(fp);
return fp->flags & FILE_ATEOF ? EOF : *fp->next++;
}
int altFflush(ALT_FILE *fp) {
int res;
if (fp->fd < 0 || fp->flags & FILE_ATEOF)
return EOF;
if (fp->flags & FILE_READ) {
res = read(fp->fd, fp->buffer, BUF_SIZE);
if (res == 0)
fp->flags |= FILE_ATEOF;
fp->bufSize = …Run Code Online (Sandbox Code Playgroud)