1.输出倒数第二行
#include
"stdafx.h"
#include <iostream>
#include <string>
#include
<fstream>
using namespace std;
int main(){
//cin.imbue(locale("chs"));
cout.imbue(locale("chs"));//控制台输出中文
int
i,len=1;
char ch=NULL;
ifstream
infile("aaa.txt");
while(ch!=EOF){
ch=infile.get();
if(ch==‘\n‘){
len++;//判断有几行,当然也可用getline来判断
}
}
cout<<len<<endl;
infile.close();
infile.open("aaa.txt");//需要重新打开文件,否则指针已到文件末尾
string
temp;
for(i=0;i<len-1;i++){//输出倒数第2行
getline(infile,temp,‘\n‘);
}
cout<<temp<<endl;
system("pause");
return
0;
}
2.逆序输出
#include
"stdafx.h"
#include <iostream>
#include <string>
#include
<fstream>
using namespace std;
int main(){
//cin.imbue(locale("chs"));
cout.imbue(locale("chs"));//控制台输出中文
int
i=0,len=0;
char ch=‘a‘;
ifstream
infile("aaa.txt");
while(ch!=EOF){
ch=infile.get();
len++;
}
infile.close();
infile.open("aaa.txt");
while(i<=len){
infile.seekg(-i-1,ios::end);
ch=infile.get();
cout<<ch;
i++;
}
system("pause");
return 0;
}
原文:http://www.cnblogs.com/duyy/p/3714168.html