x*****u 发帖数: 3419 | 1 我想读某数据文件中最后一行第二个数,C(++)有没有简单易行的法子?
谢谢! | s******r 发帖数: 21 | 2
Here is my solution. It is not necessarily the simplest one.
I use f.seek(-60, ios::end) here, assuming:
1. the data file is in Text Format;
2. the last 2nd number is always within the last 60 bytes of the data file.
The last 2nd element in the vector v is what you want, then.
#include
#include
#include
#include
using namespace std;
int main(void){
ifstream f("1.data");
f.seekg(-60, ios::end);
vector v((istream_iterator(f)), istream_itera
【在 x*****u 的大作中提到】 : 我想读某数据文件中最后一行第二个数,C(++)有没有简单易行的法子? : 谢谢!
| x*****u 发帖数: 3419 | 3 感谢! 但感觉这个-60不好把握,多了少了都成问题。
用getline应该更稳妥一点。
1 // istringstream::str
2 #include
3 #include
4 #include
5 #include
6 using namespace std;
7
8 int main () {
9
10 double val;
11
12 string file_name("Tnve.dat");
13 ifstream infile(file_name.c_str(),ios::in);
14 istringstream iss;
15 string strvalues,sv;
16
【在 s******r 的大作中提到】 : : Here is my solution. It is not necessarily the simplest one. : I use f.seek(-60, ios::end) here, assuming: : 1. the data file is in Text Format; : 2. the last 2nd number is always within the last 60 bytes of the data file. : The last 2nd element in the vector v is what you want, then. : #include : #include : #include : #include
|
|