-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetLineNumber.h
More file actions
38 lines (34 loc) · 863 Bytes
/
getLineNumber.h
File metadata and controls
38 lines (34 loc) · 863 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#pragma once
#include <iostream>
#include <fstream>
#include <string>
#include"checkfp.h"
using namespace std;
int getLineNumber(string filename)
{
ifstream ReadFile;
int n = 0;
string tmp;
ReadFile.open(filename, ios::in);//ios::in 表示以只读的方式读取文件
checkfp(ReadFile);
while (getline(ReadFile, tmp, '\n'))
{
n++;
}
ReadFile.close();
return n;
}
int getLineNumber(string filename,string endLine)
{
ifstream ReadFile;
int n = 0;
string tmp;
ReadFile.open(filename, ios::in);//ios::in 表示以只读的方式读取文件
checkfp(ReadFile);
while (getline(ReadFile, tmp, '\n') && tmp != endLine)//endLine若为空,则:每次getline将读到的行赋给tmp,当读到空行,将tmp与endLine一比,发现相同就退出。
{
n++;
}
ReadFile.close();
return n;
}