Question

In: Computer Science

Hi, I am working on this assignment where we have to read and write to a...

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);

}

Solutions

Expert Solution

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;

}


Related Solutions

I am working on a lab practical write up in which we have to measure the...
I am working on a lab practical write up in which we have to measure the acceleration due to gravity by observing carts rolling down an incline. I have to plot a scatter plot and use a program with Linear Least Squares Fit to calculate the gradient of the graph and uncertainty for this value. This program works but I don't know how to combine the uncertainty given for the Linear Fit with the uncertainties associated with my input x...
Hi i am doing an assignment on the financial crisis. I am up to the conclusion...
Hi i am doing an assignment on the financial crisis. I am up to the conclusion and i am having trouble writing it can you please write up a summary of the financial crisis. Thanks
Hello, I am working on an assignment but I am unsure of how to solve it....
Hello, I am working on an assignment but I am unsure of how to solve it. Please help me. The assignment details are below. Consider this scenario: Your friend starts a website, nothingbutflags.com, which is not making money. Your friend asks you to help generate more traffic. You ask your friend how much traffic the website had last month? And your friend replies and says only 500 visits. You also ask how many flags did you sell? Your friend replies...
Hello, I am working on an assignment but I am unsure of how to solve it....
Hello, I am working on an assignment but I am unsure of how to solve it. Please help me. The assignment details are below. Consider this scenario: Your friend starts a website, nothingbutflags.com, which is not making money. Your friend asks you to help generate more traffic. You ask your friend how much traffic the website had last month? And your friend replies and says only 500 visits. You also ask how many flags did you sell? Your friend replies...
Hi I am working on my chemistry homework about acids and bases and I am confused...
Hi I am working on my chemistry homework about acids and bases and I am confused on this question from a lab. We used grape juice and cranberry juice and we had to put HCl and NaOH in the test tube full of juice. 1. Referring back to procedure 1 of the experiment, which of the two juices is more useful as a general pH indicator and why? I think it is grape juice but I just don't know what...
Hi! I am working on a lab for Organic Chemistry and I want to make sure...
Hi! I am working on a lab for Organic Chemistry and I want to make sure my answers are correct before submitting it! Thank you in advance! Stereoisomerism: A Model Exercise In this experiment you will construct models with your molecular model set that illustrate the concepts of chirality, chiral center (stereogenic center, asymmetric carbon atom), enantiomers, diastereomers, and meso forms. You will also learn about two conventions, R-S and Fischer, for designating the configurations of chiral molecules. You will...
I am working on an accounting assignment and am having problems. Firstly, 1.I need to journalize...
I am working on an accounting assignment and am having problems. Firstly, 1.I need to journalize these entries and post the closing entries 2. i need to prepare Dalhanis multi-step income statement and statement of owners equity for August 2010 3. i need to prepare the blance sheet at august 31,2010 4. i need to prepare a post-closing trial balance at august 31,2010 DALHANI makes all credit sales on terms 2/10 n/30 and uses the Perpetual Inventory System Aug 1...
I am working on an accounting assignment and am having problems. Firstly, 1.I need to journalize...
I am working on an accounting assignment and am having problems. Firstly, 1.I need to journalize these entries and post the closing entries 2. i need to prepare Dalhanis multi-step income statement and statement of owners equity for August 2010 3. i need to prepare the blance sheet at august 31,2010 4. i need to prepare a post-closing trial balance at august 31,2010 DALHANI makes all credit sales on terms 2/10 n/30 and uses the Perpetual Inventory System Aug 1...
I have the following assignment for probability class: I am supposed to write a routine (code)...
I have the following assignment for probability class: I am supposed to write a routine (code) in MATLAB that does solve the following problem for me: a) Generate N=10000 samples of a Uniform random variable (X) using the rand () command. This Uniform RV should have a range of -1 to +3. b) Generate (Estimate) and plot the PDF of X from the samples. You are not allowed to use the Histogram () command, any commands from the Matlab Statistics...
I am working on a project where I have to create my own organization (stem cell...
I am working on a project where I have to create my own organization (stem cell research) I need to go further in depth about the program or organization in terms of describing the program implementation and the project evaluation...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT