鱼C论坛

 找回密码
 立即注册
查看: 491|回复: 3

求教预编译的时候没有错误 编译的时候报错原因。

[复制链接]
发表于 2024-11-11 10:31:23 | 显示全部楼层 |阅读模式

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

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

x
#include<stdio.h>
#include<stdlib.h>

void main()
{
        FILE *f_pic, *f_file, *f_finish;
        char  ch, pic_name[20], file_name[20], finish_name[20];
       
        printf("请输入需要合成的图片和文件名称:/n");
        printf("图片:");
        scanf("%s", pic_name);
        printf("文件:");
        scanf("%s", file_name);
        printf("生成为:");
        scnaf("%s", finish_name);
       
        if(!(f_pic = fopen(pic_name, "rb")))
        {
                printf("Cannot open the picture %s !", pic_name);
                return;
        }
       
        if(!(f_file = fopen(file_name, "rb")))
        {
                printf("Cannot open the file %s !", file_name);
                return;
        }
       
        if(!(f_finish = fopen(finish_name, "wb")))
        {
                printf("Cannot open the file %s !", finish_name);
                return;
        }
        while(!(feof(f_pic)))
        {
                ch = fgetc(f_pic);
                fputc(ch, f_finish);
        }
        fclose(f_pic);
       
        while(!(feof(f_file)))
        {
                ch = fgetc(f_file);
                fputc(ch, f_finish);
        }
        fclose(f_file);
        fclose(f_finish);
       
       
        system("pause");
}
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2024-11-11 10:32:01 | 显示全部楼层
-------------------Configuration: 56 - Win32 Debug--------------------
Linking...
56.obj : error LNK2001: unresolved external symbol _scnaf
Debug/56.exe : fatal error LNK1120: 1 unresolved externals
执行 link.exe 时出错.

56.exe - 1 error(s), 0 warning(s)
用的是WIN11的系统
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-11-11 10:53:29 | 显示全部楼层
在你提供的代码中,预编译器并没有发现错误,因为它通常处理的是宏替换、文件包含等任务,而不负责检查C语言语法和类型错误。然而,在编译阶段会出现错误有以下几个原因:

1. **未包含头文件名**:
   在`#include`后面需要加上头文件名,比如标准输入输出头文件需要包含`stdio.h`。

2. **字符串缓冲区错误**:
   `char pic_name, file_name, finish_name;`定义的变量是字符类型,而不是字符串(字符数组)。`scanf("%s", pic_name);`应该用于字符数组,以存储文件名。因此,应该声明为字符数组,例如:`char pic_name[100], file_name[100], finish_name[100];`。

3. **`scnaf`拼写错误**:
   `scanf`被错误地拼写为`scnaf`。应该是:
  
  1. scanf("%s", finish_name);
复制代码


4. **使用`void main()`**:
   虽然某些编译器允许`void main()`,但更标准和安全的做法是使用`int main()`,并返回一个整数。

解决上述问题的代码如下:

  1. #include <stdio.h>
  2. #include <stdlib.h>

  3. int main()
  4. {
  5.         FILE *f_pic, *f_file, *f_finish;
  6.         char ch;
  7.         char pic_name[100], file_name[100], finish_name[100];
  8.        
  9.         printf("请输入需要合成的图片和文件名称:\n");
  10.         printf("图片:");
  11.         scanf("%s", pic_name);
  12.         printf("文件:");
  13.         scanf("%s", file_name);
  14.         printf("生成为:");
  15.         scanf("%s", finish_name);
  16.        
  17.         if(!(f_pic = fopen(pic_name, "rb")))
  18.         {
  19.                 printf("Cannot open the picture %s !", pic_name);
  20.                 return 1;
  21.         }
  22.        
  23.         if(!(f_file = fopen(file_name, "rb")))
  24.         {
  25.                 printf("Cannot open the file %s !", file_name);
  26.                 return 1;
  27.         }
  28.        
  29.         if(!(f_finish = fopen(finish_name, "wb")))
  30.         {
  31.                 printf("Cannot open the file %s !", finish_name);
  32.                 return 1;
  33.         }
  34.         while(!feof(f_pic))
  35.         {
  36.                 ch = fgetc(f_pic);
  37.                 fputc(ch, f_finish);
  38.         }
  39.         fclose(f_pic);
  40.        
  41.         while(!feof(f_file))
  42.         {
  43.                 ch = fgetc(f_file);
  44.                 fputc(ch, f_finish);
  45.         }
  46.         fclose(f_file);
  47.         fclose(f_finish);
  48.        
  49.         system("pause");
  50.         return 0;
  51. }
复制代码


这应该能帮助你解决编译时的问题。请根据你的具体需求调整代码,确保文件名数组大小足够大以存储输入的文件名。

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

使用道具 举报

发表于 2024-11-11 11:22:11 | 显示全部楼层
本帖最后由 jackz007 于 2024-11-11 11:33 编辑

            你这是编译通过,连接不成功,表示你的代码没有语法错误,但是,你所调用的某个函数或变量、常量等标识符找不到来历。

            让我们看看编译程序给出的提示信息:

            56.obj : error LNK2001: unresolved external symbol _scnaf

            scnaf() - 这个函数你自己没有定义,C 的函数库中也找不到,很显然,你把 scanf() 错误地写成 scnaf() 了。

            问题语句在这里:
  1.       scnaf("%s", finish_name);
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-5-10 00:30

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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