Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Post History

55%
+3 −2
Q&A Why does fopen return NULL?

#include<stdio.h> #include<stdlib.h> int main (void){ FILE *cfPtr; char name[15]; int ID; if ((cfPtr = fopen("draft.txt","w")) == NULL){ printf("File could n...

2 answers  ·  posted 3y ago by dumplings‭  ·  edited 2y ago by dumplings‭

#8: Post edited by user avatar dumplings‭ · 2022-06-26T14:20:36Z (over 2 years ago)
  • ```c
  • #include<stdio.h>
  • #include<stdlib.h>
  • int main (void){
  • FILE *cfPtr;
  • char name[15];
  • int ID;
  • if ((cfPtr = fopen("students.txt","w")) == NULL){
  • printf("File could not be opened...\n"); //program gives me this as an output
  • }
  • else { //I want the output to be those in this else statement
  • do {
  • printf("Enter Name:\n");
  • fflush(stdin); //flushing the input
  • scanf("%s", name);
  • printf("Enter Student Number:");
  • scanf("%d", &ID);
  • fprintf(cfPtr, "%d %s\n", ID, name);
  • } while (ID != -1);
  • fclose(cfPtr);
  • }
  • return 0;
  • }
  • ```
  • Output:
  • > File could not be opened...
  • ```c
  • #include<stdio.h>
  • #include<stdlib.h>
  • int main (void){
  • FILE *cfPtr;
  • char name[15];
  • int ID;
  • if ((cfPtr = fopen("draft.txt","w")) == NULL){
  • printf("File could not be opened...\n"); //program gives me this as an output
  • }
  • }
  • ```
  • Output:
  • > File could not be opened...
#7: Post edited by user avatar hkotsubo‭ · 2021-12-07T13:41:06Z (almost 3 years ago)
Missed one extra backtick in code block, now it should be correct
Why does fopen return NULL?
  • ````c
  • #include<stdio.h>
  • #include<stdlib.h>
  • int main (void){
  • FILE *cfPtr;
  • char name[15];
  • int ID;
  • if ((cfPtr = fopen("students.txt","w")) == NULL){
  • printf("File could not be opened...\n"); //program gives me this as an output
  • }
  • else { //I want the output to be those in this else statement
  • do {
  • printf("Enter Name:\n");
  • fflush(stdin); //flushing the input
  • scanf("%s", name);
  • printf("Enter Student Number:");
  • scanf("%d", &ID);
  • fprintf(cfPtr, "%d %s\n", ID, name);
  • } while (ID != -1);
  • fclose(cfPtr);
  • }
  • return 0;
  • }
  • ```
  • Output:
  • > File could not be opened...
  • ```c
  • #include<stdio.h>
  • #include<stdlib.h>
  • int main (void){
  • FILE *cfPtr;
  • char name[15];
  • int ID;
  • if ((cfPtr = fopen("students.txt","w")) == NULL){
  • printf("File could not be opened...\n"); //program gives me this as an output
  • }
  • else { //I want the output to be those in this else statement
  • do {
  • printf("Enter Name:\n");
  • fflush(stdin); //flushing the input
  • scanf("%s", name);
  • printf("Enter Student Number:");
  • scanf("%d", &ID);
  • fprintf(cfPtr, "%d %s\n", ID, name);
  • } while (ID != -1);
  • fclose(cfPtr);
  • }
  • return 0;
  • }
  • ```
  • Output:
  • > File could not be opened...
#6: Post edited by user avatar hkotsubo‭ · 2021-12-07T13:26:35Z (almost 3 years ago)
Better title, fix code block
  • When I run this program, it gives me the output of the 'if statement', how can I access the 'else statement' for me to enter the student details?
  • Why does fopen return NULL?
  • ````#include<stdio.h>
  • #include<stdlib.h>
  • int main (void){
  • FILE *cfPtr;
  • char name[15];
  • int ID;
  • if ((cfPtr = fopen("students.txt","w")) == NULL){
  • printf("File could not be opened...\n"); //program gives me this as an output
  • }
  • else { //I want the output to be those in this else statement
  • do {
  • printf("Enter Name:\n");
  • fflush(stdin); //flushing the input
  • scanf("%s", name);
  • printf("Enter Student Number:");
  • scanf("%d", &ID);
  • fprintf(cfPtr, "%d %s\n", ID, name);
  • } while (ID != -1);
  • fclose(cfPtr);
  • }
  • return 0;
  • }
  • Output: File could not be opened...
  • (I can't enter the student details)
  • ````c
  • #include<stdio.h>
  • #include<stdlib.h>
  • int main (void){
  • FILE *cfPtr;
  • char name[15];
  • int ID;
  • if ((cfPtr = fopen("students.txt","w")) == NULL){
  • printf("File could not be opened...\n"); //program gives me this as an output
  • }
  • else { //I want the output to be those in this else statement
  • do {
  • printf("Enter Name:\n");
  • fflush(stdin); //flushing the input
  • scanf("%s", name);
  • printf("Enter Student Number:");
  • scanf("%d", &ID);
  • fprintf(cfPtr, "%d %s\n", ID, name);
  • } while (ID != -1);
  • fclose(cfPtr);
  • }
  • return 0;
  • }
  • ```
  • Output:
  • > File could not be opened...
