Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Relational Database Design With Microcomputer Applications

Authors: Glenn A. Jackson

1st Edition

0137718411, 978-0137718412

More Books

Students also viewed these Databases questions