天狼啸月
天狼神族
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 temp;
 ReadFile.open(filename,ios::in);                        //表示以只读方式方式读取文件
 if(ReadFile.fail())
  return 0;
 else                                                     //文件存在,返回文件行数
 {
  while(getline(ReadFile,temp))
  {
   n++;
  }
  return n;
 }
 ReadFile.close();
}
void main()
{
 ifstream file;
 int LINES;
 char filename[512];
 cout<<"请输入要打开的文件名:"<<endl;
 cin>>filename;
 file.open(filename,ios::in);
 if(file.fail())
 {
  cout<<"文件不存在"<<endl;
  file.close();
  cin.get();
 }
 else                                                         //文件存在
 {
  LINES=CountLines(filename);
 
  int *tc=new int[LINES];
  char *t=new char[LINES];
  int i=0;
  while(!file.eof())                                        //读取数据到数组
  {
   file>>tc[i];
   file>>t[i];
   i++;
   cout<<LINES<<endl;
  }
  file.close();
   cout<<LINES<<endl;
  for(i=0;i<LINES;i++)
   cout<<tc[i]<<"\t"<<t[i]<<endl;
   cin.get();
   cin.get();
 }
}
posted on 2011-12-23 09:45 烈焰之光 阅读(171) 评论(0)  编辑 收藏 引用 所属分类: 源码文件及文件夹操作

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

常用链接

留言簿

文章分类

文章档案

搜索

  •  

最新评论