鱼C论坛

 找回密码
 立即注册
查看: 67|回复: 11

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

[复制链接]
发表于 昨天 13:13 From FishC Mobile | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

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

作业是计算代码行数
运行第一次提示不能打开我的两个.c的文件,
运行第二次提示无法识别“d”为cmlet、函数…,所在位置行1列1,…
IMG_1043.jpeg
并且继续运行就重复上面的情况
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 昨天 13:14 | 显示全部楼层
亲爱的鱼油,你好!别担心,我们来一步步解决你的C语言作业问题。你的程序是计算代码行数,但遇到了两个错误。下面我帮你分析原因并提供解决方法。

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

解决方法:
- 在命令行中运行程序时,使用完整路径或确保文件在当前目录。
- 例如,如果你的程序名为 count_lines.exe,文件在同一个文件夹,可以这样运行:
  1. count_lines.exe file1.c file2.c
复制代码

- 在程序中,检查文件打开代码,确保使用正确的模式(如 "r" 用于读取)。

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

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

解决方法:
- 确保每次运行程序时,都输入完整的命令,例如:
  1. count_lines.exe file1.c file2.c
复制代码

- 如果程序需要参数,不要遗漏文件名。检查你的程序代码,确认它正确处理了命令行参数。

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

  1. FILE *file = fopen(filename, "r");
  2. if (file == NULL) {
  3.     printf("无法打开文件: %s\n", filename);
  4.     return -1;
  5. }
复制代码

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

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

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 昨天 13:42 | 显示全部楼层
         贴出你的 C 语言代码。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 昨天 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[MAX], target[MAX];

    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[MAX];

     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[MAX] = ".";
     
     printf("计算中...\n");
     
     findALLCodes(path);
     findALLDirs(path);

     printf("目前你总共写下 %ld 行代码!\n\n", total);
     system("pause");

     return 0;
}
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 昨天 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[MAX], target[MAX];

    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[MAX];

     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[MAX] = ".";
     
     printf("计算中...\n");
     
     findALLCodes(path);
     findALLDirs(path);

     printf("目前你总共写下 %ld 行代码!\n\n", total);
     system("pause");

     return 0;
}
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 昨天 14:20 | 显示全部楼层
PowerShell 报错提示:

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


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

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

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

  1. Set-Location D:
复制代码

或简写成:

  1. cd D:
复制代码



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

  1. cd "C:\Users\lenovo\Desktop\iloveu\test.c源文件"
复制代码

如果路径里有中文或空格,一定要加双引号 "..."。


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

  1. chcp 65001
复制代码



3、编译并运行:

  1. gcc test1count.c -o test1count
  2. .\test1count
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 昨天 14:44 | 显示全部楼层
anhedemo 发表于 2025-10-26 14:16
(还不会发代码图,后面看一下)
#include
#include
  1. #include <io.h>
  2. #include <direct.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>

  6. #define MAX        256

  7. long total;

  8. int countLines(const char * filename)
  9. {
  10.         FILE * fp     ;
  11.         int count = 0 ;
  12.         int temp      ;

  13.         if ((fp = fopen(filename , "r")) == NULL)
  14.         {
  15.                 fprintf(stderr , " Can not open the file: %s\n", filename);
  16.                 return 0                                                  ;
  17.         }

  18.         while((temp = fgetc(fp)) != EOF)                       /* 【1】:这里有错误,已经修正 */
  19.         {
  20.                 if (temp == '\n')
  21.                 {
  22.                         count++;
  23.                 }
  24.         }
  25.             
  26.         fclose(fp);

  27.         return count;
  28. }

  29. void findALLCodes(const char *path)
  30. {
  31.     struct _finddata_t fa;
  32.     long handle;
  33.     char thePath[MAX], target[MAX];

  34.     strcpy(thePath,path);
  35.     if((handle = _findfirst(strcat(thePath, "/*.c"), &fa)) != -1L)
  36.     {
  37.             do   
  38.             {
  39.                     sprintf(target, "%s/%s", path, fa.name) ;  /*【2】:这里有错误,已经修正 */
  40.                     total += countLines(target);
  41.             }while (_findnext(handle, &fa) == 0);
  42.     }

  43.     _findclose(handle);
  44. }

  45. void findALLDirs(const char *path)
  46. {
  47.      struct _finddata_t fa;
  48.      long handle;
  49.      char thePath[MAX];

  50.      strcpy(thePath, path);
  51.      if((handle = _findfirst(strcat(thePath, "/*"), &fa)) == -1L)
  52.      {
  53.              fprintf(stderr, "the path %s is wrong!\n",path);
  54.              return;
  55.      }

  56.      do
  57.      {
  58.              if(!strcmp(fa.name, ".") || !strcmp(fa.name, ".."))
  59.                     continue;

  60.              if( fa.attrib == _A_SUBDIR)
  61.              {      
  62.                      sprintf(thePath, "%s/%s", path, fa.name);
  63.                      findALLCodes(thePath);
  64.                      findALLDirs(thePath);
  65.              }
  66.      }while (_findnext(handle, &fa) == 0);
  67.      
  68.      _findclose(handle);
  69. }

  70. int main()
  71. {
  72.      char path[MAX] = ".";
  73.      
  74.      printf("计算中...\n");
  75.      
  76.      findALLCodes(path);
  77.      findALLDirs(path);

  78.      printf("目前你总共写下 %ld 行代码!\n\n", total);
  79.      system("pause");

  80.      return 0;
  81. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 昨天 15:10 | 显示全部楼层
不二如是 发表于 2025-10-26 14:20
PowerShell 报错提示:

把下面三个(新鱼油推荐后面的)代码,分别打到终端,按回车是吗
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 昨天 15:48 | 显示全部楼层
anhedemo 发表于 2025-10-26 15:10
把下面三个(新鱼油推荐后面的)代码,分别打到终端,按回车是吗

是的
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 昨天 16:38 | 显示全部楼层

依次输入并回车后,第四条代码的输出是乱码的,然后应该怎么做(关了终端后,再去运行代码,第二遍依然那样报错
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

环境是按教程用vscode搭建的,第二个问题还没懂
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 昨天 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. 检查终端输入:确保每次运行程序时,输入完整的命令,例如:
  
  1. 你的程序名.exe 文件1.c 文件2.c
复制代码

   不要输入单个字母或无关字符。

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

3. 检查程序逻辑:你的C程序可能在某些情况下没有正常退出,导致终端等待。确保程序中有适当的退出条件,比如:
  

  1.    #include <stdio.h>
  2.    int main() {
  3.        // 你的代码
  4.        return 0; // 正常退出
  5.    }
  6.    
复制代码


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

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

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

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-10-27 00:53

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表