求助啊 关于小甲鱼第s1e2的课后作业 计算打了多少行代码那个程序
本帖最后由 悲伤的大象 于 2016-8-16 10:34 编辑输入完以后 gcc count_lines -o count_lines
会出现这种情况
第二次再输入就是这样的情况
这是小甲鱼老师写的,你复制一下再编译试试,我怀疑你抄错了
#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 (!strcmp(filename + (length - 2), ".c"))
{
return 1;
}
else
{
return 0;
}
}
void findAllDirs(const char *path)
{
DIR *dp;
struct dirent *entry;
struct stat statbuf;
int temp;
if ((dp = opendir(path)) == NULL)
{
fprintf(stderr, "The path %s is wrong!\n", path);
return;
}
chdir(path);
while ((entry = readdir(dp)) != NULL)
{
lstat(entry->d_name, &statbuf);
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 = ".";
printf("计算中...\n");
findAllDirs(path);
printf("目前你总共写了 %ld 行代码!\n\n", total);
return 0;
} 人造人 发表于 2016-6-10 19:08
这是小甲鱼老师写的,你复制一下再编译试试,我怀疑你抄错了
Linux下怎么复制粘贴? 悲伤的大象 发表于 2016-6-10 22:03
Linux下怎么复制粘贴?
你开的是虚拟机吧
virtualbox 可以剪贴板共享,你也可以共享文件夹 麻烦师兄帮忙看看 悬赏问题 里这个 Vbox 增强工具箱安装失败问题
Stopping VirtualBox Additions这个鬼
3Q 学习 屁哥 发表于 2016-6-12 07:48
麻烦师兄帮忙看看 悬赏问题 里这个 Vbox 增强工具箱安装失败问题
Stopping VirtualBox Additions
我也不是很懂、。我就是按照小甲鱼说的那样装的。
页:
[1]