用C写的读取代码行数。
今天无事,在论坛上一直看贴子,很少动手实践,今天试着写了一个读取源程序代码行数的例子:
现在的代码如下,可能还有不完善的地方,以后再改进:
#include <stdio.h>
#define CHARCOUNT 255
#define CON 6 /*单行注释最少的时候*/
int realLength = 0;
/*
* function name: strCount
* function : count line number
* created by : lsl
*/
int strCount(int from, char * str)
{
int length = strlen(str), i = 0;
int realCount = length-2-from;
for(i = length-3; str[i]==' ' || str[i] ==' '; i--)
{
realCount--;
}
realLength = realCount;
return realLength;
}
int main(int argc, char *argv[])
{
FILE *fPtr = fopen("tt.c","r");
char buffer[CHARCOUNT];
int lineCount = 0,i=0,j=0,k=0,bufferLen = 0;
int moutLineComment = 0;
if(NULL == fPtr)
{
printf("file open error!\n");
exit(0);
}
while(NULL != fgets(buffer,CHARCOUNT,fPtr))
{
//如果只包含一上回车 begin
if(13 == buffer[0] && 10 == buffer[1])
{
//printf("%x%x\n",buffer[0],buffer[1]);
continue;
}
//如果只包含一个回车 end
else
{
bufferLen = strlen(buffer);//读入行的长度 算上读入末尾的 \n\r
//判断一行为空行的情况 此行可能包含许多空格 tab begin
for(i = 0; 32 == buffer[i] || 9 == buffer[i] || 13 == buffer[i] || 10 == buffer[i]; i++ )
{
if(i == bufferLen)
{
break;
}
}
if(bufferLen == i)
{
continue;
}
//判断一行为空行的情况 end
//判断//注释 begin
else if(buffer[i] == '/' && buffer[i+1] == '/')
{
continue;
}
//判断//注释 end
//判断多行注释 begin
else if(buffer[i] == '/' && buffer[i+1] == '*')
{
//**/rin tf("here\n");
if((bufferLen-i)>=CON)
{
int tmpLen = strCount(i,buffer);//求出此行从i开始除最后的空格、\r\n之外的字符数
if((tmpLen>=4) && buffer[i+tmpLen-2] == '*' && buffer[i+tmpLen-1] == '/')
{
continue;
}
else
{
相关文档:
unzip.c
中引用validate.cpp
文件中的函数来进行epub
纠错,产生的问题:
1.
validate.cpp
中使用iostream.h,
但是C
中没有这个文件
,所以产生的错误:
2>
正在编译...
2>unzip.c
2>D:\Program
Files\VC\include\cstdio(25) : error C2143:
语法错误:
缺少“{ ......
#ifndef _PPC_BOOT_STRING_H_
#define _PPC_BOOT_STRING_H_
#include <stddef.h>
extern char *strcpy(char *dest, const char *src);
extern char *strncpy(char *dest, const char *src, size_t n);
extern char *strcat(char *dest, const char *src);
extern int strcmp(const char *s1, const cha ......