473,395 Members | 1,941 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

c program with binary file

6
create a binary file and insert the records and query on those inserted record..
queries are: query based on search by name,search by age and search by designation.


the problem is i have created a binary file but it is not storing records permanently. i am using binary file for the first time. plz help me to know how to store records permanently . sending my code

#include<stdio.h>
#include<conio.h>
#include<string.h>
struct record
{
char name[50];
int age;
char designation[50];
}record ;

void main()
{
char name1[50], ch;
// char name[50];
char designation[50];
int age1,a,age2;
struct record r[1];
FILE *f;
int i,n,j;
clrscr();
f=fopen("c:\\input.dat","wb");
if(!f)
{
printf("unable to open binary file for reading \n");
return;
}
// fread(&r,sizeof(r),1,f);
fwrite(&r[a],sizeof(r[a]),1,f);
printf("Enter the option:\n 1.data entry:\n 2.query:\n");
scanf("%d",&i);

switch(i)
{
case 1:
{
printf("Enter number of records\n");
scanf("%d",&n);
// printf("enter the name\n");
// fgets(name,50,stdin);
for(a=1;a <=n; a++)
{

printf("Enter name\n");
scanf("%s",r[a].name);
// fgetc(r[a].name);
//gets(r[a].name,50,f);
printf("Enter age\n");
scanf("%d",&r[a].age);
//fgets(r[a].age,1,stdin)
printf("Enter designation\n");
scanf("%s",r[a].designation);
// gets(r[a].designation,50,f);
// fprintf(f,"%s %d %s\n", r[a].name,r[a].age,r[a].designation);
}
// fwrite(&r[a],sizeof(r[a]),1,f);
// break;
}
case 2:
{
printf("Enter the option:\n a.search by name:\n b.search by age:\n c.search by designation:\n");
scanf("%s",&ch);
switch(ch)
{
case 'a':
{
printf("\n Enter name\n");
scanf("%s",name1);
printf("Name\t\t Age\t\t Designation\t\t\n");
printf("--------------------------------------------\n");
for(i=0;i<=n;i++)
{
// if(strcmp(name1,r[i].name)==0)
// {
// for(j=0;j<=n;j++)
// {

if(strncmp(r[i].name,name1,strlen(name1))==0)
{

printf("%s\t\t %d\t\t %s\n", r[i].name,r[i].age,r[i].designation);
}
}

printf("--------------------------------------------\n");

break;
}
case 'b':
{
printf("\n Enter age1\n");
scanf("%d",&age1);
printf("Enter age2\n");
scanf(" %d",&age2);
printf("Name\t\t Age\t\t Designation\t\t\n");
printf("--------------------------------------------\n");

for(a=0;a<=n;a++)
{
if(r[a].age>=age1 && r[a].age<=age2)
{
printf("%s\t\t%d\t\t%s\n",r[a].name,r[a].age,r[a].designation);
}
// else
// printf("The name is not in the list of records\n");
}
printf("--------------------------------------------\n");
break;
}
case 'c':
{
printf("\n Enter designation\n");
scanf("%s",designation);

printf("Name\t\t Age\t\t Designation\t\t\n");
printf("--------------------------------------------\n");
for(i=0;i<=n;i++)
{
if(strncmp(r[i].designation,designation,strlen(designation))==0)
{
printf("%s\t\t %d\t\t%s\n",r[i].name,r[i].age,r[i].designation);
}
}
printf("--------------------------------------------\n");
break;
}
}
// default: printf("invalid choice\n");
// break;
}
}
getch();
}
Feb 15 '08 #1
2 2664
weaknessforcats
9,208 Expert Mod 8TB
You can't read from a file rhat is write-only. I see the "wb" but I don't see where you re-open the file as readable.
Feb 15 '08 #2
You need to do a fclose so that the file's saved and a new fopen for rb or wb+.
Feb 15 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Mahmood Ahmad | last post by:
Hello, I have written a program that reads three types of records, validates them acording to certain requirements and writes the valid records into a binary file. The invalid records are...
10
by: Son of Sam | last post by:
Ok what does the program do: opens a file binary, jumps to an specific offset (0x3529BC9) reads out the string which is 7 bytes (7 chars) long (which starts at the offset), then generates a random...
4
by: Blankdraw | last post by:
I have a program below that runs fine. It accesses a datafile for random-access transfers. Supposedly, it does this by accessing the file in binary mode. I do not understand how it does this...
1
by: khaleel.alyasini | last post by:
anyone could point me out where did i do wrong? it seems that i can't get back the original Lena image after the IDCT(inverse discrete cosine transform) process. the output raw image is nothing...
66
by: genestarwing | last post by:
QUESTION: Write a program that opens and read a text file and records how many times each word occurs in the file. Use a binary search tree modified to store both a word and the number of times it...
14
by: prasadjoshi124 | last post by:
Hi All, I am writing a small tool which is supposed to fill the filesystem to a specified percent. For, that I need to read how much the file system is full in percent, like the output given...
3
dfound
by: dfound | last post by:
#include<windows.h> #include<dos.h> #include<dir.h> #include<fstream.h> #include<stdio.h> #include<process.h> #include"resource.h" //resource header file
4
memoman
by: memoman | last post by:
Can any body help me in that program ??? mail me if anybody could reach any -helpfull- thing Write a C++ program that namely insert, delete, and search in a fixed record length file (containing...
0
by: ragnarok8990 | last post by:
//BLOOD BANK MANAGEMENT SYSTEM// #include<iostream.h> #include<conio.h> #include<fstream.h> #include<string.h> #include<stdio.h> #include<process.h> fstream file; fstream file1;
13
by: Logan Lee | last post by:
Hi. I've written a small program to learn to write in C. But unfortunately the output is all jumbled up and not nice. /* read_file.c The whole point of this code is to read the entire content...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.