linux 第1个参数时在不兼容的指针类型间转换,从不兼容的指针类型中传递“fopen”的参数1 ?...

I'm stuck for sometime in this code:

我被这个密码困住了一段时间:

#include

const char *fn; int a;

int exists(const char *fname)

{

FILE *file;

if (file = fopen(fname, "r"))

{

fclose(file);

return 1;

}

return 0;

}

main(){

printf("Name:\n");

scanf("%s",&fn);

a=exists(&fn)

if(a==0){

fopen(&fn,"w");

fprintf(fn,"banhdhsjha");

}

}

When I try to run the program, it works until fprintf(fn,"banhdhsjha");, but it crashes here (Windows gives me an error) and the compiler (CodeBlocks) gives me the following notice:

当我尝试运行程序时,它一直工作到fprintf(fn,“banhdhsjha”);但是它在这里崩溃(Windows给我一个错误),编译器(代码库锁)给我以下通知:

passing argument 1 of 'fopen' from incompatible pointer type.

从不兼容的指针类型中传递“fopen”的参数1。

I am trying to make fprintf write data in the file, but I don't know how to do it. Can you help me?

我正在尝试让fprintf在文件中写入数据,但是我不知道怎么做。你能帮我吗?

1 个解决方案

#1

5

There are quite a few problems in your code.

您的代码中有很多问题。

You are passing &fn to scanf. This &fn is a pointer to pointer of const char ** type. This does not make sense. Format specifier %s requires a const char * argument, not const char **. You are basically using the pointer fn itself as a target buffer for file name (4 or 8 bytes long, depending on your platform's pointer size). Most likely you type in longer file name than that "buffer" can accommodate. That overrides memory in your program and leads to unpredictable (undefined) behavior.

你正在通过&fn到scanf。这个&fn是一个指向const char **类型指针的指针。这没有道理。格式说明符%s需要一个const char *参数,而不是const char **。您基本上是使用指针fn本身作为文件名称的目标缓冲区(根据平台的指针大小,长度为4或8字节)。您输入的文件名很可能比“缓冲区”所能容纳的文件名要长。这将重写程序中的内存,并导致不可预测的(未定义的)行为。

The proper form is probably scanf("%s", fn), but the problem is that you never allocated memory for the target buffer. You have to make sure that fn points to a char buffer of sufficient size to hold your file name.

正确的形式可能是scanf(“%s”,fn),但是问题是您从来没有为目标缓冲区分配内存。您必须确保fn指向一个足够大小的char缓冲区,以保存文件名。

Your fopen(&fn,"w") suffers from the same problem (and that is what the compiler is telling you). fopen expects an argument of const char * type and you are passing a const char ** instead. The proper form is fopen(fn,"w"), but again, see 2. Also, fopen return a file handle that you are supposed to store and use later. You are ignoring (discarding) the return value of fopen. That also makes no sense. You need an additional FILE * variable to store the return of fopen. You already know that, judging by what you did in exists function, but somehow you are ignoring that knowledge in your main.

您的fopen(和fn,“w”)也有同样的问题(这就是编译器告诉您的)。fopen期望一个const char *类型的参数,而您正在传递一个const char **。正确的形式是fopen(fn,“w”),但是同样的,参见2。此外,fopen还返回一个文件句柄,该句柄应该存储并在以后使用。您正在忽略(丢弃)fopen的返回值。这也没有道理。您需要一个附加的文件*变量来存储fopen的返回值。你已经知道了,根据你在存在函数中所做的判断,但是不知何故你忽略了你主体中的知识。

Your fprintf call also makes no sense at all. fprintf requires file handle (of FILE * type) as its first argument. Instead, you are trying to pass it a file name. That's not going to work and that's also going to trigger a diagnostic message from the compiler. You are supposed to store the return value of fopen (as I said in 3) and pass it to fprintf.

你的fprintf调用也毫无意义。fprintf需要文件句柄(文件*类型)作为它的第一个参数。相反,您试图将文件名传递给它。这是行不通的,它也会触发编译器的诊断消息。您应该存储fopen的返回值(如我在3中所说)并将其传递给fprintf。

Your exists(&fn) call suffers from the same problem as 1 and 3 and produces the same diagnostic message as your fopen call.

您的存在(&fn)调用遭受与1和3相同的问题,并产生与您的fopen调用相同的诊断消息。

Stop trying to write random code. Where did you get the idea to pass file name to fprintf instead of file handle? Read the documentation for each function you are trying to use and act accordingly.

停止尝试写随机代码。您是在哪里获得将文件名传递给fprintf而不是文件句柄的想法的?阅读要使用的每个函数的文档,并相应地采取行动。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值