linecount source code

#include <stdio.h>
#include <errno.h>
 /*for directroy funcgion */
#include <iostream>
#include <vector>
#include <string>
#include <iomanip>
#include <fstream>

//#define _LINUX


#ifdef _LINUX
#include <sys/types.h>
#include <dirent.h>
#endif

#ifdef _WIN32
#include <windows.h>
#endif

using namespace std;

vector<string> ReadDir(string dirName);
void PrintVec(vector<string> vecStr);
int ProcessFile(string fileName);


int main(int argc ,char **argv)
{
 int i;
 string dirName;
 vector<string> strVec;
 

 if (argc == 1)
 {
  strVec = ReadDir(".");
 }else{
  strVec = ReadDir(argv[1]);
 }
 cout << "the size of strVec is " << strVec.size()<<endl;
 for(vector<string>::iterator pos =strVec.begin();pos<strVec.end();pos++)
  ProcessFile(*pos);
 return 0;
}

void PrintVec(vector<string> vecStr){
 for(int i=0;i<vecStr.size();i++)
  cout << vecStr[i]<<endl;
}
#ifdef _LINUX
vector<string> ReadDir(string dirName)
{
 DIR* dp;
 struct dirent* ent;
 vector<string> ret;
 
 
 if((dp=opendir(dirName.c_str())) == NULL){
 
  exit(1);
 }
 
 errno = 0;
 while((ent=readdir(dp))!= NULL){
  //cout << setw(8)<< ent->d_ino <<"    "<< ent->d_name << endl;
  ret.push_back(ent->d_name);
 
 }
 
 if(errno != 0){
 
  exit(1);
 }
 
 if(closedir(dp) !=0){
 
  exit(1);
 
 }
 return ret;
}
#endif

#ifdef _WIN32
vector<string> ReadDir(string dirName)
{
   char *fileName;
   char curDir[ 256 ];
   char fullName[ 256 ];
   HANDLE fileHandle;
   WIN32_FIND_DATA findData;
   vector<string> ret;
     
   // print out the current directory name
   if( !GetFullPathName("*.*", 256, fullName,&fileName ) )
      return ret;
   cout << endl << "Directory - " << fullName << endl;
     
   // Loop through all files in the directory
   fileHandle = FindFirstFile("*.*",&findData );
   while ( fileHandle != INVALID_HANDLE_VALUE )
   {
      // If the name is a directory,
      // recursively walk it. Otherwise
      // print the file's data
      if( findData.dwFileAttributes &
         FILE_ATTRIBUTE_DIRECTORY )
      {
         //ListDirectoryContents( findData.cFileName,fileMask );
      }
      else{
         ret.push_back(findData.cFileName);
      }       
     // loop thru remaining entries in the dir
      if (!FindNextFile( fileHandle, &findData ))
         break;
   }
  
   // clean up and restore directory
   FindClose( fileHandle );
   return ret;
}
#endif
 
int ProcessFile(string fileName)
{
 // open input file
 int counter =0;
 ifstream file (fileName.c_str());
 // file opened?
 if (! file) {
  // NO, abort program
  cerr << "can't open input file /"" << fileName << "/""<< endl;
  return (0);
 }

 char c;
 while (file.get(c)) {
  if( c == '/n'){
   counter++;
  }
 }
 cout << fileName << "---->" << counter <<endl;
 return counter;
 
 
}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值