C语言第一课作业运行提示分段错误
编译完后运行,提示计算中...
Segmentation fault
请问是怎么回事?
1 #include <stdio.h>
2 #include <unistd.h>
3 #include <dirent.h>
4 #include <string.h>
5 #include <stdlib.h>
6 #include <sys/stat.h>
7
8 #define MAX 256
9
10 long total;
11
12 int countLines(const char *filename);
13 int isCode(const char *filename);
14 void findAllDirs(const char *path);
15
16 int countLines(const char *filename)
17 {
18 FILE *fp;
19 int count = 0;
20 int temp;
21
22 if ((fp = fopen(filename, "r")) == NULL)
23 {
24 fprintf(stderr, "Can not open the file: %s\n",filename);
25 return 0;
26 }
27
28 while ((temp = fgetc(fp)) != EOF)
29 {
30 if (temp == '\n')
31 {
32 count++;
33 }
34 }
35
36 fclose(fp);
37
38 return count;
39 }
40
41 int isCode(const char *filename)
42 {
43 int length;
44
45 length = strlen(filename);
46
47 if (!strcmp(filename + (length - 2), ".c"))
48 {
49 return 1;
50 }
51 else
52 {
53 return 0;
54 }
55 }
56
57 void findAllDirs(const char *path)
58 {
59 DIR *dp;
60 struct dirent *entry;
61 struct stat statbuf;
62
63 if ((dp = opendir(path)) == NULL)
64 {
65 fprintf(stderr, "The path %s is wrong!\n", path);
66 return;
67 }
68
69 chdir(path);
70 while ((entry = readdir(dp)) != NULL)
71 {
72 lstat(entry->d_name, &statbuf);
73
74 if (!strcmp(".",entry->d_name) || !strcmp(".",entry->d_name))
75 continue;
76
77 if (S_ISDIR(statbuf.st_mode))
78 {
79 findAllDirs(entry->d_name);
80 }
81 else
82 {
83 if (isCode(entry->d_name))
84 {
85 total += countLines(entry->d_name);
86 }
87 }
88 }
89
90 chdir("..");
91 closedir(dp);
92 }
93
94 int main()
95 {
96 char path = ".";
97
98 printf("计算中...\n");
99
100 findAllDirs(path);
101
102 printf("目前你总共写了 %ld 行代码!\n\n", total);
103
104 return 0;
105 }
直接复制看看
#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;
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;
} zltzlt 发表于 2020-2-26 21:15
直接复制看看
直接复制可以,我再对一下代码 C:\Users\98021\Desktop 终于找到了,74行的代码少了一个点,顺便问一下本地图片怎么发
页:
[1]