0

The code is supposed to Accept Data,Display the entire data stored till the current execution,Modify or make changes to the already accepted data. WHAT IM LOOKING FOR IN AN ANSWER:how to write the code for a Modify module? how to correct the Add or accept data,Display
data so they function perfectly.

#include<stdio.h>

typedef struct
{
    int select;
    char lastname[25];
    char firstname[25];
    char address[25];
    int phonenumber[25];
} addressbook;

#define ARRAYLEN 2       //The lecturer told to use this "#define ARRAYLEN 2

addressbook a[ARRAYLEN];   //but why 2?and why use ARRAYLEN 2 with define
FILE *fp;

int main()                 //how is it affecting my program
{
    int i;
    int c;
    int fgetc(FILE *stream);
    int fputc(int c,FILE *stream);
    fp = fopen("addressbook.dat","a+");

    //ADD
    for( i=0; i<ARRAYLEN ; ++i)
    {
        printf("enter details\n");
        printf("enter lastname:\n");
        scanf("%s", a[i].lastname);
        printf("enter firstname:\n");
        scanf("%s", a[i].firstname);
        printf("enter address:\n");
        scanf("%s", a[i].address);
        printf("enter phone number:\n");
        scanf("%d", a[i].phonenumber);
        fwrite(&a[i], sizeof(a), 1, fp); /* notice, array indexed */
    };
    fclose(fp);

    //DISPLAY
    fopen("addressbook.dat", "r");
    for(i=0; i<ARRAYLEN; ++i)
    {
        fread(&a[i], sizeof(a), 1, fp );
        fgetc;
        printf("first name is %s",a[i].firstname);
        printf("last name is %s",a[i].lastname);
        printf("the address is %s",a[i].address);
        printf("the phonenumber is %d",a[i].phonenumber);
    };
    fclose(fp);

    return 0;
}

IMPORTANT NOTE: I use a windows vista os and the compiler is Turbo C++

WhozCraig
  • 65,258
  • 11
  • 75
  • 141
yasaaMoin
  • 31
  • 7
  • Unrelated, man, you'd be hard pressed to pit a worse OS with a worse tool-chain. – WhozCraig Sep 11 '16 at 08:36
  • i know but theres no way i can afford a better PC or laptop. – yasaaMoin Sep 11 '16 at 08:42
  • its sad but yeah..Also im having a lot of trouble trying to install any other compiler,so this would have to do for the time being – yasaaMoin Sep 11 '16 at 08:44
  • Yeah, I understand, just kinda sucks. Anyway, yeah, there's a few problems here, but not terrible. Your `phonenumber` reading and writing is broken, and your `fgetc;` makes no sense at all. Modifying a record can be done a multitude of ways, so there is no single answer to that. You have to (a) have the record, (b) change its values, and finally (c) either resave the entire file or in your case you can `fseek` to the proper location and blast over the old data record (can only be done with fixed-length records, which yours are). Keep at it. – WhozCraig Sep 11 '16 at 08:56

0 Answers0