0

The Code doesn't work as expected, The main focus is to modify through files, It stops during while loop and takes no input, Can Someone Please tell me what error is? also is the concept of modifying correct?

#include "stdafx.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <conio.h>


void modify(){
    struct cust
{
    char ID[10];
    char name[25];
    char pnum[15];
    char bill[5];
    char IDn[10];
    char namen[25];
    char pnumn[15];
    char billn[5];
};
struct cust d1;
        struct cust d1;

        struct custnew
        {
            char IDn[10];
            char namen[25];
            char pnumn[15];
            char billn[5];
        };

        int a;
        a = 1;
        while (a == 1) {
            viewmod();
            char id[10];

            FILE *f, *fn;
            f = fopen("customer.txt", "r");
            fn = fopen("customertemp", "w");

            printf("Enter ID to be edited:");
            scanf("%s", &id);



            printf("Select option to be modified 1.ID  2.Name  3.P/No.  4.Bill  5.RETURN\n");

        int opt;
        scanf("%d", &opt);
        switch (opt)

        {
        case 1:
            while (!feof(f))
            {
                fscanf(f, "\nID : %s\tName : %s\tP/No. %s\tBill : %s \n ", &d1.ID, &d1.name, &d1.pnum, &d1.bill);
                if (strcmp(id, d1.ID)==0)
                {
                    printf("Enter New ID : ");//Repeats after entering
                    scanf("%s", &d1.IDn);
                    fprintf(fn, "\nID : %s\tName : %s\tP/No. %s\tBill : %s \n ", &d1.IDn, &d1.name, &d1.pnum, &d1.bill);

                }
                else
                {
                    fprintf(fn, "\nID : %s\tName : %s\tP/No. %s\tBill : %s \n ", &d1.ID, &d1.name, &d1.pnum, &d1.bill);
                }

            }
                fclose(f);
                fclose(fn);
                remove("customer.txt");
                rename("customertemp.txt", "customer.txt");

                break;//More switch statements didn't provide due to repetitiveness 
                Default : printf("");

                return;
}

I am Using structs. The above code is for Modifying files, it takes a array from from struct and changes it Edit : Now it runs until new ID and then gets me repeated time "Enter new id"

Ezlo
  • 13
  • 1
  • 6
  • 2
    Possible duplicate of [Why is “while ( !feof (file) )” always wrong?](https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) – Fantastic Mr Fox Jul 19 '18 at 11:47
  • edit : id is already initialized, id compares d1.ID and select line to be edited. – Ezlo Jul 19 '18 at 11:48
  • Please include the struct for `d2` – Ed Heal Jul 19 '18 at 11:55
  • when asking a question about a run time problem, as this question is doing, always post a [mcve] otherwise the question will soon be closed as 'off topic' – user3629249 Jul 19 '18 at 14:07
  • there is no function defined in the posted code, the `#include` statement are missing, The posted code does not compile. please correct these problems – user3629249 Jul 19 '18 at 14:09

0 Answers0