just want to add for example 5 vehicles to my file if it's more than that, the program should give a message that the registry is full.
void add_vehicle(){
char another;
FILE *fp;
int n,i;
struct vehicle info;
do{
printf("\t\t\t\t=======Add vehicle Info=======\n\n\n");
fp=fopen("information.txt","a");
printf("\n\t\t\tEnter Owner name :");
scanf("%s",info.owner);
printf("\n\t\t\tEnter vehicle-No :");
scanf("%d",&info.car_no);
printf("\n\t\t\tEnter age : ");
scanf("%d",&info.age);
printf("\n\t\t\tEnter vehicle type : ");
scanf("%s",info.car_type);
printf("\n\t\t\tEnter vehicle mark : ");
scanf("%s",info.car_mark);
printf("\n\t\t\tEnter registration nummber : ");
scanf("%s",info.car_registration);
printf("\n\t\t\t______________________________\n");
if(fp==NULL){
fprintf(stderr,"can't open file");
}
else{
printf("\t\t\tRecord stored successfuly\n");
}
fwrite(&info, sizeof(struct vehicle), 1, fp);
fclose(fp);
}
I don't have any idea if I should use a while loop or if statement and how in that case.
Related
I am making a simple database to book restaurant seats using basic file handling. I want to check if the user has entered an already booked seat number. As I am inputting the seat number while in append mode, how do I check if the seat number inputted by the user is already stored in the file or not. Here is a piece of my code -:
struct restaurant
{
int seats, seatNo, seatAmount, pizzaQt, burgerQt, drinkQt, pizzaPrice, burgerPrice, drinkPrice;
char name[20], restaurantName[20];
int foodTotal, total;
int counter;
};
int book()
{
FILE *fp;
struct restaurant r;
char choice, choice1;
r.counter=0;
fp=fopen("Restaurant.txt", "a");
if(fp==NULL)
{
printf("\nFailed to open the file.");
exit(1);
}
else
{
do
{
printf("Proceed to booking...");
getch();
printf("\n\t\t\t\t---BOOK SEAT---");
printf("\n\t\t\tEnter your name : ");
scanf("%s", r.name);
printf("\n\t\t\tEnter the restaurant name (ABC, XYZ, PQR, EFG) : ");
scanf("%s", r.restaurantName);
printf("\n\t\t\tEnter the number of seats : ");
scanf("%d", &r.seats);
**printf("\n\t\t\tEnter the seat number(s) : ");
scanf("%d", &r.seatNo);**
I am new to C language and I need help.
The Program will ask the user the account number and Account Pin. Then will search the File "Account.txt". If the Account Number and Pin are matched to what was written on the file then will display another Option, such as Balance inquiry,Deposit, withdrawal and Quit.
How can it be possible to search the file for a match? What exactly will I do?
Here's some code:
void OpenAnAccount() {
FILE * fptr;
fptr = fopen("Account.txt", "w");
if (fptr == NULL) {
printf("File does not exists \n");
}
printf("*************************************\n\n");
printf("\n ACCOUNT CREATION ");
printf("\n**********************************\n");
printf("Enter Your Account Number :\n");
scanf("%d", & acctno);
printf("Enter your Name:\n");
scanf("%s", & acctname);
printf("Enter your Account Pin: \n");
scanf("%d", & acctpin);
printf("Please Enter Your Initial Deposit :");
scanf("%f", & dep);
fprintf(fptr, "%d%s%d%f\n", acctno, acctname,
acctpin);
}
void BankTransactions() {
int AN, AP;
printf("Enter 5 digits Account Number :\n");
scanf("%d", & AN);
printf("Enter 4 digits Accoount Pin :\n");
scanf("%d", & AP);
}
Store the account number and pin separated by comma(,) and single entry in each line.
acount.txt
4654654688481,1234
char *account,*pin;
FILE *myfile = fopen ( filename, "r" );
if(myfile)
{
char entry [100]; //maximum line size
while(fgets(entry,sizeof(entry),myfile)!= NULL)
{
account=strtok(entry,",");
pin = strtok(NULL,",");
//match these with user input if matches break out of loop
}
fclose (myfile);
}
you can do something like this. you will able to retrieve the "account" and "pin"
struct customer information[6];
int count,loop;
printf("How many records do you want to add?\n");
scanf("%d",&loop);
FILE *ptr;
ptr = fopen("information.txt","w+");
if(!ptr)
{
printf("file could not be opened\n");
getchar();
return -1;
}
for(count=1; count<=10; count++)
{
printf("Please enter the customer's id number:\n");
scanf("%d",&information[6].idnum);
printf("Please enter the customer's first name and last name:\n");
scanf("%s%s",information[6].Fname,information[6].Lname);
printf("Please enter the customer's car model type:\n");
scanf("%s",information[6].cartyp);
printf("Please enter the customer's license plate number:\n");
scanf("%s",information[6].Licnum);
printf("Please enter the customer's car difficulty:\n");
scanf("%s",information[6].Crdffcty);
fprintf(ptr,"%d\%s\%s\%s\%s\%s\n",information[6].idnum,information[6].Fname,
information[6].Lname,information[6].cartyp,information[6].Licnum,
information[6].Crdffcty);
if(loop==count)
{
continue;
}
}
fclose(ptr);
}
i am trying to write to a file using for loop but when i run the code the program doesn't loop more than one times. An error message comes up saying the program stopped working and nothing is in the text document created. i tried some of the suggestions on this site but it seems there is something else wrong with the coding. there are no errors or warning messages. Can someone tell me what i did wrong?
You have your struct defined with 6, and you are trying to access index 6, that is the 7th element. So, you have to make this, supposing that you want the last element:
struct customer information[6];
int count,loop;
printf("How many records do you want to add?\n");
scanf("%d",&loop);
FILE *ptr;
ptr = fopen("information.txt","w+");
if(!ptr)
{
printf("file could not be opened\n");
getchar();
return -1;
}
for(count=1; count<=10; count++)
{
printf("Please enter the customer's id number:\n");
scanf("%d",&information[5].idnum);
printf("Please enter the customer's first name and last name:\n");
scanf("%s%s",information[5].Fname,information[5].Lname);
printf("Please enter the customer's car model type:\n");
scanf("%s",information[5].cartyp);
printf("Please enter the customer's license plate number:\n");
scanf("%s",information[5].Licnum);
printf("Please enter the customer's car difficulty:\n");
scanf("%s",information[5].Crdffcty);
fprintf(ptr,"%d\%s\%s\%s\%s\%s\n",information[5].idnum,information[5].Fname,
information[5].Lname,information[6].cartyp,information[5].Licnum,
information[5].Crdffcty);
if(loop==count)
{
continue;
}
}
fclose(ptr);
}
The program runs fine till I create the file and enter records from structure, but when I try to search a record by using roll no, it crashes.
I am using a structure called student to store data and then I am creating a text file and then I use a for loop to write data on the text file. Then I reopen the file and try to search a record using student roll no.
Program runs fine until I try to search the student roll no. It crashes right after I enter the student roll no to be searched.
Can anyone tell me what modification is needed to make the search work?
Below is my code:
#include<stdio.h>
struct student {
int roll_no;
char name [80];
int age;
}st[30],s;
int main ()
{
int i,n;
char fname[80];
int search;
int found;
FILE *fp;
printf("\nEnter the file name : \n");
scanf("%s",fname);
fp=fopen(fname,"w");
if(fp==NULL)
{
printf("\nCannot create file :");
exit(0);
}
printf("\nNumber of students : \n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n\nInformation for student#%d : \n\n",i+1);
printf("\nStudent roll number : \n" );
scanf("%d",&st[i].roll_no);
printf("\nStudent name: \n: ");
scanf("%s",st[i].name);
printf("\nStudent age : ");
scanf("%d", &st[i].age);
}
fprintf(fp, "\nStudent roll no\t\t Student name\t\t student age\t\t\n\n");
for(i=0;i<n;i++)
{
fprintf(fp, "\n%d \t\t %s \t\t %d \t\t", st[i].roll_no,st[i].name,st[i].age);
}
fclose(fp);
fp=fopen(fname,"r+t");
if(fp==NULL)
{
printf("\nCannot open file\n");
exit(0);
}
printf("\n\nStudent roll no to be searched : ");
found=0;
scanf("%d", search);
while(!(feof(fp)) && (found==0))
{
fscanf(fp,"%d,%s,%d",&s.roll_no,s.name,&s.age);
if(s.roll_no==search)
{
fseek(fp,-sizeof(struct student), SEEK_CUR);
printf("\nEnter new name : \n");
scanf("%s", s.name);
fprintf(fp, "\n%d \t\t %s \t\t %d \t\t", s.roll_no,s.name,s.age);
found=1;
}
}
if(found=0)
{
printf("\nStudent record doesn't exist \n");
}
fclose(fp);
return 0;
}
In your code, you're missing an address-of operator in scanf(), thereby passing an invalid type of argument. Basically
scanf("%d", search);
should be
scanf("%d", &search);
That said, it is always a good practice to size-limit the inputs for string, like, for an array defined like char fname[80];, you should use
scanf("%79s",fname);
to avoid possible buffer overflow by excessively long input.
Also, always check for the return value of scanf() and family of functions to ensure their success.
Computerizing health records could make it easier for the patients to share their health profiles and histories among their various health care professionals. A health clinic needs your help to computerize the patients' health records. The patient's records consist of first name, middle name, last name (including SR. JR., etc), gender, date of birth, height (in inches), weight (in pounds). The clinic requires the following features of the program:
read existing record from a file where each patient record is one line entry separating each data with comma
add additional records to file
a function to calculate and return patients age in 3yrs
a function that calculates body mass index with the given formula BMI=(weight-in-pounds X 703)/(height-in-inches X 2) or BMI = (weight-in-kgs)/(height-in-meters X 2)
search patient's name and display patient's information with age and BMI value including category
update patient's information on date of birth, height and/or weight and save updates to file
display all records in tabular format
So far what I have made is:
#include<stdio.h>
#include<stdlib.h>
main(){
FILE*fin;
char name,fname,mname,lname,ename,gender,ch,getch,patient;
int dob,month,day,year,height,weight;
fin=fopen("oldrec.c","w");{
printf("Error: File does not exists");
return 0;
}
{
printf("Add Record? y/n");
ch=toupper(getch);
if(ch='y')
break;
}while (1);
struct patient{
char name;
char fname[20];
char mname[20];
char lname[20];
char gender;
int dob;
int month;
int day;
int year;
int height;
int weight;
printf("/n Patient's Name");
printf("First Name: ");
scanf("%s", &patient.fname);
printf("Middle Name: ");
scanf("%s", &patient.mname);
printf("Last Name: ");
scanf("%s", &patient.lname);
printf("Gender: ");
scanf("%s", &patient.gender);
printf("Date of Birth");
printf("Month: ");
scanf("&d", &patient.month);
printf("Day: ");
scanf("&d", &patient.day);
printf("Year: ");
scanf("%s", %patient.year);
printf("Height: ");
scanf("%d", & patient.height);
printf("Weight: ");
scanf("%d", &patient.weight);
}
I have made another file already, but when I run the codes, it says "Error: File does not exist". What is wrong, and what are the codes for the other problems? Please help me! This is our final requirement on my data structure subject.
fin=fopen("oldrec.c","w");{ // no if
printf("Error: File does not exists"); // all statements will be executed
return 0; // and function will terminate here
}
Ofcourse it will show that message , no condition . No matter if fopen is successful without if all statements will be executed.
Put it in a if block witn a condition .
Write like this -
fin=fopen("oldrec.c","w");
if(fin==NULL){ // check if fin is NULL
printf("Error: File does not exists");
return 0;
}
Other problems are these statements -
scanf("%s", &patient.fname);
...
scanf("%s", &patient.mname);
...
scanf("%s", &patient.lname);
...
scanf("%s", &patient.gender); // use %c for reading char variable
...
scanf("%s", %patient.year); // use %d to read int
^ whats this
Write these statemetns like this -
scanf("%s", patient.fname);
...
scanf("%s", patient.mname);
...
scanf("%s", patient.lname);
...
scanf("%c", &patient.gender);
...
scanf("%d", &patient.year);