当我使用c++处理文件时,我发现文件末尾总是有一个空行。有人说vim会在文件末尾附加一个'\ n',但是当我使用gedit时,它也有同样的问题。谁能告诉我原因?
1 #include<iostream>
2 #include<fstream>
3
4 using namespace std;
5 const int K = 10;
6 int main(){
7 string arr[K];
8 ifstream infile("test1");
9 int L = 0;
10 while(!infile.eof()){
11 getline(infile, arr[(L++)%K]);
12 }
13 //line
14 int start,count;
15 if (L < K){
16 start = 0;
17 count = L;
18 }
19 else{
20 start = L % K;
21 count = K;
22 }
23 cout << count << endl;
24 for (int i = 0; i < count; ++i)
25 cout << arr[(start + i) % K] << endl;
26 infile.close();
27 return 1;
28 }
while test1 file just:
abcd
but the program out is :
2
abcd
(upside is a blank line)