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"