0
#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?

5
  • Your program should be ok, I tried it and it works. What's the problem you get? Commented Jun 23, 2016 at 11:19
  • The error message follows " Your program's output is shorter than the expected". Commented Jun 23, 2016 at 11:32
  • Sorry mate but I have never encountered this kind of problem Commented Jun 23, 2016 at 11:35
  • Its ok now I will ask the tutor. Commented Jun 23, 2016 at 11:37
  • please if you can share the answer when you will find out which is the problem. Commented Jun 23, 2016 at 11:41

5 Answers 5

3

If you want to know what is wrong check the result of fopen

opening = fopen("hello.usr","w"); if (opening == NULL) { perror("fopen"); } 
Sign up to request clarification or add additional context in comments.

3 Comments

Actually I am doing a online C programming course and I have this program to write.Write a program that prints the text "Hello world!" into the file "hello.usr". The file does not exist, so it must be created. Finally, the program must print a message on the screen indicating that writing to the file was successful. The text printed to the file must exactly match the assignment. Example output: Writing to the file was successful. Closing the program. I have tried both of your answers but still The compiler shows the error "The output is shorter than the expected". What can I do now?
"The output is shorter than the expected"? This is very strange. What is your compiler?
I don't know. This is the online compiler provided by the course which compares the output to the assignment.
1

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.

Comments

1

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.

Comments

0

Try to Instead "w" use "wt" in fopen

1 Comment

Print put after open prinrf("ernno: %d\n", errno); and share an output
0

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; } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.