Try this:
Code:
#include <stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
#include <math.h>
//********* prototype******//
void ShowData();
bool isEmpty( char *str );
struct Manufacturer //creating your own data type
{
char code[6],name[16], address[21],contact[11];//63 type long
int position;
bool pass;
};
//*********main********//
int main(void)
{
#define UNDEFINED 12345
int test = UNDEFINED; //...hmmm...unused...
long offsettest = UNDEFINED; // ...unused...
int increment = 0;
struct Manufacturer m;
FILE *f = fopen("a:\\Manufacturer.txt","wb");//no such "ab", see below...
fseek(f, 0, SEEK_END); ///...to append...
printf("%d",test);
while (isEmpty(m.code) == false)
{
memset(m, 0, sizeof(struct Manufacturer) ); //...initialize all to zero...
puts("Enter name:");
gets(m.name);
puts("Enter Address: ");
gets(m.address);
puts("Enter contact");
gets(m.contact);
puts("Enter code");
gets(m.code);
m.position = ++increment;
m.pass=true;
fwrite(&m, sizeof(m),1,f);
}
fclose(f);
ShowData();
}
//*******function************//
void ShowData()
{
struct Manufacturer ma;
int count = 1;
FILE *f;
f=fopen("a:\\Manufacturer.txt","rb");
while (fread(&ma,sizeof(ma),1,f)==1)
{
printf("Show");
printf("Record %d\n",count++);
puts(ma.code);
puts(ma.name);
puts(ma.address);
puts(ma.contact);
printf("%i", ma.position); //..."puts" is for strings only...
printf("inside");
getch();
}
fclose(f);
}
bool isEmpty( char *str ){
int len = strlen(str);
int i = 0;
while(i < len)
{
if( !isspace(str[i]) && str[i] != '\n' )
return false;
i++;
}
return true;
}