Related
Im basically Writing a program that creates, reads, updates and
deletes records in a binary file.
Everything compiles correctly, no syntax errors, but I do have some
bugs.
KNOWN BUGS
1.) Imputing any strings does not work, using fgets
2.) Ctrl-D Does Work but outputs a 'default' error before it exits.
3.) Update does not work (Not my main issue at the moment as the others are more important for now.)
4?) I'm not sure if the menu is working how it's supposed to work. I
think the do while is correct, since in the menu if I select and hit
CTRL-D it does exit the program. Just wanna be sure.
Right now I just want to know why, It is skipping the courseName in
the inputs function.
Here is my code thus far
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <errno.h>
#include <string.h>
typedef struct{
char courseName [64];
char courseSched [4];
unsigned int courseHours;
unsigned int courseSize;} COURSE;
FILE *pfileCourse;
int courseNumber = 0;
//Prototypes
void inputDetails(COURSE *c);
void readCourseRecord();
void createCourseRecord();
void print_menu();
void modifyCourseInfo();
void deleteCourse();
void display(COURSE c);
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[]) {
char choice; // this is the choice
printf("Enter one of the following actions or press CTRL-D to exit\n");
printf("C - Create a new course record\n");
printf("R - Read an existing course record\n");
printf("U - Update an existing course record\n");
printf("D - Delete an existing course record\n");
do{
choice = getchar();
switch(choice) {
case 'c':
case 'C':
printf("YOU PICKED C for Create\n");
createCourseRecord();
break;
case 'r':
case 'R':
printf("This is Choice R\n");
readCourseRecord();
break;
case 'u':
case 'U':
printf("Here is where you update an existing course\n");
modifyCourseInfo();
break;
case 'd':
case 'D':
printf("here is where you Delete an existing course record\n");
deleteCourse();
break;
default:
printf("Wrong Choice!\n");
}
}while(choice != EOF);
return 0;
}
void createCourseRecord() {
COURSE data;
pfileCourse = fopen("courses.dat", "ab");
printf("Please Enter The Details of The Course\n");
inputDetails(&data);
fwrite(&data, sizeof(data), 1, pfileCourse);
fclose(pfileCourse);
printf("Course Has Been Created!\n");
}
void inputDetails(COURSE *c) {
printf("Enter a course number: \n");
scanf("%d", &courseNumber);
printf("Enter a Course Name: \n");
fgets(c->courseName, sizeof(courseName), stdin);
printf("Enter the course schedule (MWF or TR): \n");
fgets(c->courseSched, 4, stdin);
fflush(stdin);
printf("Enter the course credit hours: \n");
scanf("%d",&c->courseHours);
fflush(stdin);
printf("Enter Number of Students Enrolled: \n");
scanf("%d",&c->courseSize);
return;
}
void readCourseRecord(){
COURSE data;
int flag = 0;
int readCourseNumber = 0;
printf("Please Enter a Course Number to Display\n");
scanf("%d", &readCourseNumber);
fflush(stdin);
pfileCourse = fopen("courses.dat", "rb");
while((fread(&data, sizeof(data), 1, pfileCourse)) > 0) {
if(readCourseNumber == courseNumber)
{
display(data);
flag = 1;
}
}
fclose(pfileCourse);
if(flag == 0)
printf("Course not Found!\n");
}
void deleteCourse(){
int newCourseNum;
COURSE data;
FILE *file2;
printf("Please Enter The Course You Wish You Delete\n");
scanf("%d", &newCourseNum);
pfileCourse = fopen("courses.dat", "rb");
file2 = fopen("temp.dat", "wb");
rewind(pfileCourse);
while((fread(&data, sizeof(data), 1, pfileCourse)) > 0)
{
if(courseNumber != newCourseNum)
{
fwrite(&data, sizeof(data), 1, file2);
}
}
fclose(file2);
fclose(pfileCourse);
remove("courses.dat");
rename("temp.dat", "courses.dat");
printf("%d was Successfully deleted\n", newCourseNum);
}
void modifyCourseInfo()
{
COURSE data;
int newCourseNum, found = 0;
printf("Modify\n");
printf("Please Enter The Course You Wish You Modify\n");
scanf("%d", &newCourseNum);
pfileCourse = fopen("courses.dat", "rb+");
while ((fread(&data, sizeof(data), 1, pfileCourse)) > 0 && found == 0)
{
if (courseNumber == newCourseNum)
{
display(data);
printf("Please Enter New Details\n");
inputDetails(&data);
fseek(pfileCourse, - (long)sizeof(data), 1);
fwrite(&data, sizeof(data), 1, pfileCourse);
printf("Course Updated\n");
found == 1;
}
}
fclose(pfileCourse);
if(found == 0)
printf("ERROR: course not found\n");
}
void display(COURSE c){
printf("courseNumber:\t %d\n", courseNumber);
printf("courseName:\t %s\n",c.courseName);
printf("courseSched:\t %s\n",c.courseSched);
printf("courseName:\t %d\n",c.courseHours);
printf("courseSize:\t %d\n",c.courseSize);
}
It doesn't skip courseName, courseName just gets value '\n' because scanf function stops reading your input BEFORE white space. Scanf ignores any whitespace characters encountered before the next non-whitespace character. So you can just add
scanf("%d[^\n]", &courseNumber);
getchar();
after every scanf you have but I'd recommend you to use fgets function for every interactive input.
I am tasked to create an inventory program that stores data using structure and save the data into a binary file. The program should then be able to load the data i.e. the structure elements from the binary file. I am able to save the input data to a binary file using the fwrite function; however, I am having a problem in loading the file to the program using the fread function.
I already looked for similar problems but I can't find a similar case wherein the fread is used to pass the structure elements saved to the structure variable in the program.
This is the code for my structure
struct details {
char name[30];
double price;
int code;
int qty;
};
details item[SIZE];
Function in adding an item to the inventory
int AddItem(int n){
details inputitem;
printheader();
printf("Item Name: ");
scanf("%s", inputitem.name); //check %[^\n]
printf("\nItem Price: ");
scanf("%lf", &inputitem.price);
printf("\nItem Code: ");
scanf("%d", &inputitem.code);
printf("\nQuantity: ");
scanf("%d", &inputitem.qty);
printf("\nItem Added! The ID of the item is %d.\n", n+1);
item[n] = inputitem;
}
Function in displaying the inventory
int DisplayInventory(int n){
printheader();
printf("ID | NAME | CODE | QUANTITY | PRICE \n");
printf("------------------------------------------------------------\n");
for(int i=0; i<n; i++){
printf("%d %-18s %-10d %-10d %-10lf \n",i+1,item[i].name,item[i].code,item[i].qty,item[i].price);
}
}
Function in saving to a binary file
int SaveFile(int n){
FILE *fp;
fp=fopen("C:\\Users\\Royce\\Documents\\CPEPROG2 Goden Final Project\\Inventory.txt","wb");
if(!fp) {
printf("Cannot open file.\n");
system("pause");
exit(1);
}
for(int i=0; i<n; i++){
fwrite(&item[i], sizeof(struct details), 1, fp);
}
fclose(fp);
printf("File is saved!\n");
}
Function in loading the binary file
int LoadFile(int n){
FILE *fp;
fp=fopen("C:\\Users\\Royce\\Documents\\CPEPROG2 Goden Final Project\\Inventory.txt","rb");
if(!fp) {
printf("Cannot open file.\n");
system("pause");
exit(1);
}
struct details inputitem;
int i = 0;
while(fread(&inputitem, sizeof(struct details),1, fp)){
item[i] = inputitem;
i++;
/* printf("%d %-18s %-10d %-10d %-10lf \n",i+1,item[i].name,item[i].code,item[i].qty,item[i].price); */
}
fclose(fp);
printf("File is loaded!\n");
}
I expect the program to show in the DisplayInventory function the details of the structure saved in the binary file. However, nothing shows up at all.
When i try to print the structure in the LoadFile function (using the commented line), all variables show 0.
EDIT
Main Function
int main (){
int choice; //gets the choice of user from the menu
bool condition = 1; //loops the menu
//details item[50];
int count=0; //counts the number of items in the inventory
do{
printheader(); //prints the title of the program
printmenu(); //prints the menu (list of commands)
scanf("%d", &choice);
switch(choice){
case 1: system("cls");
AddItem(count);
count++;
system("PAUSE");
system("cls");
break;
case 2: system("cls");
//EditItem();
system("PAUSE");
system("cls");
break;
case 3: system("cls");
//DeleteItem();
system("PAUSE");
system("cls");
break;
case 4: system("cls");
//ViewItem();
system("PAUSE");
system("cls");
break;
case 5: system("cls");
DisplayInventory(count);
system("PAUSE");
system("cls");
break;
case 6: system("cls");
SaveFile(count);
system("PAUSE");
system("cls");
break;
case 7: system("cls");
LoadFile(count);
system("PAUSE");
system("cls");
break;
case 8: printf("\nThank you!");
exit(0);
break;
default: printf("\nInvalid Input!\n");
getch();
system("cls");
}
}while(condition = 1);
return 0;
}
The program never increments the 'count' variable, so when 'DisplayInventory' is called the value is always 0 and nothing is displayed :
return the i variable in 'LoadFile()' and assign it to the 'count' variable in the main() scope :
in LoadFile :
...
printf("File is loaded!\n");
return i;
}
in main :
...
case 7: system("cls");
count = LoadFile(count);
system("PAUSE");
system("cls");
Note that the 'n' argument in 'int LoadFile(int n)' is not used, you could remove it.
It's my first time asking here, so I'm sorry in advance if my post is a bit messy. I'm a freshman and my finals is to make an atm program.
My program uses switch statements for the options: the first one asks the user for their info, (account number, account name, PIN, initial deposit), while the second one is for the actual transactions: Balance check, Deposit and Withdrawal. Before you could do any one of those options, of course I'll have to check if the info (in this case the account number and PIN) matches the ones in my file.
The file is in binary. My problem is that my code for reading the file works fine if i want to display the contents of my file (all the data entries that I populated the file with are shown), but when i want to use the contents of the file (searching inside the file for a match of the user inputted account number) it only reads the first line of the file, thus only the first data entry works, while the one's after that are not read.
My question is, what am i doing wrong in my code? its confusing because if I change my code to display the contents it shows everything, meaning it reads the whole file. but when i want to search the file using a conditional statement, only the first data entry gets read. Thank you very much for your time
tldr: can't use data in file for conditionals (verify the data inside the file, cuz my code doesn't read it whole apparently, except for the first entry)
but if i print the contents it reads it fully
my outputs
1.My data entries
data entries
2.Entering the Account no. of the 1st one (desired output)
desired output
3.Entering The account no. of the 2nd one (Problem part)
problem
my main code
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
#include<stdlib.h>
#include<string.h>
#include<process.h>
struct account
{
int no;
char name[100];
int pin;
float id;
};
main()
{
FILE *fptr;
fptr = fopen("accrec.dat","ab");
if (fptr == NULL)
{
printf("File does not exists \n");
return 0;
}
system("cls");
struct account accrec;
int i,tpin[4];
char step1,ch;
printf("\t\tWelcome to Banking System\n\t");
printf("\nA.Open an Account\n");
printf("\nB.Bank Transaction\n");
printf("\nC.exit\n");
scanf("%c",&step1);
switch (step1)
{
case 'A':
printf("Open a New Account\n");
printf("\nEnter the following information\n");
printf(" \n5-digit Account number:");
scanf("%d",&accrec.no);
getchar();
printf("\nAccount Name:");
scanf("%[^\n]s",&accrec.name);
printf("\n4-digit Account PIN:");
/*for(i=0;i<4;i++)
{
ch = getch();
tpin[4] = ch;
ch = '*' ;
printf("%c",ch);
}mask works but does not allow PIN saving */
scanf("%d",&accrec.pin);
printf("\nInitial deposit:");
scanf("%f",&accrec.id);
fwrite(&accrec,sizeof(struct account),1,fptr);
fclose(fptr);
break;
case 'B':
{
fptr = fopen("accrec.dat","rb");
int choice;
int accno = 0;
printf("Enter Your Account no.");
scanf("%d",&accno);
while (fread(&accrec,sizeof(account),1,fptr)!=NULL)
{
if(accno == accrec.no)
{
printf("\tWelcome to PUPQC Banking System\n");
printf("1.Balance Inquiry\n");
printf("2.Deposit\n");
printf("3.Withdrawal\n");
printf("4.quit\n");
scanf("%d",&choice);
}
else
{
printf("account doesn't exist\n");
exit(1);
}
fclose(fptr);
switch (choice)
{
case 1:
printf("BALANCE INQUIRY\n");
printf("Current Balance:");
fptr = fopen("accrec.dat","rb");
if (fptr == NULL)
{
printf("File Cant be read");
exit(1);
}
printf("Account No: %d\n",accrec.no);
while(fread(&accrec,sizeof(struct account),1,fptr)!=NULL);
{
printf("Initial Deposit is %0.2f\n",accrec.id);
}
printf("%d\n",&accrec.id);
break;
case 2:
float dv;
printf("DEPOSIT\n");
printf("Current Balance:");
fptr = fopen("accrec.dat","rb");
if (fptr == NULL)
{
printf("File Cant be read");
exit(1);
}
while(fread(&accrec,sizeof(struct account),1,fptr)!=NULL);
{
printf("%0.2f\n",accrec.id);
}
printf("Enter amount to deposit:\n");
printf("Deposit Value:");
scanf("%0.2f",&dv);
accrec.id = accrec.id + dv;
fwrite(&accrec,sizeof(struct account),1,fptr);
fclose(fptr);
break;
case 3:
float wv;
printf("WITHDRAWAL\n");
printf("Current Balance:");
fptr = fopen("accrec.dat","rb+");
if (fptr == NULL)
{
printf("File Cant be read");
exit(1);
}
while(fread(&accrec,sizeof(struct account),1,fptr)!=NULL);
{
printf("%0.2f\n",accrec.id);
}
printf("Enter amount to withdraw:\n");
printf("Withdrawal Value:");
scanf("%0.2f",wv);
accrec.id = accrec.id - wv;
fwrite(&accrec,sizeof(struct account),1,fptr);
fclose(fptr);
break;
case 4:
printf("thank you for your patronage \n");
return 0;
break;
default:
printf("error 404");
break;
}
}
}
break;
case 'C':
printf("Thank you! Have a good day");
return 0;
break;
case 'D':
fptr = fopen("accrec.dat","rb");
if (fptr == NULL)
{
printf("File Cant be read");
exit(1);
}
printf("Data From file\n");
while(fread(&accrec,sizeof(struct account),1,fptr)!=NULL)
printf("\n acc.no is %d\n acc.name is %s\n PIN %d\n Initial Deposit is %0.2f\n ",accrec.no,accrec.name,accrec.pin,accrec.id);
fclose(fptr);
getch();
break;
default:
printf("invalid input! please select form the options given\n");
}
}
my structure
struct account
{
int no;
char name[100];
int pin;
float id;
};
code for finding a match of account number in my file
(the part i'm having trouble with)
fptr = fopen("accrec.dat","rb");
int choice;
int accno = 0;
printf("Enter Your Account no.");
scanf("%d",&accno);
while (fread(&accrec,sizeof(account),1,fptr)!=NULL)
{
if (accno == accrec.no)
{
printf("\tWelcome to Banking System\n");
printf("1.Balance Inquiry\n");
printf("2.Deposit\n");
printf("3.Withdrawal\n");
printf("4.quit\n");
scanf("%d",&choice);
}
else
{
printf("account doesn't exist\n");
exit(1);
}
}
fclose(fptr);
The expected output is that my conditional statement works properly and reads my whole file for matches.
try to change the exit(1) to continue;, the exit(1) will make your program quit the while loop and exit the program immediately whenever accno != accrec.no.
in fact, you don't need the else statement inside the while loop, you should put it outside the while loop. for example:
scanf("%d",&accno);
boolean found = false;
while (fread(&accrec,sizeof(account),1,fptr)!=NULL)
{
if (accno == accrec.no)
{
found = true;
scanf("%d",&acpin);*/
printf("\tWelcome to Banking System\n");
printf("1.Balance Inquiry\n");
printf("2.Deposit\n");
printf("3.Withdrawal\n");
printf("4.quit\n");
scanf("%d",&choice);
}
}
if (!found) {
printf("account doesn't exist\n");
}
fclose(fptr);
This code read/write data to/from binary file. Here modify() will take input from user, search for it in the file. If found, will prompt user to give new entry and the older entry would be replaced.
The question is, in modify() after fwrite() if I break the while loop then everything would be fine but, if I don't then the entry would still be modified but along with that the content of the file would be duplicated, why so ?
As in the beginning there are no duplicate entries in the file. So what I expect is, that even if I don't use break the loop should go on and finally terminate when whole file has been read. Here, the if(strcmp(e.name,user)==0) would only be true for one entry therefore the control should enter only once in this if block. Then how the entries are getting duplicated ?
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void add(FILE *);
void list(FILE *);
void modify(FILE *);
struct emp
{
char name[20];
int id;
float sal;
}e;
void add(FILE *f)
{
char *p=NULL;
printf("\nEnter name id sal\n");
scanf(" %s %d %f", e.name,&e.id,&e.sal);
fseek(f,0,SEEK_END);
if((fwrite(&e,sizeof(struct emp),1,f))==1)
{
printf("\nAdded Successfully\n");
}
else
{
printf("\nError wrting to file in ADD func\n");
}
}
void list(FILE *f)
{
rewind(f);
while(fread(&e,sizeof(struct emp),1,f)>0)
{
printf("\nRead %s %d %f\n",e.name,e.id,e.sal);
}
}
void modify(FILE *f)
{
char user[20];
char *p=NULL;
printf("\nEnter name to modify\n");
scanf(" %s", user);
rewind(f);
while(fread(&e,sizeof(struct emp),1,f)==1)
{
//printf("\n --------------- %s %d %f\n",e.name,e.id,e.sal);
if(strcmp(e.name,user)==0)
{
//fseek(f,-sizeof(struct emp),SEEK_CUR);
printf("\nEnter new name id salary\n");
scanf(" %s %d %f", e.name,&e.id,&e.sal);
fseek(f,-sizeof(struct emp),SEEK_CUR);
if(fwrite(&e,sizeof(struct emp),1,f)==1)
{
printf("\nModified successfull!!\n");
//break;
}
else
{
printf("\nError while modifying\n");
}
}
else
{
printf("\n\nstring not matched\n\n");
}
}
}
int main()
{
char val='T';
FILE *fp=NULL;
if((fp=fopen("database.dat","rb+"))==NULL)
{
if((fp=fopen("database.dat","wb+"))==NULL)
{
printf("\nError opening file in wb+ mode\n");
exit(0);
}
}
do
{
printf("\nEnter a to add, l to list, d to delete, m to modify and e to exit\n");
scanf(" %c", &val);
switch(val)
{
case 'a':
add(fp);
break;
case 'l':
list(fp);
break;
case 'm':
modify(fp);
break;
case 'd':
// del(fp);
break;
case 'e':
fclose(fp);
exit(0);
break;
default:
printf("\nInvalid Input\n");
break;
}
}
while(1);
}
I think it's a problem between fread() and fwrite().
After you use fwrite() to modify the emp, you continue the while loop without using fseek().
In fact, before, you used fseek() to go back and it was OK.
Now you have to place another fseek() which doesn't move, like this:
fseek(f, 0, SEEK_CUR);
Now you can remove the break and your Modify() function would seems like this:
void modify(FILE *f)
{
char user[20];
printf("\nEnter name to modify\n");
scanf("%s",user);
fflush(stdin);
rewind(f);
while(fread(&e,sizeof(struct emp),1,f)==1)
{
if(strcmp(e.name,user)==0)
{
printf("\nEnter new name id salary\n");
scanf("%s %d %f",e.name,&e.id,&e.sal);
fflush(stdin);
fseek(f,-sizeof(struct emp),SEEK_CUR);
if(fwrite(&e,sizeof(struct emp),1,f)==1)
{
printf("\nModified successfull!!\n");
//break; //This can be removed
fseek(f, 0, SEEK_CUR); //Place this here
}
else
{
printf("\nError while modifying\n");
}
}
else
{
printf("\n\nstring not matched\n\n");
}
}
}
I've got a question concerning C programming.It is concerning binary files.In my code I(at least imagined that I) made it read and write from a binary file,but it won't do that for some reason.I've tried changing some things but I just don't see the error anywhere.And it is just this minimal detail which is giving me a hard time,it is all that is left to make in this code before it is done and then I'm done with the program altogether,so it gets on my nerves a bit more than it would usually.Please point out what it is I haven't done and what i should do to make it read from and write to binary files properly:
#define MAX 4
//========================-STRUCTURE-=====================================================================
struct person{
char name[30];
char mail[30];
char tele[30];
};
struct person p[MAX];
//========================-MAIN FUNCT-===============================================================
int main()
{
bool continue=true,exists=false;
int answer,i;
char filnme[20];
FILE *fil;
printf("Would you like to open an already existing file?[y/n]: ");
scanf("%d",&answer);
if(answer=='y'||answer=='Y')
{
finns=true;
}
else finns=false;
getchar();
printf("\n\nType the name of the file you will use: ");
scanf("%s",filnme);
if(exists)
{
fil = fopen(filnme,"rb");
for(i=0;i<MAX;i++)
{
while(feof(fil)==0)
{
fread(&p, MAX, 1, fil);
printf("%s %s %s\n",p.name,p.mail,p.tele);
}
}
fclose(fil);
}
else fil = fopen(filnme,"wb");
do
{
system("cls");
int choice;
printf("What would you like to do?");
printf("\n_______________\n");
printf("1. Add another person to the list\n");
printf("2. Print the entire list\n");
printf("3. Remove information from the list\n");
printf("4. Sort\n");
printf("5. Change information\n");
printf("6. Search\n");
printf("7. Exit\n");
printf("\nChoice: ");
scanf("%d",&choice);
printf("\n");
system("cls");
switch(choice)
{
case 1:
addtolist();
getch();
break;
case 2:
write();
getch();
break;
case 3:
remove();
getch();
break;
case 4:
sort();
getch();
break;
case 5:
change();
getch();
break;
case 6:
search();
getch();
break;
case 7:
continue=false;
break;
default:
printf("Not a valid choice!");
getch();
break;
}
}while(continue);
for(i=1;i<=MAX;i++)
{
fwrite(&p, MAX, 1, fil);
}
fclose(fil);
system("cls");
}
Nevermind the declaration of functions or such,I've got that covered,just the binary file handling I put into the main.
Thanks in advance!
You are not using fread and fwrite correctly.
fread(&p, MAX, 1, fil);
should be:
fread(&p[i], sizeof(person), 1, fil);
Similarly for the fwrite call.
An alternative to calling fread and fwrite in a loop is to read/write the entire array in one go, like so:
fread(&p, sizeof(person), MAX, fil);
continue is a reserved keyword in C/C++ - don't use that as a boolean variable name.
finns is not declared (assumed to be bool)
You aren't checking the return value of fopen. How do you know if the file was successfully opened?
Your while loop on fread does't look anywhere near right.
This looks much better
int records_read = 0;
size_t count;
count = 0;
for (i = 0; i < MAX; i++)
{
count = fread(&p[i], sizeof(p[i]), 1, fil);
if (count == 0)
{
break;
}
records_read++;
}
Same for your write loop (note that I adjusted the starting value of "i")
for(i=0;i<MAX;i++)
{
fwrite(&p[i], sizeof(p[i]), 1, fil);
}
Apart from other things, scanf("%d", &answer) attempts to read a number, not a y/n character.