简体   繁体   English

在C编程中创建一个文件,并在文件中写入hello world

[英]create a file in c programming and write hello world in the file

#include<stdio.h>    
int main()
{    
    FILE *opening;
    opening = fopen("hello.usr","w");
    fprintf(opening,"Hello world!");     
    fclose(opening);
    printf("Writing to the file was successful.\n");
    printf("Closing the program");
    return 0;
}

I have tried this code to create a file in c programming and write the text "Hello world!" 我已经尝试过使用此代码在c编程中创建一个文件,并编写文本“ Hello world!”。 in it. 在里面。 What's wrong with this code? 此代码有什么问题?

If you want to know what is wrong check the result of fopen 如果您想知道哪里出了问题,请检查fopen的结果

opening = fopen("hello.usr","w");
if (opening == NULL) {
    perror("fopen");
}

As of now, you don't know whether you managed to write to the file or not, so here's a suggestion which checks for it. 截至目前,您尚不知道是否设法写入文件,因此,这里有一条建议可以检查该文件。

FILE *opening;
opening = fopen("hello.usr", "w");
if (opening == NULL){
    perror("fopen");
    return 0;
}

By returning 0 here you remove the option for segmentation fault as the code will still try to write to the file even if it doesn't exist. 通过在此处返回0,您可以删除分段错误选项,因为即使该文件不存在,该代码仍会尝试写入该文件。

The error message you are getting most certainly is NOT produced by a compiler. 您最肯定会收到的错误消息不是由编译器产生的。 It looks to me as a message of some automatic checker that tests correctness of the submited solutions. 在我看来,它就像是一些自动检查器的消息,用于测试提交的解决方案的正确性。

Make sure that the output matches exactly the required one. 确保输出与所需的输出完全匹配。

The message: 消息:

Your program's output is shorter than the expected 您的程序的输出比预期的短

may indicate that there is something wrong with new line characters ('\\n'). 可能表示换行符('\\ n')出了问题。 Check for those. 检查那些。

For example if the required output is: 例如,如果所需的输出是:

Writing to the file was successful. 写入文件成功。 Closing the program. 关闭程序。

... printed in one line, your output obviously doesn't match as it has a new line after the first sentence. ...打印在一行中,您的输出显然不匹配,因为它在第一句之后有新行。 And if the checker testes for the first occurrence of a new line character it sees only 并且如果检查器测试了换行符的首次出现,它只会看到

Writing to the file was successful. 写入文件成功。

which could be one of many possible explanations. 这可能是许多可能的解释之一。 If this is the case try simply: 如果是这种情况,请尝试简单地:

#include<stdio.h>    
int main()
{    
    FILE *opening;
    opening = fopen("hello.usr","w");
    fprintf(opening,"Hello world!");     
    fclose(opening);
    // printf("Writing to the file was successful.\n");
    // printf("Closing the program");
    printf("Writing to the file was successful. Closing the program\n");
    return 0;
}

Note also that this sort of error messages (in automatic testing environments) are usually triggered by ommited, added extra or confused non-printable characters (spaces, tabs, new lines) or punctuation marks which is hard to notice. 另请注意,此类错误消息(在自动测试环境中)通常是由难以注意到的遗漏,添加的额外或混乱的不可打印字符(空格,制表符,新行)或标点符号触发的。

You may also want to check in this respect the text you print to the file. 在这方面,您可能还需要检查打印到文件中的文本。

尝试在fopen中使用“ w”代替“ w”

Try the following 尝试以下

#include <stdio.h>

int main() { 

    FILE *opening = fopen("hello.usr", "w");

    if(opening == NULL){
        printf("An error occurred when opening the file!");
        return 0;
    }

    else{
        fprintf(opening, "Hello world!\n"); 
        fclose(opening);
        printf("Writing to the file was successful.\nClosing the program.\n");
    }
    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM