流的 fscanf 类型函数?
我习惯使用 fscanf 进行简单的文件输入,因为它使事情变得简单。我正在尝试转移到流,但我希望能够做到这一点:
fscanf(file, %d %s, int1, str1);
如您所见,读取文件相对容易,将您遇到的第一个 int 粘贴到一个容器中,然后将第一个字符串粘贴到一个容器中转换为 char*。我想要的是使用流函数通过 fstreams 来完成此操作。这是我以我有限的流知识想出来的。
while((fGet = File.get() != EOF))
{
int x;
int y;
bool oscillate = false;
switch(oscillate)
{
case false:
{
x = fGet;
oscillate = true;
break;
}
case true:
{
y = fGet;
oscillate = false;
break;
}
}
}
基本上我想扫描一个文件并将第一个 int 放入 x 中,将第二个 int 放入 y 中。
正如您所知,由于某些原因,这非常糟糕,而且我从未真正使用过它,但这就是我能想到的。有更好的方法来解决这个问题吗?
I'm used to using fscanf for simple file input, because it makes it simple. I'm trying to move to streams though and I'd like to be able to do this:
fscanf(file, %d %s, int1, str1);
as you can see it's relatively easy to read through a file, stick the first int you come across into one container, and then the first string into a char*. What I want, is to do this with fstreams, using stream functions. This is what I came up with, with my limited stream knowledge.
while((fGet = File.get() != EOF))
{
int x;
int y;
bool oscillate = false;
switch(oscillate)
{
case false:
{
x = fGet;
oscillate = true;
break;
}
case true:
{
y = fGet;
oscillate = false;
break;
}
}
}
Basically I want to scan through a file and put the first int into x, and the second into y.
This is pretty bad for a few reasons, as you can tell, and I'd never actually use this but it's all I can think of. Is there a better way to go about this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要从流中读取两个整数,您所要做的就是
And 相当于
Is
And 确保如果您想检查读取是否有效,请将读取放入条件:
或
或其他条件中。
To read two integers from a stream, all you have to do is
And the equivalent of
Is
And make sure that if you want to check whether the reads worked, put the reads in the condition:
or
or whatever.