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
#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...
#8: Post edited
- ```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 statementdo {printf("Enter Name:\n");fflush(stdin); //flushing the inputscanf("%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
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
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...
#4: Post edited
- ````#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
- ````#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
- ````#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
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; }