EXC_BAD_ACCESS 将字符写入txt文件

发布于 2024-11-27 06:55:45 字数 297 浏览 3 评论 0原文

#include <stdio.h>
int main (int argc, const char * argv[])

{
    FILE *fp;
    fp=fopen("/Users/--------/Desktop/C.txt", "w");
    putc("C", "/Users/-------/Desktop/C.txt");
    fclose(fp);
}

这是我将字母“C”写入文本文件 C.txt 的简单代码。每次写入时都会收到错误代码 EXC_BAD_ACCESS。发生什么事了?

#include <stdio.h>
int main (int argc, const char * argv[])

{
    FILE *fp;
    fp=fopen("/Users/--------/Desktop/C.txt", "w");
    putc("C", "/Users/-------/Desktop/C.txt");
    fclose(fp);
}

Here is my simple code for writing the letter "C" into the textfile C.txt. I get the error code EXC_BAD_ACCESS every time I write it. Whats happeneing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

雨夜星沙 2024-12-04 06:55:45

putc 不应该这样使用。尝试:

putc('C', fp);

putc 看起来像这样:

int putc(int c, FILE *stream);

您传递的是 char * 而不是 FILE *

That's not how putc should be used. Try:

putc('C', fp);

putc looks like this:

int putc(int c, FILE *stream);

You are passing a char * instead of a FILE *.

罪#恶を代价 2024-12-04 06:55:45

请尝试以下操作:

 putc('C', fp);

putc 期望其第二个参数是 FILE*,而不是文件的路径。

第一个参数也应该是 char,而不是示例中的 char* 。

Try the following:

 putc('C', fp);

putc expects its second parameter to be FILE*, not a path to the file.

First parameter should also be char, not char* as in your example.

━╋う一瞬間旳綻放 2024-12-04 06:55:45

您应该写入该文件:

putc('C', fp);

此外,您应该检查 fopen 没有返回 NULL

You should write to the file:

putc('C', fp);

Also, you should check that fopen didn't return NULL

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文