In: Computer Science
I am Writing a C-Program to read and write files. but none of my code is working like it should be. Please fix all code and supply output response. Please try to use existing code and code in comments. But if needed change any code that needs to be changed.
Thank you in advance
//agelink.c
//maintains list of agents
//uses linked list
#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(getchar())
{
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){
struct personnel *ptr;
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;
}
//THIS CODE ISN'T WORKING
printf("\nRecord %d. \nEnter name: ", n+1); // get name
scanf("%s", agptr[n].name);
printf("\nEnter agent number (3 digits): ");
scanf("%d", &agptr[n].agnumb);
printf("Enter height in inches: " );
scanf("%f",&agptr[n].height);
}
//THIS CODE DOESN'T WORK, PLS FIX
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:%d \n", agptr[j].agnumb);
printf(" Height: %f \n", agptr[j].height);
}
}
/*********************************************************
complete this method so that use can save all the records to disk
**********************************************************/
void wfile()
{
char Info[50];
FILE *fptr;
fptr = fopen("C:\\FILE_IO.txt","w");
if(fptr == NULL)
{
printf("Error!");
exit(1);
}
printf("Enter the agents information Below: \n");
scanf("%s", Info);
fclose(fptr);
}
void rfile(void){
char Info[50];
FILE *fptr;
if ((fptr = fopen("C:\\FILE_IO.txt","r")) == NULL){
printf("Error! opening file");
exit(1);
}
fscanf(fptr,"%s", Info);
fclose(fptr);
return;
//calculate how many structures is the value of the n
/* USE THIS CODE TO WRITE THE rfile FUNCTION
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);
*/
}
#include <stdio.h>
#include<string.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;
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: ");
char ch;
scanf(" %c",&ch);
switch(ch)
{
case 'e': newname(); break;// enter new agent
case 'l': listall(); break;// list all agents
case 'w': wfile(); break;// write agents to file
case 'r': rfile(); break;// read agents from file and store in list
case 'q': exit(0); break;// exit
default:
puts("\nenter only selections listed");
}//end switch
}//end while
}// end main();
// function to enter new agent
void newname(void){
// reallocate memory for new personel
agptr = realloc(agptr, (n+1)*sizeof( struct personnel));
if(agptr==NULL)// memory not allocated
{
printf("\nCan't reallocate memory");
return;
}
printf("\nRecord %d. \nEnter name: ", (n+1)); // get name
scanf("%s", agptr[n].name);
printf("\nEnter agent number (3 digits): ");
scanf("%d", &agptr[n].agnumb);
printf("Enter height in inches: " );
scanf("%f",&agptr[n].height);
n++;
}
// function to display all agents in list
void listall(void){
int j;
if(n<1)
printf("\nEmpty list");
for(j=0; j<n; j++){
printf("\nRecord number %d \n", (j+1));
printf(" name: %s \n", agptr[j].name);
printf(" agent number:%d \n", agptr[j].agnumb);
printf(" Height: %f \n", agptr[j].height);
}
}
// function to write all agents of list to file
void wfile()
{
char Info[50];
FILE *fptr;
fptr = fopen("FILE_IO.txt","wb");
if(fptr == NULL)
{
printf("Error!");
exit(1);
}
for(int j=0;j<n;j++){// write all n agents
fwrite (&agptr[j], sizeof(struct personnel), 1,fptr);
}
printf("Agents successfully written to file");
fclose(fptr);
}
// function to read agents from file and then store them in list
void rfile(void){
char Info[50];
FILE *fptr;
if ((fptr = fopen("FILE_IO.txt","rb")) == NULL){
printf("Error! opening file");
exit(1);
}
struct personnel temp;
printf("Reading agents from file:\n");
while(fread(&temp, sizeof(struct personnel), 1,fptr)) {// loop till agent is present in file
printf ("%s, %d, %f\n", temp.name,temp.agnumb,temp.height);
agptr = realloc(agptr, (n+1)*sizeof( struct personnel));
// store agents in list of agents
strcpy(agptr[n].name,temp.name);
agptr[n].agnumb=temp.agnumb;
agptr[n].height=temp.height;
n++;
}
printf("Total number of agents in list: %d",n);
fclose(fptr);
return;
}