C语言 简单的 C 程序打开文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5012126/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SImple C Program opening a file
提问by paxdiablo
I'm trying to make a program to open a file, called "write.txt".
我正在尝试制作一个程序来打开一个名为“write.txt”的文件。
#include <stdio.h>
main() {
FILE *fp;
fp = fopen("write.txt", "w");
return 0;
}
Should this work? Because it returns nothing.
这应该工作吗?因为它什么都不返回。
回答by paxdiablo
Other than an old variant of main, there's not really muchwrong with that code. It should, barring errors, create the file.
除了 的旧变体之外main,该代码并没有太大问题。除非出现错误,否则它应该创建文件。
However, since you're not checking the return value from fopen, you may get an error of some sort and not know about it.
但是,由于您没有检查 的返回值fopen,因此您可能会收到某种错误并且对此一无所知。
I'd start with:
我会从:
#include <stdio.h>
#include <errno.h>
int main (void) {
FILE *fp;
fp = fopen ("write.txt","w");
if (fp == NULL) {
printf ("File not created okay, errno = %d\n", errno);
return 1;
}
//fprintf (fp, "Hello, there.\n"); // if you want something in the file.
fclose (fp);
printf ("File created okay\n");
return 0;
}
If you're adamant that the file isn't being created but the above code says it is, then you may be a victim of the dreaded "IDE is working in a different directory from what you think" syndrome :-)
如果您坚持认为文件没有被创建,但上面的代码说它是,那么您可能是可怕的“IDE 正在与您认为的目录不同的目录中工作”综合症的受害者:-)
Some IDEs (such as Visual Studio) will actually run your code while they're in a directory like <solution-name>\binor <solution-name>\debug. You can find out by putting:
某些 IDE(例如 Visual Studio)实际上会在它们位于<solution-name>\bin或 之类的目录中时运行您的代码<solution-name>\debug。你可以通过输入:
system ("cd"); // for Windows
system ("pwd") // for UNIXy systems
in to your code to see where it's running. That'swhere a file will be created if you specify a relative path line "write.txt". Otherwise, you can specify an absolute path to ensure it tries to create it at a specific point in the file system.
在您的代码中查看它的运行位置。这就是在那里,如果你指定一个相对路径线上的文件将被创建"write.txt"。否则,您可以指定一个绝对路径以确保它尝试在文件系统中的特定点创建它。
回答by Martin Beckett
What did you expect it to 'return' - it opens a file, on most platforms creating one if it doesn't exist.
你期望它“返回”什么 - 它打开一个文件,在大多数平台上如果它不存在则创建一个。
You should probably fclose(fp) the file at the end.
您可能应该在最后 fclose(fp) 文件。
回答by Algorithmist
I think you want to print the contents of file write.txt. (Assume it contains characters).
我想你想打印文件 write.txt 的内容。(假设它包含字符)。
#include <stdio.h>
int main()
{
FILE *fp,char ch;
fp=fopen("write.txt","r");
if(fp==NULL)
{
printf("Some problem in opening the file");
exit(0);
}
else
{
while((ch=fgetc(fp))!=EOF)
{
printf("%c",ch);
}
}
fclose(fp);
return 0;
}
回答by in70x
I think you should study some more fundamentals in C before you start attempting to work with files. A return means some data is passed back to the calling code from the called function.In this case you return 0 at the end of your program. You did not do anything with your FILE pointer except cause a new file to be created...
我认为在开始尝试处理文件之前,您应该学习更多 C 基础知识。返回意味着一些数据从被调用函数传回调用代码。在这种情况下,您在程序结束时返回 0。除了导致创建新文件之外,您没有对 FILE 指针执行任何操作...

