天狼啸月
天狼神族
posts - 0,  comments - 0,  trackbacks - 0
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int CountLines(char *filename)
{
 ifstream ReadFile;
 int n=0;
 string tmp;
 ReadFile.open(filename,ios::in);
 if(ReadFile.fail())
 {
  return 0;
 }
 else
 {
  while(getline(ReadFile,tmp))
  {
   n++;
  }
  return n;
 }
 ReadFile.close();
}
string ReadLine(char *filename,int line)
{
 int lines,i=0;
 string temp;
 fstream file;
 file.open(filename,ios::in);
 lines=CountLines(filename);
 if(lines<=0)
 {
  return"Error 1:行数错误,不能为零或负数。";
 }
 if(file.fail())
 {
  return"Error 2:文件不存在";
 }
 if(line>lines)
 {
  return "Error 3:行数超出文件长度";
 }
 while(getline(file,temp)&&i<line-1)
 {
  i++;
 }
 file.close();
 return temp;
}
void main()
{
 int l;
 char filename[256];
 cout<<"请输入文件名:"<<endl;
 cin>>filename;
 cout<<"\n请输入要读取的行数:"<<endl;
 cin>>l;
 cout<<ReadLine(filename,l);
 cin.get();
 cin.get();
}
posted on 2011-12-23 09:43 烈焰之光 阅读(184) 评论(0)  编辑 收藏 引用 所属分类: 源码文件及文件夹操作

<2026年6月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

常用链接

留言簿

文章分类

文章档案

搜索

  •  

最新评论