初学者在 C 语言中使用 fopen 命令的困难

发布于 2024-11-15 03:58:37 字数 907 浏览 1 评论 0原文

我正在关注 http://www.cprogramming.com/tutorial/c 上的 C 编程教程/lesson10.html。这个特殊的教程教授 C 语言中的文件 I/O;特别讨论了 fopen 命令。在某一时刻,他们给出了以下示例(我认为应该打印文件 test.txt 的内容):

FILE *fp;
fp=fopen("c:\\test.txt", "w");
fprintf(fp, "Testing...\n");

因此,我创建了一个名为 test.txt 的文本文件并将其保存在我当前的工作目录中(C:\cygwin\家\安德鲁\cprogramming)。然后我在同一目录中创建了 ac 文件,它包含以下代码:

#include <stdio.h>

int main()
{
  FILE *fp;
  fp=open("test.txt","w");
  fprintf(fp,"Testing...\n");
}

当我使用 gcc 编译此 c 文件(我称之为 helloworld2.c)时,我收到以下消息:

helloworld2.c: In function `main':
helloworld2.c:40: warning: assignment makes pointer from integer without a cast

然后当我尝试运行可执行文件时,我得到:

Segmentation fault (core dumped)

你对我下一步应该尝试什么有什么想法吗?

非常感谢您抽出时间。

I am following the C programming tutorial at http://www.cprogramming.com/tutorial/c/lesson10.html. This particular tutorial teaches file I/O in C; in particular, the fopen command is discussed. At one point, they give the following example (which I think should print the contents of file test.txt):

FILE *fp;
fp=fopen("c:\\test.txt", "w");
fprintf(fp, "Testing...\n");

So, I made a text file called test.txt and saved it in my current, working directory (C:\cygwin\home\Andrew\cprogramming). Then I created a c file in this same directory, and it contains the following code:

#include <stdio.h>

int main()
{
  FILE *fp;
  fp=open("test.txt","w");
  fprintf(fp,"Testing...\n");
}

When I compile this c file (which I've called helloworld2.c) using gcc, I get the following messages:

helloworld2.c: In function `main':
helloworld2.c:40: warning: assignment makes pointer from integer without a cast

Then when I try to run the executable, I get:

Segmentation fault (core dumped)

Do you have any ideas about what I should try next?

Thank you very much for your time.

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

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

发布评论

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

评论(3

锦爱 2024-11-22 03:58:37

这是因为您使用 open 而不是 fopenOpen 来自 POSIX 标准并返回一个(整数)句柄; fopen 返回 FILE 结构的内存地址。您不能以互换的方式使用两者。按照目前的情况,您的代码隐式地将接收到的整数(可能是 4)转换为 FILE* 指针,使其指向内存地址 4。当 fprintf 时,这会导致您的程序出现段错误。尝试访问它。

fopen 是跨平台的,但 open 仅限 POSIX。您现在可能想坚持使用fopen

This is because you use open instead of fopen. Open is from the POSIX standard and returns an (integer) handle; fopen returns the memory address of a FILE structure. You cannot use both in an interchangeable way. As it stands, your code implicitly casts the received integer (likely 4) to a FILE* pointer, making it point to the memory address 4. This segfaults your program when fprintf attempts to access it.

fopen is cross-platform, but open is POSIX-only. You may want to stick to fopen for now.

堇色安年 2024-11-22 03:58:37

fopen() 返回指向 FILE 对象的指针,而 open() 返回一个普通 int 文件描述符>.
除非您需要低级函数,否则通常最好使用 fopenFILE 对象。

fopen() returns a pointer to a FILE object while open() returns a file descriptor which is a plain int.
Unless you need low-level functions it's usually better to work with fopen and FILE objects.

你的呼吸 2024-11-22 03:58:37

我猜这只是一个不幸的拼写错误 - open() 而不是 fopen() - 它恰好能够很好地构建最终的可执行文件(而不是故意尝试使用open())...

您会看到警告:赋值使指针来自整数而不进行强制转换因为没有“原型” - 参数的声明和返回类型 - 对于 中的 open()

如果没有这样的原型,编译器会假定存在这样的函数并返回一个 int,您的代码将其分配给指针变量 fp

事实上,它确实链接成功,因为 C 库中有一个名为 open() 的函数,但它做了一些不同的事情(正如其他人提到的)。但是,如果(例如)您改为编写 fpen() ,事情会变得更明显错误 - 它会在链接阶段失败,因为没有该名称的库函数。

如果您在启用更多警告的情况下进行编译 - 例如 GCC 的 -Wall - 您将得到一些更有用的错误:

$ gcc -Wall -o helloworld2 helloworld2.c
helloworld2.c: In function 'main':
helloworld2.c:6: warning: implicit declaration of function 'open'
helloworld2.c:6: warning: assignment makes pointer from integer without a cast
helloworld2.c:8: warning: control reaches end of non-void function
$ 

warning:implicit statements of function 'open' 告诉您您包含的标头与您尝试使用的函数之间不匹配。

I'm guessing this was just an unfortunate typo - open() instead of fopen() - which just happens to work well enough to build a final executable (rather than a deliberate attempt to use open())...

You see warning: assignment makes pointer from integer without a cast because there is no "prototype" - a declaration of the argument and return types - for open() in <stdio.h>.

In the absence of such a prototype, the compiler assumes that such a function exists and returns an int, which your code assigns to the pointer variable fp.

It does in fact link successfully because there is a function called open() in the C library, but it does something different (as others have mentioned). But if (for example) you'd written fpen() instead, things would have gone more obviously wrong - it would have failed at the link stage, as there is no library function of that name.

If you compile with more warnings enabled - e.g. -Wall for GCC - you'll get some more helpful errors:

$ gcc -Wall -o helloworld2 helloworld2.c
helloworld2.c: In function 'main':
helloworld2.c:6: warning: implicit declaration of function 'open'
helloworld2.c:6: warning: assignment makes pointer from integer without a cast
helloworld2.c:8: warning: control reaches end of non-void function
$ 

The warning: implicit declaration of function 'open' tells you that there is a mismatch between the headers you've included, and the function you're trying to use.

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