马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
code#include <stdio.h>
#include <unistd.h>
#include <dirent.h>
#include <string.h>
#include <stdlib.h>
#include <sys/stat.h>
#define MAX 256
long total;
int countLines(const char *filename);
int isCode(const char *filename);
void findAllDirs(const char *path)
int countLines(const char *filename)
{
FILE *fp;
int count = 0;
int temp;
if((fp = fopen(filename, 'r')) == NULL)
{
fprintf(stderr, "Can not open the file:%s\n", filename);
return 0;
}
while ((temp) = fgetc(fp) != EOF)
{
if (temp == '\n')
{
count++;
}
}
fclose(fp);
return count;
}
int isCode(const char *filename)
{
int length;
length = strlen(filename);
if (!strcamp(filename + (length-2), ".c"))
{
return 1;
}
else
{
return 0;
}
}
void findAllDirs(const char *path)
{
DIR *dp;
struct dirent *entry;
struct stat statbuf;
if ((dp = opendir(path)) == NULL)
{
fprintf(strerr, "The path %s is wrong!\n", path);
return;
}
chdir(path);
while ((entry = reddir(dp)) != NULL)
{
lstat(entry->d_name, &stabuf);
if (!strcmp(".", entry->d_name) || !strcmp("..", entry->d_name))
continue;
if (S_ISDIR(statbuf.st_mode))
{
findAllDirs(entry->d_name);
}
else
{
if (isCode(entry->d_name))
{
total += countLines(entry->d_name);
}
}
}
chdir("..");
closedir(dp);
}
int main()
{
char path[MAX] = '.';
printf("strat...\n");
findAlllDirs(path);
printf("%ld", total);
return 0;
}
errorsle1.c: In function ‘findAllDirs’:
sle1.c:16:38: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
int countLines(const char *filename) {
^
sle1.c:40:34: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
int isCode(const char *filename) {
^
sle1.c:55:36: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
void findAllDirs(const char *path) {
^
sle1.c:91:12: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
int main() {
^
sle1.c:101:1: error: expected ‘{’ at end of input
}
14 void findAllDirs(const char *path) //少分号
22 if((fp = fopen(filename, 'r')) == NULL) // "r"
47 if (!strcamp(filename + (length-2), ".c")) //strcmp
65 fprintf(strerr, "The path %s is wrong!\n", path); //stderr
你可以看下别人的经验:
http://blog.163.com/njut_wangjian/blog/static/16579642520121022112940349/
抱歉,我没在linux环境下编过程,没办法帮你调试。最好对着小甲鱼老师的一行一行看
fighting!!!
|