通过线C和C ++读书线? [英] Line by line reading in C and C++?
本文介绍了通过线C和C ++读书线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想从C或C ++文件中读取一行行了,我知道该怎么做,当我假定行的一些固定的大小,但有一个简单的方法以某种方式计算或获得所需的准确大小对于行或文件中的所有行? (按字读字,直到换行也是为我好,如果任何人都可以做到这样。)
I want to read line by line from a file in C or C++, and I know how to do that when I assume some fixed size of a line, but is there a simple way to somehow calculate or get the exact size needed for a line or all lines in file? (Reading word by word until newline is also good for me if anyone can do it that way.)
推荐答案
如果您使用的是流阅读器,这一切都将在你被隐藏。参见函数getline
。下面的例子是基于从这里中的code 。
If you use a streamed reader, all this will be hidden from you. See getline
. The example below is based from the code here.
// getline with strings
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main () {
string str;
ifstream ifs("data.txt");
getline (ifs,str);
cout << "first line of the file is " << str << ".\n";
}
这篇关于通过线C和C ++读书线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文