In: Computer Science
Hi,
I am working on this assignment where we have to read and write to a file. Change any code that needs to be changed, I need the wfile and rfile functions to be edited to work and the rest of the program.
please change the gets() to something that works. if I need to create a .txt file to run the program pls let me know
Thanks in advance for the help
//agalloc.c
// maintains list of agents infile, allocate memory
#include <stdio.h>
#include <stdlib.h>
#define TRUE 1
void listall(void);
void newname(void);
void rfile(void);
void wfile(void);
struct personnel
{
char name[40];
int agnumb;
float height;
};
struct personnel *agptr = NULL;
int n = 0;
void main(void){
while(TRUE) // cycle until user chooses 'q'
{
printf("\n'e' enter new agent\n'l'
list all agents");
printf("\n 'w' write file\\n'r'
read file\n'q' exit: ");
switch(getche())
{
case 'e':
newname(); break;
case 'l':
listall(); break;
case 'w':
wfile(); break;
case 'r':
rfile(); break;
case 'q':
exit(0); break;
default:
puts("\nenter only selections listed");
}//end switch
}//end while
}// end main();
void newname(void){
char numstr[40];
// reallocate memory for new personel
agptr = realloc(agptr, (n+1)*sizeof( struct
personnel));
if(agptr==NULL)
{
printf("\nCan't reallocate
memory");
return;
}
printf("\nRecord %d. \nEnter name: ", n+1); // get
name
gets(agptr[n].name);
printf("\nEnter agent number (3 digits): ");
gets(numstr);
agptr[n].agnumb = atoi(numstr);
printf("Enter height in inches: " );
gets(numstr);
agptr[n].height = atoi(numstr);
}
void listall(void){
int j;
if(n<1)
printf("\nEmpty list");
for(j=0; j<0; j++){
printf("\nRecord number %d\n",
j+1);
printf(" name: %s\n",
agptr[j].name);
printf(" agent
number:%03d\n", agptr[j].agnumb);
printf(" Height:
%4.2f\n", agptr[j].height);
}
}
/*********************************************************
complete this method so that use can save all the records to disk
**********************************************************/
void wfile(void){
FILE *fptr;
if(n<1)
{
printf("\nCan't write empty
list.\n");
return;
}
fclose(fptr);
printf("\n File of %d records written. \n", n);
}
/*********************************************************
Complete this method do user can read the saved information
back to memory
*********************************************************/
void rfile(void){
FILE *fptr;
long int file_size; //size of file in bytes
if((fptr = fopen("agents.rec", "rb")) ==NULL){
printf("\nCan't open file
agents.rec\n"); return;
}
fseek(fptr, 0, SEEK_END); // put file ptr at end of
file
file_size = ftell(fptr); //file size is file
pointer
fseek(fptr, 0, SEEK_SET); //return file potr to
start
//allocate memory for entire file
fclose(fptr);
printf("\nFile read. Total agents is now %d. \n",
n);
}
You created newname() and Listall() methods. Seems which looks good and prints expected values.
As you seek for help to write and read file operations, Below are the two methods and explanation.
//For this you need to create a file in your local/server
folder
//Here i am creating a file in c folder
//
void wfile()
{
string content="";
printf("Please enter content to insert");
content=scanf("%s",&content);
FILE *fp;
//Creates a file if it foesnt exists,else it opens file and
append requested content
//a+ indicates appeds content on write and read from starting
fp = fopen("c/tmp/test.txt", "a+");
// write lines into file using fputs().
fputs(content, fp);
//Closing the opened file
fclose(fp);
}
void rfile(void){
int num;
FILE *fptr;
//Create valiable to carry file data
char str[MAXCHAR];
//Open an exising file with right access
fptr = fopen("c/tmp/test.txt", "r");
//Check if file /exists or not
if(fptr == NULL)
{
printf("File contains no data");
//Exit from the block if file/data couldnt found
exit(1);
}
//Iterate the file to get data
//User fgets ,which reads whole data from a file
while (fgets(str, MAXCHAR, fptr) != NULL)
printf("%s", str);
//Print data which holds in fptr
fclose(fptr);
return 0;
}