简单 测试出 代码行数

zhoubenchi20 2008-06-23 06:05:56
总往txt里写代码
想实现测试出代码的行数
函数测出有代码,就n++

格式:chengxu(程序名) a.txt(目录下的txt文件)


各位能提及下有什么函数麽
...全文
151 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
simo110 2008-06-25
  • 打赏
  • 举报
回复
刚刚写了一个,有bug。
明天再完善一下吧。

/********************************************************************
created: 2008/06/25
created: 25:6:2008 0:19
filename: E:\CODING\CodeLineTotal\codeLineTotal.cpp
file path: E:\CODING\CodeLineTotal
file base: codeLineTotal
file ext: cpp
author: hecan

purpose: count the line of code
*********************************************************************/
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main(int argc, char *argv[])
{
if (argc == 1)
{
cout << "programName fileName" <<endl;
return -1;
}

int iCodeLineNum = 0;

ifstream ifsFile(argv[1], ios::in);
if (!ifsFile)
{
cerr << "Can't open file!" << endl;
return -1;
}

while (!ifsFile.eof())
{
string sLine;
getline(ifsFile, sLine);

if (sLine == string(""))
{
continue;
}

//====================================================================================
if (sLine.find("//") == static_cast< string::size_type >(0))//去掉以"//"开头的的注释行
{
continue;
}
//====================================================================================

///////////////////////////////////////////////////////////////////////////////////////
if (sLine.find("/*")==static_cast< string::size_type >(0)) //去掉以"/**/"包含的注释段
{
getline(ifsFile, sLine);

while (!ifsFile.eof() &&
(sLine.find("*/") != string::npos) )//未找到结束符"*/"
{
getline(ifsFile, sLine);
}
continue;
}
//////////////////////////////////////////////////////////////////////////////////////////

iCodeLineNum++;

/////////////================DEBUG=============/////////////
cout << sLine << endl;
/////////////================DEBUG=============/////////////

}

cout << "The total line(Don't include the comment): " << iCodeLineNum << endl;

return 0;

}
zhoubenchi20 2008-06-25
  • 打赏
  • 举报
回复
基本解决了,送分~
其他问题的自己耐心看
jy02684358 2008-06-25
  • 打赏
  • 举报
回复
if(ch='\n')
5楼代码。
这句错了其他都对,是==不是=。
zhoubenchi20 2008-06-24
  • 打赏
  • 举报
回复
#include <stdio.h>
int main(int argc,char *argv[])
{
FILE *fp;
char ch;
int n=0;
fp=fopen("1.txt","r");
while((ch=fgetc(fp))!=EOF)
{
if(ch='\n')
n++;
}
printf("行数为%d",n);
fclose(fp);
return 0;
}
zhoubenchi20 2008-06-24
  • 打赏
  • 举报
回复
//LS的共享下啊 大家肯定能看懂的 ~
//今天上嵌入式课 写的 肯定有错误 大家帮看看 我没机器调试 哎。
#include <stdio.h>
main(int argc,char *argv[])
{
FILE *fp;
char ch;
int n=0;
fp=("1.txt","r");
ch=fgetc(fp);
while(ch!=EOF)
{
if(ch='\n')n++;
ch=fgetc(fp);
}
printf("行数为%d",n);
fclose(fp);
}


redjuly 2008-06-24
  • 打赏
  • 举报
回复
用getline吧
詹天佐 2008-06-24
  • 打赏
  • 举报
回复
没调试,感觉应该就是这个样子
太乙 2008-06-23
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 k2eats 的回复:]
楼主什么意思,测试行数读取 '\n'的个数不就行了吗?
要专门的函数吗
[/Quote]

很好的方法~~不过效率会不会有点儿低啊?


我有个程序,老师给的,不过我没看懂他是怎么计算行数的~~
zhoubenchi20 2008-06-23
  • 打赏
  • 举报
回复
恩 以\r\n为判断,有多少个\r\n就有多少行 呵呵

不好意思 有点仓促了~
K行天下 2008-06-23
  • 打赏
  • 举报
回复
楼主什么意思,测试行数读取 '\n'的个数不就行了吗?
要专门的函数吗

3,881

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 其它技术问题
社区管理员
  • 其它技术问题社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