[英]File reading with and without new line
In terms of an input file reading,在输入文件读取方面,
I found我发现
for (i = 0; !inStream.eof() ; i++) inStream >> input[i];
tries to read one more time if there is a "new line" at the end of file.如果文件末尾有“新行”,则尝试再读一次。
for (i=0; inStream >> input[i]; i++ ) ;
seems to work whether there is a new line or not at the end of file .无论文件末尾是否有新行,似乎都有效。
Are there any other neat solution for handling a "new line" at the end of file ?是否有其他巧妙的解决方案来处理文件末尾的“新行”?
in c, I write在c中,我写
FILE *fp = fopen("file", "r") ; for (i=0; fscanf(fp, "%d", & input[i]) > 0 ; i++ ) ;
Are there any way I can use fscanf
with c++ input file stream not using fopen()
?有什么方法可以将
fscanf
与不使用fopen()
C++ 输入文件流一起使用吗?
The C++ library provides you with a handy getline
function. C++ 库为您提供了一个方便的
getline
函数。 Here is a minimal example from cplusplus.com这是来自cplusplus.com 的一个最小示例
// reading a text file
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main () {
string line;
ifstream myfile ("example.txt");
if (myfile.is_open())
{
while ( getline (myfile,line) )
{
cout << line << '\n';
}
myfile.close();
}
else cout << "Unable to open file";
return 0;
}
any way I can use
fscanf
with c++ input file stream?有什么办法可以将
fscanf
与 C++ 输入文件流一起使用?
Possibly.可能。 Both
g++
and Visual Studio have had their own extenstions making it possible. g++
和 Visual Studio 都有自己的扩展使其成为可能。 If you find such possibilities, they are non-standard and not portable.如果您发现此类可能性,则它们是非标准的且不可移植。 I advice you to find other ways.
我建议你寻找其他方法。
If you use C++, try to use the most of it.如果您使用 C++,请尽量使用它。
C
does have fscanf
which looks neat - but it's a beast. C
确实有fscanf
看起来很整洁 - 但它是一头野兽。 C++ has std::scanf
for portability - and it's the same kind of beast. C++ 具有用于可移植性的
std::scanf
- 它是同一种野兽。 Don't use it if you are programming C++.如果您正在编写 C++,请不要使用它。
The problem you're trying to avoid你试图避免的问题
for (i = 0; !inStream.eof() ; i++) inStream >> input[i];
tries to read one more time if there is a "new line" at the end of file.如果文件末尾有“新行”,则尝试再读一次。
is caused by improper use of eof()
.是由不当使用
eof()
引起的。 eof()
returns true
after you've tried to read beyond end of file. eof()
在您尝试读取文件末尾后返回true
。 A better loop would look like:更好的循环如下所示:
for (i = 0; inStream >> input[i] ; i++);
Notice how the extraction and condition became one?注意提取和条件是如何变成一的? They aren't.
他们不是。 The extraction happens first and returns the stream used in the extraction.
提取首先发生并返回提取中使用的流。 The stream has a
bool
overload that tells you if it's in a failed state or not, making it great for this kind of checking: if(stream >> variable) { /* success */ } else { /* fail */ }
.流有一个
bool
重载,告诉你它是否处于失败状态,非常适合这种检查: if(stream >> variable) { /* success */ } else { /* fail */ }
。
This only works if your array has entries enough to store all input though.这仅在您的数组具有足以存储所有输入的条目时才有效。 Your loop does not check that.
你的循环不检查。 If you use a
vector
you can just read elements and push_back
to make it work a long long time.如果您使用
vector
您只需读取元素和push_back
即可使其长时间工作。 If you have a fixed (relatively small) size and do the above, it'll fail eventually.如果你有一个固定的(相对较小的)大小并执行上述操作,它最终会失败。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.