Identify and list all the User defined Functions to be used in the system
#include <stdio.h> ///for input output functions
like printf, scanf
#include <stdlib.h>
#include <conio.h>
#include <windows.h> ///for windows related functions (not
important)
#include <string.h> ///string operations
/** List of Global Variable */
COORD coord = {0,0}; /// top-left corner of window
/**
function : gotoxy
@param input: x and y coordinates
@param output: moves the cursor in specified position of
console
*/
void gotoxy(int x,int y)
{
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),coord);
}
/** Main function started */
int main()
{
FILE *fp, *ft; /// file pointers
char another, choice;
/** structure that represent a employee */
struct emp
{
char name[40]; ///name of employee
int age; /// age of employee
float bs; /// basic salary of employee
};
struct emp e; /// structure variable creation
char empname[40]; /// string to store name of the employee
long int recsize; /// size of each record of employee
/** open the file in binary read and write mode
* if the file EMP.DAT already exists then it open that file in read
write mode
* if the file doesn't exit it simply create a new copy
*/
fp = fopen("EMP.DAT","rb+");
if(fp == NULL)
{
fp = fopen("EMP.DAT","wb+");
if(fp == NULL)
{
printf("Connot open file");
exit(1);
}
}
/// sizeo of each record i.e. size of structure variable
e
recsize = sizeof(e);
/// infinite loop continues untile the break statement
encounter
while(1)
{
system("cls"); ///clear the console window
gotoxy(30,10); /// move the cursor to postion 30, 10 from top-left
corner
printf("1. Add Record"); /// option for add record
gotoxy(30,12);
printf("2. List Records"); /// option for showing existing
record
gotoxy(30,14);
printf("3. Modify Records"); /// option for editing record
gotoxy(30,16);
printf("4. Delete Records"); /// option for deleting record
gotoxy(30,18);
printf("5. Exit"); /// exit from the program
gotoxy(30,20);
printf("Your Choice: "); /// enter the choice 1, 2, 3, 4, 5
fflush(stdin); /// flush the input buffer
choice = getche(); /// get the input from keyboard
switch(choice)
{
case '1': /// if user press 1
system("cls");
fseek(fp,0,SEEK_END); /// search the file and move cursor to end of
the file
/// here 0 indicates moving 0 distance from the end of the
file
another = 'y';
while(another == 'y') /// if user want to add another record
{
printf("\nEnter name: ");
scanf("%s",e.name);
printf("\nEnter age: ");
scanf("%d", &e.age);
printf("\nEnter basic salary: ");
scanf("%f", &e.bs);
fwrite(&e,recsize,1,fp); /// write the record in the file
printf("\nAdd another record(y/n) ");
fflush(stdin);
another = getche();
}
break;
case '2':
system("cls");
rewind(fp); ///this moves file cursor to start of the file
while(fread(&e,recsize,1,fp)==1) /// read the file and fetch
the record one record per fetch
{
printf("\n%s %d %.2f",e.name,e.age,e.bs); /// print the name, age
and basic salary
}
getch();
break;
case '3': /// if user press 3 then do editing existing
record
system("cls");
another = 'y';
while(another == 'y')
{
printf("Enter the employee name to modify: ");
scanf("%s", empname);
rewind(fp);
while(fread(&e,recsize,1,fp)==1) /// fetch all record from
file
{
if(strcmp(e.name,empname) == 0) ///if entered name matches with
that in file
{
printf("\nEnter new name,age and bs: ");
scanf("%s%d%f",e.name,&e.age,&e.bs);
fseek(fp,-recsize,SEEK_CUR); /// move the cursor 1 step back from
current position
fwrite(&e,recsize,1,fp); /// override the record
break;
}
}
printf("\nModify another record(y/n)");
fflush(stdin);
another = getche();
}
break;
case '4':
system("cls");
another = 'y';
while(another == 'y')
{
printf("\nEnter name of employee to delete: ");
scanf("%s",empname);
ft = fopen("Temp.dat","wb"); /// create a intermediate file for
temporary storage
rewind(fp); /// move record to starting of file
while(fread(&e,recsize,1,fp) == 1) /// read all records from
file
{
if(strcmp(e.name,empname) != 0) /// if the entered record
match
{
fwrite(&e,recsize,1,ft); /// move all records except the one
that is to be deleted to temp file
}
}
fclose(fp);
fclose(ft);
remove("EMP.DAT"); /// remove the orginal file
rename("Temp.dat","EMP.DAT"); /// rename the temp file to original
file name
fp = fopen("EMP.DAT", "rb+");
printf("Delete another record(y/n)");
fflush(stdin);
another = getche();
}
break;
case '5':
fclose(fp); /// close the file
exit(0); /// exit from the program
}
}
return 0;
}
note:- I have explained each and everything along with code, not in comment mode so don't directly run it on the compiler. run your original code on the compiler.
in this program, we are just making a simple record storing the
program, which will store all records.
let see one by one:-
we can divide our program in four-part
1. Write record for writing a new record
2. Display record for displaying all record
3. Modify Record for modifying the old stored record
4. Delete record for delete our stored record
for implement our whole program we need some header file
below ara some headers
#include <stdio.h> //for standered input output functions
like printf, scanf and file handling
#include <stdlib.h> // standered Libraryies and storege we
use it like bat extension and file handler
#include <conio.h>
#include <windows.h> ///for windows related functions like
space skipping using goto function we will see furthure
#include <string.h> ///string operations
it' s Global Variable use to indicate or start our cursure from
located cell
COORD coord = {0,0}; // top-left corner of window indecting
below function just move our cursor into give or silicing cell with the help of gotoxy Imagin loke 2d place and we are moving like a pencile at the paper.
void gotoxy(int x,int y)
{
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),coord);
}
/** Main function started */
int main()
{
FILE *fp, *ft; /// file pointers and file indicate as file handler
and ft and fp is file pointer as object
char another, choice;
/** structure that represent a employee */
struct emp
{
char name[40]; ///name of employee
int age; /// age of employee
float bs; /// basic salary of employee
};
struct emp e; /// structure variable creation
char empname[40]; /// string to store name of the employee
long int recsize; /// size of each record of employee
* if the file EMP.DAT already exists then it open that file in read
write mode
fp = fopen("EMP.DAT","rb+"); // open the file in binary read and
write mode
if(fp == NULL)
{
fp = fopen("EMP.DAT","wb+"); // if the file doesn't exit it simply
create a new copy wb menas wtitten binary mode
if(fp == NULL)
{
printf("Connot open file"); //if file is not created or not
exist.
exit(1); simple close program ans return to main 1.
}
}
recsize = sizeof(e); /// sizeo of each record i.e. size of
structure variable e because e is object that store all
stodrage
/// infinite loop continues untile the break statement
encounter
while(1)
{
system("cls"); ///clear the console window
gotoxy(30,10); /// move the cursor to postion 30, 10 from top-left
corner
printf("1. Add Record"); /// option for add record
gotoxy(30,12);
printf("2. List Records"); /// option for showing existing
record
gotoxy(30,14);
printf("3. Modify Records"); /// option for editing record
gotoxy(30,16);
printf("4. Delete Records"); /// option for deleting record
gotoxy(30,18);
printf("5. Exit"); /// exit from the program
gotoxy(30,20);
printf("Your Choice: "); /// enter the choice 1, 2, 3, 4, 5
fflush(stdin); /// flush the input buffer
here we decleare on variable for choice making it will run untill we will not give 0(boolean true=0 and false means 0) to while
get the input from keyboard
choice = getche();
switch(choice)
{
case '1':
if user press 1 then this program will execute
system("cls");
fseek(fp,0,SEEK_END);
in fseek file we have fp as object of file handler
class(file)
here 0 indicates moving 0 distance from the end of the file
another = 'y'; by defult value is yes Or 1
while(another == 'y') /// if user want to add another
record
{
printf("\nEnter name: ");
scanf("%s",e.name);
printf("\nEnter age: ");
scanf("%d", &e.age);
printf("\nEnter basic salary: ");
scanf("%f", &e.bs);
in above printf is just printing our commend which is written
inside printf and scanf is taking input in varibable .And below
using fwrite function we are writting our data to our EMP.bat file
using addresser(onject)) e
and we can access that variable using dot operrator like
e.name;
fwrite(&e,recsize,1,fp); /// write the record in the file
when we want to store our data at single storke without pressing enter button then us just use fflush function it's defind insde window header and fush register store caches.
printf("\nAdd another record(y/n) ");
fflush(stdin);
another = getche();
}
break;
in this case 2 decision we are just simple displaying our store
data in Emp.bat file
case '2':
system("cls");
rewind(fp); ///this moves file cursor to start of the file
while(fread(&e,recsize,1,fp)==1) /// read the file and fetch
the record one record per fetch
{
printf("\n%s %d %.2f",e.name,e.age,e.bs); /// print the name, age
and basic salary
}
getch();
break;
in case 3 we gonna modify our data in case any error occurs.
case '3': /// if user press 3 then do editing existing record
system("cls");
another = 'y'; // it;s as it is previous command like decisiion
making as eariler i explain.
while(another == 'y')
{
printf("Enter the employee name to modify: "); // just enter name
which one record you want ot modify.
scanf("%s", empname);
rewind(fp);
while(fread(&e,recsize,1,fp)==1) /// fetch all record from
file
{
if(strcmp(e.name,empname) == 0) ///if entered name matches with
that in file
{
printf("\nEnter new name,age and bs: "); // overwritting at that
location
scanf("%s%d%f",e.name,&e.age,&e.bs);
fseek(fp,-recsize,SEEK_CUR); /// move the cursor 1 step back from
current position
fwrite(&e,recsize,1,fp); /// override the record
break;
}
}
printf("\nModify another record(y/n)");
fflush(stdin);
another = getche();
}
break;
this case is permanatly delete our record from our data base.
case '4':
system("cls");
another = 'y';
while(another == 'y')
{
printf("\nEnter name of employee to delete: "); //authentication
for simply deleteing the record you have to specify which recird do
wnat delete that is locate by name so enter name
scanf("%s",empname);
ft = fopen("Temp.dat","wb"); /// create a intermediate file for
temporary storage
rewind(fp); /// move record to starting of file
while(fread(&e,recsize,1,fp) == 1) /// read all records from
file
{
if(strcmp(e.name,empname) != 0) /// if the entered record
match
{
fwrite(&e,recsize,1,ft); /// move all records except the one
that is to be deleted to temp file
}
}
fclose(fp);
fclose(ft);
remove("EMP.DAT"); /// remove the orginal file
rename("Temp.dat","EMP.DAT"); /// rename the temp file to original
file name
fp = fopen("EMP.DAT", "rb+");
printf("Delete another record(y/n)");
fflush(stdin);
another = getche();
}
break;
case 5 just declreade for exit from that fucntion ....
case '5':
fclose(fp); /// close the file
exit(0); /// exit from the program
}
}
return 0;
}
// thanks for chosing HomeworkLib happy coding :)
Identify and list all the User defined Functions to be used in the system #include <stdio.h>...
Create another program that will prompt a user for input file. The user will choose from 4 choices: 1. Display all positive balance accounts. 2. Display all negative balance accounts. 3. Display all zero balance accounts. 4. End program. C PROGRAMMING my code so far #include<stdlib.h> #include<stdio.h> int main(void) { int request; int account; FILE *rptr; char name[20]; double balance; FILE *wptr; int accountNumber; if((wptr = fopen("clients.dat","w")) == NULL) { puts("File could not be opened"); } else {...
Hardware Inventory – Write a database to keep track of tools, their cost and number. Your program should initialize hardware.dat to 100 empty records, let the user input a record number, tool name, cost and number of that tool. Your program should let you delete and edit records in the database. The next run of the program must start with the data from the last session. This is my program so far, when I run the program I keep getting...
create case 4 do Method 1:copy item by item and skip the item you want to delete after you are done, delete the old file and rename the new one to the old name. #include int main(void) { int counter; int choice; FILE *fp; char item[100]; while(1) { printf("Welcome to my shopping list\n\n"); printf("Main Menu:\n"); printf("1. Add to list\n"); printf("2. Print List\n"); printf("3. Delete List\n"); printf("4. Remove an item from the List\n"); printf("5. Exit\n\n"); scanf("%i", &choice); switch(choice) { case 1:...
Write a C language program that will prompt for 12 different resistor values, entered one at a time via the keyboard, expressed in Ohms. Valid values are 0.01 Ohm up to 10,000,000,000 Ohms. Write each entry to a text file on the disk thus: Resistor 1 Ohmic value = 2200. Update the resistor number for each of the 12 entries. Do not repeat any resistance value. Write a second C language program to read the 12 entries from the text...
Below is a basic implementation of the Linux command "cat". This command is used to print the contents of a file on the console/terminal window. #include <stdio.h> #include <stdlib.h> int main(int argc, char* argv[]) {FILE *fp; if(2 != argc) {priritf ("Usage: cat <filename>\n"); exit(1);} if ((fp = fopen(argv[1], "r")) == NULL) {fprintf (stderr, "Can't. open input file %s\n", argv[1]); exit (1);} char buffer[256]; while (fgets(X, 256, fp) != NULL) fprintf(Y, "%s", buffer); fclose(Z); return 0;} Which one of the following...
Convert this code from C to C++ include <stdio.h> include <locale.h> void filtre (double x. double yll, double yol, int , int N) int Nn-10000 int main) setlocale (LC ALL,") double x(Nm) , y [ Nm],yo Nml®(0.0); int m; FILE dosyal-fopen("D:/veri/673.txtr FILE dosya2-fopen("D:/veri/data2.txt int i-0 while( feof (dosyal)) fscanf (dosyal, " ",xi sytil) fclose (dosyal) int Nij printf("Pencere Genişligi scanf() 3 filtre(x,y.Yo,m,N) for(int k-0keNkt+)fprintf (dosya2, 10.41 10.411 0.41fn",x[k],y[k].yo[k]) fclose(dosya2) void filtre (double x double y. double yoll, int m, int...
How would I change the following C code to implement the following functions: CODE: #include <stdio.h> #include <stdlib.h> int main(int argc, char * argv[]) { if (argc != 2) printf("Invalid input!\n"); else { FILE * f = fopen (argv[1], "r"); if (f != NULL) { printf("File opened successfully.\n"); char line[256]; while (fgets(line, sizeof(line), f)) { printf("%s", line); } fclose(f); } else { printf("File cannot be opened!"); } } return 0; } QUESTION: Add a function that uses fscanf like this:...
convert this code from C to C++ include <stdio.h> include <locale.h> void filtre (double x. double yll, double yol, int , int N) int Nn-10000 int main) setlocale (LC ALL,") double x(Nm) , y [ Nm],yo Nml®(0.0); int m; FILE dosyal-fopen("D:/veri/673.txtr FILE dosya2-fopen("D:/veri/data2.txt int i-0 while( feof (dosyal)) fscanf (dosyal, " ",xi sytil) fclose (dosyal) int Nij printf("Pencere Genişligi scanf() 3 filtre(x,y.Yo,m,N) for(int k-0keNkt+)fprintf (dosya2, 10.41 10.411 0.41fn",x[k],y[k].yo[k]) fclose(dosya2) void filtre (double x double y. double yoll, int m, int...
C Language Programming. Using the program below - When executing on the command line only this program name, the program will accept keyboard input and display such until the user does control+break to exit the program. The new code should within only this case situation “if (argc == 1){ /* no args; copy standard input */” Replace line #20 “filecopy(stdin, stdout);” with new code. Open read a text file “7NoInputFileResponse.txt” that contains a message “There were no arguments on the...
URGENT. Need help editing some C code. I have done most of the code it just needs the following added: takes an input file name from the command line; opens that file if possible; declares a C struct with three variables; these will have data types that correspond to the data types read from the file (i.e. string, int, float); declares an array of C structs (i.e. the same struct type declared in point c); reads a record from the...