#5: Post edited by user avatar Lundin‭ · 2021-12-07T11:10:45Z (almost 3 years ago)
More relevant tags
#4: Post edited by user avatar dumplings‭ · 2021-12-07T10:59:46Z (almost 3 years ago)
  • ````#include<stdio.h>
  • #include<stdlib.h>
  • int main (void){
  • FILE *cfPtr;
  • char name[15];
  • int ID;
  • if ((cfPtr = fopen("students.txt","w")) == NULL){
  • printf("File could not be opened...\n"); //program gives me this as an output
  • }
  • else { //I want the output to be those in this else statement
  • do {
  • printf("Enter Name:\n");
  • fflush(stdin); //flushing the input
  • scanf("%s", name);
  • printf("Enter Student Number:");
  • scanf("%d", &ID);
  • fprintf(cfPtr, "%d %s\n", ID, name);
  • } while (ID == -1); //our stop
  • fclose(cfPtr);
  • }
  • return 0;
  • }
  • Output: File could not be opened...
  • (I can't enter the student details)
  • ````#include<stdio.h>
  • #include<stdlib.h>
  • int main (void){
  • FILE *cfPtr;
  • char name[15];
  • int ID;
  • if ((cfPtr = fopen("students.txt","w")) == NULL){
  • printf("File could not be opened...\n"); //program gives me this as an output
  • }
  • else { //I want the output to be those in this else statement
  • do {
  • printf("Enter Name:\n");
  • fflush(stdin); //flushing the input
  • scanf("%s", name);
  • printf("Enter Student Number:");
  • scanf("%d", &ID);
  • fprintf(cfPtr, "%d %s\n", ID, name);
  • } while (ID != -1);
  • fclose(cfPtr);
  • }
  • return 0;
  • }
  • Output: File could not be opened...
  • (I can't enter the student details)
#3: Post edited by user avatar dumplings‭ · 2021-12-07T10:57:59Z (almost 3 years ago)
  • ````#include<stdio.h>
  • #include<stdlib.h>
  • int main (void){
  • FILE *cfPtr;
  • char name[15];
  • int ID;
  • if ((cfPtr = fopen("students.txt","w")) == NULL){
  • printf("File could not be opened...\n"); //program gives me this as an output
  • }
  • else { //I want the output to be those in this else statement
  • do {
  • printf("Enter Name:\n");
  • fflush(stdin); //flushing the input
  • scanf("%s", name);
  • printf("Enter Student Number:");
  • scanf("%d", &ID);
  • fprintf(cfPtr, "%d %s\n", ID, name);
  • } while (ID == -1); //our stop
  • fclose(cfPtr);
  • }
  • return 0;
  • }
  • Output: File could not be opened...
  • ````#include<stdio.h>
  • #include<stdlib.h>
  • int main (void){
  • FILE *cfPtr;
  • char name[15];
  • int ID;
  • if ((cfPtr = fopen("students.txt","w")) == NULL){
  • printf("File could not be opened...\n"); //program gives me this as an output
  • }
  • else { //I want the output to be those in this else statement
  • do {
  • printf("Enter Name:\n");
  • fflush(stdin); //flushing the input
  • scanf("%s", name);
  • printf("Enter Student Number:");
  • scanf("%d", &ID);
  • fprintf(cfPtr, "%d %s\n", ID, name);
  • } while (ID == -1); //our stop
  • fclose(cfPtr);
  • }
  • return 0;
  • }
  • Output: File could not be opened...
  • (I can't enter the student details)
#2: Post edited by user avatar dumplings‭ · 2021-12-07T10:57:24Z (almost 3 years ago)
  • ````#include<stdio.h>
  • #include<stdlib.h>
  • int main (void){
  • FILE *cfPtr;
  • char name[15];
  • int ID;
  • if ((cfPtr = fopen("students.txt","w")) == NULL){
  • printf("File could not be opened...\n"); //program gives me this as an output
  • }
  • else { //I want the output to be those in this else statement
  • do {
  • printf("Enter Name:\n");
  • fflush(stdin); //flushing the input
  • scanf("%s", name);
  • printf("Enter Student Number:");
  • scanf("%d", &ID);
  • fprintf(cfPtr, "%d %s\n", ID, name);
  • } while (ID == -1); //our stop
  • fclose(cfPtr);
  • }
  • return 0;
  • }
  • ````#include<stdio.h>
  • #include<stdlib.h>
  • int main (void){
  • FILE *cfPtr;
  • char name[15];
  • int ID;
  • if ((cfPtr = fopen("students.txt","w")) == NULL){
  • printf("File could not be opened...\n"); //program gives me this as an output
  • }
  • else { //I want the output to be those in this else statement
  • do {
  • printf("Enter Name:\n");
  • fflush(stdin); //flushing the input
  • scanf("%s", name);
  • printf("Enter Student Number:");
  • scanf("%d", &ID);
  • fprintf(cfPtr, "%d %s\n", ID, name);
  • } while (ID == -1); //our stop
  • fclose(cfPtr);
  • }
  • return 0;
  • }
  • Output: File could not be opened...
#1: Initial revision by user avatar dumplings‭ · 2021-12-07T10:56:47Z (almost 3 years ago)
When I run this program, it gives me the output of the 'if statement', how can I access the 'else statement' for me to enter the student details?
````#include<stdio.h>
#include<stdlib.h>
int main (void){
  
   FILE *cfPtr; 
   char name[15];
   int ID;
   if ((cfPtr = fopen("students.txt","w")) == NULL){
    printf("File could not be opened...\n"); //program gives me this as an output
   }
   else { //I want the output to be those in this else statement
        do {
            printf("Enter Name:\n"); 
            fflush(stdin); //flushing the input
            scanf("%s", name); 
            printf("Enter Student Number:");
            scanf("%d", &ID);

            fprintf(cfPtr, "%d %s\n", ID, name); 
        } while (ID == -1); //our stop

        fclose(cfPtr); 
   }
    return 0; 
}