anhedemo 发表于 前天 13:13

c语言第一个课后作业报错求解惑vscode8.1.0

本帖最后由 anhedemo 于 2025-10-26 15:46 编辑

作业是计算代码行数
运行第一次提示不能打开我的两个.c的文件,
运行第二次提示无法识别“d”为cmlet、函数…,所在位置行1列1,…

并且继续运行就重复上面的情况

FishC 发表于 前天 13:14

亲爱的鱼油,你好!别担心,我们来一步步解决你的C语言作业问题。你的程序是计算代码行数,但遇到了两个错误。下面我帮你分析原因并提供解决方法。

第一次错误:不能打开.c文件
这个错误通常是因为程序找不到或无法访问你指定的文件。可能的原因包括:
- 文件路径不正确:确保你提供的文件名和路径是准确的。
- 文件不存在:检查文件是否在当前位置,或者文件名是否拼写错误。
- 权限问题:在某些系统中,文件可能被锁定或无读取权限。

解决方法:
- 在命令行中运行程序时,使用完整路径或确保文件在当前目录。
- 例如,如果你的程序名为 count_lines.exe,文件在同一个文件夹,可以这样运行:
count_lines.exe file1.c file2.c
- 在程序中,检查文件打开代码,确保使用正确的模式(如 "r" 用于读取)。

第二次错误:无法识别“d”为cmlet、函数…
这个错误看起来像是在命令行中输入了无效命令。"cmlet" 可能是 "cmdlet" 的笔误(常见于PowerShell),但更可能是你意外输入了单个字母 "d",而不是程序名或参数。

原因分析:
- 你可能在第一次运行后,直接在命令行中输入了 "d" 并回车,导致系统尝试执行 "d" 这个命令,但它不存在。
- 这与你程序的C代码无关,而是命令行使用问题。

解决方法:
- 确保每次运行程序时,都输入完整的命令,例如:
count_lines.exe file1.c file2.c
- 如果程序需要参数,不要遗漏文件名。检查你的程序代码,确认它正确处理了命令行参数。

整体建议
- 先检查你的C程序代码,确保文件打开部分正确。例如:

FILE *file = fopen(filename, "r");
if (file == NULL) {
    printf("无法打开文件: %s\n", filename);
    return -1;
}

- 在命令行中运行程序时,注意输入格式,避免拼写错误。
- 如果问题持续,尝试用调试工具或打印语句检查程序逻辑。

记住,编程中遇到错误很正常,多练习就会进步!如果还有细节不清楚,欢迎补充描述,我会继续帮你。加油!

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。

jackz007 发表于 前天 13:42

         贴出你的 C 语言代码。

anhedemo 发表于 前天 14:11

jackz007 发表于 2025-10-26 13:42
贴出你的 C 语言代码。

sorry我不知道怎么发代码图片
#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 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;
}

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   
            {
                  Ssprintf(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;
}

anhedemo 发表于 前天 14:16

(还不会发代码图,后面看一下)
#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 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;
}

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   
            {
                  Ssprintf(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;
}

不二如是 发表于 前天 14:20

PowerShell 报错提示:

无法将“d”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。

这说明 PowerShell 把 d 当成命令执行了,而你其实是想 切换到 d 盘 或 切换目录。

在 PowerShell 中,切换目录的写法与 cmd 不一样!

d: 在 cmd 里能直接切换盘符,但 PowerShell 中要这样写:

Set-Location D:
或简写成:

cd D:


推荐鱼油按这个执行)
1、先切换到你的代码目录,打开 PowerShell,输入:

cd "C:\Users\lenovo\Desktop\iloveu\test.c源文件"
如果路径里有中文或空格,一定要加双引号 "..."。


2、设置编码(防止中文乱码):

chcp 65001


3、编译并运行:

gcc test1count.c -o test1count
.\test1count

jackz007 发表于 前天 14:44

anhedemo 发表于 2025-10-26 14:16
(还不会发代码图,后面看一下)
#include
#include


#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)
{
      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)                     /* 【1】:这里有错误,已经修正 */
      {
                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) ;/*【2】:这里有错误,已经修正 */
                  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;
}

anhedemo 发表于 前天 15:10

不二如是 发表于 2025-10-26 14:20
PowerShell 报错提示:




把下面三个(新鱼油推荐后面的)代码,分别打到终端,按回车是吗

不二如是 发表于 前天 15:48

anhedemo 发表于 2025-10-26 15:10
把下面三个(新鱼油推荐后面的)代码,分别打到终端,按回车是吗

是的

anhedemo 发表于 前天 16:38

不二如是 发表于 2025-10-26 15:48
是的

依次输入并回车后,第四条代码的输出是乱码的,然后应该怎么做(关了终端后,再去运行代码,第二遍依然那样报错

anhedemo 发表于 前天 17:52

FishC 发表于 2025-10-26 13:14
亲爱的鱼油,你好!别担心,我们来一步步解决你的C语言作业问题。你的程序是计算代码行数,但遇到了两个错 ...

环境是按教程用vscode搭建的,第二个问题还没懂

FishC 发表于 前天 17:53

anhedemo 发表于 2025-10-26 17:52
FishC 发表于 2025-10-26 13:14
亲爱的鱼油,你好! ...

亲爱的鱼油,你好!很高兴能继续帮你解惑。关于第二个问题“无法识别‘d’为cmlet、函数…”,这通常与你的C程序代码无关,而是你在VSCode终端中输入命令时的小失误。下面我来详细解释:

问题原因:
- 这个错误消息是来自终端(比如PowerShell或命令提示符),而不是你的C程序。
- “cmlet”可能是“cmdlet”的笔误(cmdlet是PowerShell中的命令)。
- 当你在终端中输入单个字母d并回车时,系统会尝试将d当作一个命令来执行,但系统中没有这个命令,所以报错。
- 数字“199344”可能是其他输出或错误代码,但根据描述,它可能来自你的程序或系统,需要结合代码确认。

为什么会出现这种情况?
- 你可能在第一次运行程序后,没有正确退出或输入了新命令,而是意外输入了d。
- 或者,你的程序在运行后没有正常结束,导致终端等待输入,你输入了d作为响应。
- 在VSCode中,终端可能会保留历史命令,重复运行时会执行之前的输入。

解决方法:
1. 检查终端输入:确保每次运行程序时,输入完整的命令,例如:
   你的程序名.exe 文件1.c 文件2.c
   不要输入单个字母或无关字符。

2. 清理终端:如果终端有历史命令,可以关闭并重新打开VSCode的终端,避免旧输入干扰。

3. 检查程序逻辑:你的C程序可能在某些情况下没有正常退出,导致终端等待。确保程序中有适当的退出条件,比如:
   
   #include <stdio.h>
   int main() {
       // 你的代码
       return 0; // 正常退出
   }
   

4. 验证文件路径:第一个错误“不能打开.c文件”可能影响了后续运行。确保在命令行中提供正确的文件名,且文件存在于当前目录。

整体建议:
- 在VSCode中,使用终端时,专注输入程序名和参数,避免额外输入。
- 如果问题持续,分享你的C代码片段,我可以帮你检查文件打开部分。
- 记住,编程中遇到终端错误很常见,多练习就会熟悉!

加油,鱼油!如果还有不清楚的地方,欢迎继续问。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
页: [1]
查看完整版本: c语言第一个课后作业报错求解惑vscode8.1.0