Question: I want to write a program in C where if the user chooses 1, the user can write stuff in the text file called something.txt
I want to write a program in C where if the user chooses 1, the user can write stuff in the text file called "something.txt" within the terminal. If the user chooses 2, reads what's written in something.txt. Feel free to edit the following code and tell me what I did wrong here.
#include
int main() { int a; char c[50]; char x; FILE *pwrite; FILE *pread; printf("Chose an option: "); printf(" 1. to write. 2. to read. Other number. to exit"); scanf("%d", &a);
if (a == 1) {
pwrite = fopen("something.txt", "wb"); fwrite(&c, sizeof(char), 50, pwrite); for(int b = 0; b != 10; b++) { fscanf(pwrite, "%c", &x); c[b] = x; } fclose(pwrite);
} else if (a == 2) { // code to read the stuff I wrote in the text file. Still very unsure about how to do it. pread = fopen("something.txt", "r");
do { fread(&c, sizeof(char), 50, pread); } while(); }
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
