C 编程 fopen() 打开文件时

发布于 2024-10-05 23:01:07 字数 746 浏览 0 评论 0原文

我一直想知道这个问题。我读过的大多数书籍都表明,当您打开一个文件并且发现该文件不存在时,您应该输入一个错误,指出不存在这样的文件,然后退出系统......

FILE *stream = NULL; 
stream = fopen("student.txt", "rt");
if (stream==NULL) {
    printf(“Cannot open input file\n”);
    exit(1);
else {printf("\nReading the student list directory. Wait a moment please...");

但我认为不应该这样做......当您发现要打开的文件不存在时,为什么不自动创建一个新文件呢?即使您在使用该程序时不会在文件上写入(但下次会使用它)。我不确定这是否有效。我是新来的,没有任何编程经验,所以我想询问您的意见,在尝试打开文件时创建文件而不是像书本上通常示例的那样退出系统,有什么优点和缺点。

FILE *stream = NULL; 
stream = fopen("student.txt", "rt");
     if (stream == NULL) stream = fopen("student.txt", "wt");
     else {
          printf("\nReading the student list directory. Wait a moment please...");

我们将非常感谢您的意见。谢谢。

I've been wondering about this one. Most books I've read shows that when you open a file and you found that the file is not existing, you should put an error that there's no such file then exit the system...

FILE *stream = NULL; 
stream = fopen("student.txt", "rt");
if (stream==NULL) {
    printf(“Cannot open input file\n”);
    exit(1);
else {printf("\nReading the student list directory. Wait a moment please...");

But I thought that instead of doing that.. why not automatically create a new one when you found that the file you are opening is not existing. Even if you will not be writing on the file upon using the program (but will use it next time). I'm not sure if this is efficient or not. I'm just new here and have no programming experience whatsoever so I'm asking your opinion what are the advantages and disadvantages of creating a file upon trying to open it instead of exiting the system as usually being exampled on the books.

FILE *stream = NULL; 
stream = fopen("student.txt", "rt");
     if (stream == NULL) stream = fopen("student.txt", "wt");
     else {
          printf("\nReading the student list directory. Wait a moment please...");

Your opinion will be highly appreciated. Thank you.

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

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

发布评论

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

评论(4

幼儿园老大 2024-10-12 23:01:07

因为从您的示例来看,它似乎是一个输入文件,如果它不存在,则没有必要创建它。

例如,如果程序应该打开一个文件,然后计算其中有多少个元音,那么如果该文件不存在,我看不出创建该文件有多大意义。

我的价值 0.02 美元。

Because from your example, it seems like it's an input file, if it doesn't exist, no point creating it.

For example if the program is supposed to open a file, then count how many vowels in it, then I don't see much sense of creating the file if it doesn't exist.

my $0.02 worth.

自由如风 2024-10-12 23:01:07

论证方式:

 ``r''   Open text file for reading. 
 ``r+''  Open for reading and writing. 
 ``w''   Truncate file to zero length or create text file for writing.
 ``w+''  Open for reading and writing.  The file is created if it does not
         exist, otherwise it is truncated.  
 ``a''   Open for writing.  The file is created if it does not exist.
 ``a+''  Open for reading and writing.  The file is created if it does not
         exist.  

你的问题是一个简单的案例。阅读上面的描述,当您调用 fopen() 时,您应该决定使用哪种模式。请考虑为什么“r”和“r+”不创建文件,为什么“w”和“w+”文件被截断等等,这些都是合理的设计。

Argument mode:

 ``r''   Open text file for reading. 
 ``r+''  Open for reading and writing. 
 ``w''   Truncate file to zero length or create text file for writing.
 ``w+''  Open for reading and writing.  The file is created if it does not
         exist, otherwise it is truncated.  
 ``a''   Open for writing.  The file is created if it does not exist.
 ``a+''  Open for reading and writing.  The file is created if it does not
         exist.  

Your question is a simple case. Read above description, when you call fopen(), you should decide which mode shall be used. Please consider why a file is not created for "r" and "r+", and why a file is truncated for "w" and "w+", etc. All of these are reasonable designs.

情释 2024-10-12 23:01:07

如果您的程序期望一个文件存在,但它不存在,那么您自己创建一个文件就没有多大意义,因为它会是空的。

如果OTOH,您的程序可以处理不存在的文件,并且知道如何从头开始填充文件,那么这样做就完全没问题。

只要对您的程序有意义,两者都可以。不要担心这里的效率——它可以忽略不计。首先担心正确性。

If your program expects a file to exist and it doesn't, then creating one yourself doesn't make much sense, since it's going to be empty.

If OTOH, your program is OK with a file not existing and knows how to populate one from scratch, then it's perfectly fine to do so.

Either is fine as long as it makes sense for your program. Don't worry about efficiency here -- it's negligible. Worry about correctness first.

一个人的夜不怕黑 2024-10-12 23:01:07

您可能无权在用户选择的目录中创建/写入文件。您将必须处理该错误情况。

You may not have permission to create/write to a file in the directory that the user chooses. You will have to handle that error condition.

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