简体   繁体   中英

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!" in it. What's wrong with this code?

If you want to know what is wrong check the result of 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.

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'). 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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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