初学者在 C 语言中使用 fopen 命令的困难
我正在关注 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是因为您使用
open
而不是fopen
。Open
来自 POSIX 标准并返回一个(整数)句柄;fopen
返回FILE
结构的内存地址。您不能以互换的方式使用两者。按照目前的情况,您的代码隐式地将接收到的整数(可能是 4)转换为 FILE* 指针,使其指向内存地址 4。当fprintf
时,这会导致您的程序出现段错误。尝试访问它。fopen
是跨平台的,但open
仅限 POSIX。您现在可能想坚持使用fopen
。This is because you use
open
instead offopen
.Open
is from the POSIX standard and returns an (integer) handle;fopen
returns the memory address of aFILE
structure. You cannot use both in an interchangeable way. As it stands, your code implicitly casts the received integer (likely 4) to aFILE*
pointer, making it point to the memory address 4. This segfaults your program whenfprintf
attempts to access it.fopen
is cross-platform, butopen
is POSIX-only. You may want to stick tofopen
for now.fopen()
返回指向FILE
对象的指针,而open()
返回一个普通int
文件描述符>.除非您需要低级函数,否则通常最好使用
fopen
和FILE
对象。fopen()
returns a pointer to aFILE
object whileopen()
returns a file descriptor which is a plainint
.Unless you need low-level functions it's usually better to work with
fopen
andFILE
objects.我猜这只是一个不幸的拼写错误 -
open()
而不是fopen()
- 它恰好能够很好地构建最终的可执行文件(而不是故意尝试使用open()
)...您会看到
警告:赋值使指针来自整数而不进行强制转换
因为没有“原型” - 参数的声明和返回类型 - 对于
中的open()
。如果没有这样的原型,编译器会假定存在这样的函数并返回一个
int
,您的代码将其分配给指针变量fp
。事实上,它确实链接成功,因为 C 库中有一个名为
open()
的函数,但它做了一些不同的事情(正如其他人提到的)。但是,如果(例如)您改为编写fpen()
,事情会变得更明显错误 - 它会在链接阶段失败,因为没有该名称的库函数。如果您在启用更多警告的情况下进行编译 - 例如 GCC 的
-Wall
- 您将得到一些更有用的错误:warning:implicit statements of function 'open'
告诉您您包含的标头与您尝试使用的函数之间不匹配。I'm guessing this was just an unfortunate typo -
open()
instead offopen()
- which just happens to work well enough to build a final executable (rather than a deliberate attempt to useopen()
)...You see
warning: assignment makes pointer from integer without a cast
because there is no "prototype" - a declaration of the argument and return types - foropen()
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 variablefp
.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 writtenfpen()
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: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.