Question

In: Computer Science

I am Writing a C-Program to read and write files. but none of my code is...

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

*/

}

Solutions

Expert Solution

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

}


Related Solutions

I am writing a program that will work with two other files to add or subtract...
I am writing a program that will work with two other files to add or subtract fractions for as many fractions that user inputs. I need to overload the + and - and << and >> opperators for the assignment. The two files posted cannot be modified. Can someone correct the Fraction.ccp and Frction.h file that I am working on? I'm really close. // // useFraction.cpp // // DO NOT MODIFY THIS FILE // #include "Fraction.h" #include<iostream> using namespace std;...
I am writing a shell program in C++, to run this program I would run it...
I am writing a shell program in C++, to run this program I would run it in terminal like the following: ./a.out "command1" "command2" using the execv function how to execute command 1 and 2 if they are stored in argv[1] and argv[2] of the main function?
how can I get my program to read one of 4 text files (chosen/ inputted by...
how can I get my program to read one of 4 text files (chosen/ inputted by user, game1.txt, game2, game3, or game4.txt). and place its information into variables, one of which is an array. I sort of understand this, but I don't know how to make my program know which parts of the textfile go into which variables. my 4 textfiles are in the format: the list of e4-bc need to be entered into my array. B 35 e4 e5...
I am trying to write a code in C for a circuit board reads in a...
I am trying to write a code in C for a circuit board reads in a temperature from a sensor on the circuit board and reads it onto a 7-segment LED display. How exactly would you code a floating 2 or 3 digit reading onto the LED display? I am stuck on this part.
I need to write a java program (in eclipse) that will read my text file and...
I need to write a java program (in eclipse) that will read my text file and replace specific placeholders with information provided in a second text file. For this assignment I am given a text file and I must replace <N>, <A>, <G>, with the information in the second file. For example the information can be John 22 male, and the template will then be modified and saved into a new file or files (because there will be multiple entries...
C++ Code Writing prompt: Grade Calculation: Write a program that asks the user to enter in...
C++ Code Writing prompt: Grade Calculation: Write a program that asks the user to enter in a number greater than or equal to zero and less than or equal to 100. If they do not you should alert them and end the program. Next, determine the letter grade associated with the number. For example, A is any grade between 90 and 100. Report the letter grade to the user.
I need assistance translating a custom C++ program to MIPS. My C++ code is the following:...
I need assistance translating a custom C++ program to MIPS. My C++ code is the following: I have made numerous attempts on my own to no avail, any assistance is appreciated. Also, template code for this solution is provided below: #include int moveRobots(int *, int *, int, int ); int getNew(int, int); int main() { int x[4], y[4], i, j, myX = 25, myY = 25, move, status = 1; // initialize positions of four robots x[0] = 0; y[0]...
C++ Goals: Write a program that works with binary files. Write a program that writes and...
C++ Goals: Write a program that works with binary files. Write a program that writes and reads arrays to and from binary files. Gain further experience with functions. Array/File Functions Write a function named arrayToFile. The function should accept three arguments: the name of file, a pointer to an int array, and the size of the array. The function should open the specified file in binary made, write the contents into the array, and then close the file. write another...
C Programming Run Length Encoder (Compression). I am writing a program that takes an image (from...
C Programming Run Length Encoder (Compression). I am writing a program that takes an image (from a text file) and looks for patterns of 2 or more duplicate values. The program should replace these values with the following pattern:  2 such characters followed by an Integer ( which represents number of occurrences of each character), followed by an asterisk. For example say the input of the non-compressed image was: ,,,,)H 7. i.e. it consists of four commas, one closed bracket, the...
WITHOUT USING POINTERS. This is C programming. I am writing a program which should calculate standard...
WITHOUT USING POINTERS. This is C programming. I am writing a program which should calculate standard deviation of an array of exam scores and then add the standard deviation to each array element. It should print the original array, the standard deviation, and the new rounded adjusted array. I included my question as a comment in the line of code that is giving me an issue. (Removing the a from E-x-a-m as Chegg doesn't allow the word.) #include <stdio.h> #include...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT