【发布时间】:2015-02-22 23:16:39
【问题描述】:
在 C 语言中,rewind 函数用于将流的位置设置为最开始。我想问一下是否有一个等效的函数可以将流位置向左移动一个标记。
例如,我有一个名为 FooFile.txt 的文件,其中包含几行整数序列,由 " " 空格字符分隔。
int main()
{
// open file stream.
FILE *FooFile = fopen("FooFile.txt" , "r");
int Bar = 0;
// loop through every integer token in the file stream.
while ( fscanf( FooFile, "%d", &Bar ) == 0 )
{
// I don't want to reset the stream to the very beginning.
// rewind( FooFile );
// I only need to move the stream back one token.
Bar = fscanf ( FooFile, "%d", &Bar )
Bar = fscanf ( FooFile, "%d", &Bar )
}
}
【问题讨论】:
-
见
fgetpos和fsetpos。祝你好运。 -
对于 1 个字符的情况,
ungetc()应该可以工作。