求助,这行代码不知道出什么问题了
if ((fp = fopen(filename, "r")) == NULL) 那个"r"不应该用单引号包起来吗?还有,发完整代码 本帖最后由 Husing 于 2020-8-5 17:42 编辑
qiuyouzhi 发表于 2020-8-5 17:34
那个"r"不应该用单引号包起来吗?
还有,发完整代码
#include <io.h>
#include <direct.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 256
long total;
int countlines(const char *filename);
void findAllCodes(const char *path);
void findALLFiles(const char *path);
int countLines(const char *filename)
{
FILE *fg;
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;
}
void findAllCodes(const char *path)
{
struct _finddata_t fa;
long handle;
char thePath, target;
strcpy(thepath, path);
if((hande = _findfirst(strcat(thePath, "/*.c"), &fa)) != -1L)
{
do
{
sprintf(target, "%s/%s", path, fa.name);
total += countLines(target);
}while (_findnext(handle, &fa) == 0);
}
_findclose(handle);
}
void findALLDirs(const char *path)
{
struct _finddata_t fa;
long handle;
char thePath;
strcpy(thePath, path);
if((handle = _findfirst(strcat(thePath, "/*"), &fa)) == -1L)
{
fprintf(stderr, "The path %s is wrong!\n",path);
return;
}
do
{
if (!strcmp(fa.name, ".") || !strcmp(fa.name, ".."))
continue;
if ( fa.attrib == _A_SUBDIR)
{
sprintf(thePath, "%s/%s", path, fa.name);
findAllCodes(thePath);
findALLDirs(thePath);
}
}while (_findnext(handle, &fa) == 0);
_findclose(handle);
}
int main()
{
char path = ".";
printf("计算中...\n");
findAllCodes(path);
findALLDirs(path);
printf("目前你总共写了 %1d 行代码!\n\n", total);
system("pause");
return 0;
} qiuyouzhi 发表于 2020-8-5 17:34
那个"r"不应该用单引号包起来吗?
还有,发完整代码
换成单引号之后还是运行不了{:10_266:} Husing 发表于 2020-8-5 17:37
换成单引号之后还是运行不了
直接复制下面的看看:
#include <io.h>
#include <direct.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 256
long total;
int countLines(const char *filename);
void findAllCodes(const char *path);
void findALLFiles(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;
}
void findAllCodes(const char *path)
{
struct _finddata_t fa;
long handle;
char thePath, target;
strcpy(thePath, path);
if((handle = _findfirst(strcat(thePath, "/*.c"), &fa)) != -1L)
{
do
{
sprintf(target, "%s/%s", path, fa.name);
total += countLines(target);
}while (_findnext(handle, &fa) == 0);
}
_findclose(handle);
}
void findALLDirs(const char *path)
{
struct _finddata_t fa;
long handle;
char thePath;
strcpy(thePath, path);
if((handle = _findfirst(strcat(thePath, "/*"), &fa)) == -1L)
{
fprintf(stderr, "The path %s is wrong!\n",path);
return;
}
do
{
if (!strcmp(fa.name, ".") || !strcmp(fa.name, ".."))
continue;
if( fa.attrib == _A_SUBDIR)
{
sprintf(thePath, "%s/%s", path, fa.name);
findAllCodes(thePath);
findALLDirs(thePath);
}
}while (_findnext(handle, &fa) == 0);
_findclose(handle);
}
int main()
{
char path = ".";
printf("计算中...\n");
findAllCodes(path);
findALLDirs(path);
printf("目前你总共写了 %ld 行代码!\n\n", total);
system("pause");
return 0;
} 哪报错? zltzlt 发表于 2020-8-5 17:38
直接复制下面的看看:
直接复制可以运行,但自己手打出来的却运行不了{:10_266:} Husing 发表于 2020-8-5 17:37
换成单引号之后还是运行不了
噗
printf("目前你总共写了 %1d 行代码!\n\n", total);
是ld,不是1d xiaosi4081 发表于 2020-8-5 17:39
哪报错?
第22行 if ((fp = fopen(filename, "r")) == NULL)
就这个,把双引号换成单引号还是不行 Husing 发表于 2020-8-5 17:37
换成单引号之后还是运行不了
噗,错误有点多,看大佬zltzlt的回复8 qiuyouzhi 发表于 2020-8-5 17:45
噗,错误有点多,看大佬zltzlt的回复8
我发现了{:10_266:}谢谢大佬 zltzlt 发表于 2020-8-5 17:38
直接复制下面的看看:
我发现我很多错了{:10_266:},谢谢大佬 xiaosi4081 发表于 2020-8-5 17:39
哪报错?
发现我很多错误了{:10_266:}谢谢大佬
页:
[1]