H
happy
/* Book name : The prodessional programmers guide to C
File name : E:\programs\tc\iti01\ch09\main\01setupm.c
Program discription: file setuping -up -Version 01-ver01-W
Logic :
1-open file for output
2-while more customers
2-1-input customers record
2-2-write customer record to file
3-write end of file record to file
4-close file
Book :
-File Organization :Reading , storing information from/to files
into forms of records in an organized manner not haphzaradly
manner.
-File made up from a number of records.
-Each record must be unique.
-Any program using file must know where each record starts and
finishes,
One mean of doing this to write an end of record marker to the file
as it is being setup. In C programming the character <return>
can be used to denote the end of a record.
-Each record is broken into data items(fields)
-Each data item starting and finsihing must be very well known
using separators.
-Separators can be into two different ways:
-Specified length
*Each piece of data is written to the record its length is
specified.
If the reading from a file of a data item with a lenght
less than
the required lenght of the data item that will lead to a
padded
out with spaces.
*If the reading from a file of a data item with a lenght
more than
the required lenght of the data item that will lead to
reading first characters
with the same length.
*Specified lenght may lead to wasted lenght in some
records.
The lenght of the data are the same in all records
in the file.
-Item Separators
*The items are separated by some known character e.g
comma,space.
A space would be unsuitable as the data itself contains
spaces .
Item separators are written to the record between each data
item.
*When the record is read it is done character by character
looking for the item separator to indicate when a compelete
item has been read.
*This method is slower than using the specified length
method of
item seeparators.
-File Organizations.
-The ways of combining records into file :
*Serial
--------
Records are stored ---------------------in any order .
Records are accessed (processed) -------in any order .
*Sequential
------------
Records are stored -------------------in a pre-determined
order.
Records are accessed (processed) -----in any order order.
e.g : payroll,inventory files.
*Indexed Sequntial
------------------
Records are stored -------------------in any order order.
Records are accessed (processed) -----in a pre-determined
order.
hint : The advantage of this method of file organization
is that if the records are long(i.e contain a lot
of information )then the actual records do not have
to be moved to be sorted.
Adding records will be to the end of the file,
Only the index needs to be kept in order.
*Random
--------
Records are stored ---------------------in any order .
Records are accessed (processed) -------in any order .
8.4 Sequential File Organization
*-------------------------------
Records are ordered on wither alphabetic or numeric key.
The key has been used in setting up is that the one used
with the further processing.
Setting up A Sequential File
*----------------------------
To order records before putting them into the file.
Ordering is done into two ways :
1-All records are entered in the file regardless of
their
order.
2-Deciding which key field and whetherthe orders are to
be
held in ascending or descending order of that key,a
sort
program is run on the producing a sorted file.
*/
#include <stdio.h>
#include <conio.h>
/*TV Rental file Set_up -Version 01
-Setting up a file containing sutomer information*/
struct customer_record /*Defining a csutomer record*/
{
int customer_no;
int no_of_weeks;
char tv_type;
};
main ()
{
/*-------1-open file for output*/
struct customer_record customer;
FILE *fp_setup;
if ((fp_setup=fopen("setup.txt","w")) == NULL)
{
printf("\nCan not open file 'setup.txt' for writing \n");
printf("Program is termainted");
exit();
}
clrscr();
/*-------2-while more customers----------separate function-------*/
do {
/*-------2-1-input customers record------separate function-------*/
customer_input(&customer);
/*-------2-2-write customer record to file*/
fprintf(fp_setup,"%4d%2d%c\n",customer);
} while (another_customer()=='y');
/*-------3-write end of file record to file */
fprintf(fp_setup,"%4d%2d%c\n",9999,99,' ');
/*-------4-close file*/
fclose(fp_setup);
return 0;
}
customer_input(cust)
/*---------------------*/
struct customer_record *cust;
{
printf("\nEnter customer number :");
scanf("%4d",&(*cust).customer_no);
printf("\nEnter number of weeks rent due :");
scanf("%2d",&(*cust).no_of_weeks);
printf("Enter type of rental -c for colors TV");
printf("\n -b for black and white TV");
printf("\n -v for video");
printf("\n -o for other : ");
scanf("\n");
scanf("%c",&(*cust).tv_type);
}
another_customer()
/*----------------*/
{
char another;
printf("\nAnother new_screen_customer for input (y or n) : ");
scanf("\n");
scanf("%c",&another);
return (another);
}
File name : E:\programs\tc\iti01\ch09\main\01setupm.c
Program discription: file setuping -up -Version 01-ver01-W
Logic :
1-open file for output
2-while more customers
2-1-input customers record
2-2-write customer record to file
3-write end of file record to file
4-close file
Book :
-File Organization :Reading , storing information from/to files
into forms of records in an organized manner not haphzaradly
manner.
-File made up from a number of records.
-Each record must be unique.
-Any program using file must know where each record starts and
finishes,
One mean of doing this to write an end of record marker to the file
as it is being setup. In C programming the character <return>
can be used to denote the end of a record.
-Each record is broken into data items(fields)
-Each data item starting and finsihing must be very well known
using separators.
-Separators can be into two different ways:
-Specified length
*Each piece of data is written to the record its length is
specified.
If the reading from a file of a data item with a lenght
less than
the required lenght of the data item that will lead to a
padded
out with spaces.
*If the reading from a file of a data item with a lenght
more than
the required lenght of the data item that will lead to
reading first characters
with the same length.
*Specified lenght may lead to wasted lenght in some
records.
The lenght of the data are the same in all records
in the file.
-Item Separators
*The items are separated by some known character e.g
comma,space.
A space would be unsuitable as the data itself contains
spaces .
Item separators are written to the record between each data
item.
*When the record is read it is done character by character
looking for the item separator to indicate when a compelete
item has been read.
*This method is slower than using the specified length
method of
item seeparators.
-File Organizations.
-The ways of combining records into file :
*Serial
--------
Records are stored ---------------------in any order .
Records are accessed (processed) -------in any order .
*Sequential
------------
Records are stored -------------------in a pre-determined
order.
Records are accessed (processed) -----in any order order.
e.g : payroll,inventory files.
*Indexed Sequntial
------------------
Records are stored -------------------in any order order.
Records are accessed (processed) -----in a pre-determined
order.
hint : The advantage of this method of file organization
is that if the records are long(i.e contain a lot
of information )then the actual records do not have
to be moved to be sorted.
Adding records will be to the end of the file,
Only the index needs to be kept in order.
*Random
--------
Records are stored ---------------------in any order .
Records are accessed (processed) -------in any order .
8.4 Sequential File Organization
*-------------------------------
Records are ordered on wither alphabetic or numeric key.
The key has been used in setting up is that the one used
with the further processing.
Setting up A Sequential File
*----------------------------
To order records before putting them into the file.
Ordering is done into two ways :
1-All records are entered in the file regardless of
their
order.
2-Deciding which key field and whetherthe orders are to
be
held in ascending or descending order of that key,a
sort
program is run on the producing a sorted file.
*/
#include <stdio.h>
#include <conio.h>
/*TV Rental file Set_up -Version 01
-Setting up a file containing sutomer information*/
struct customer_record /*Defining a csutomer record*/
{
int customer_no;
int no_of_weeks;
char tv_type;
};
main ()
{
/*-------1-open file for output*/
struct customer_record customer;
FILE *fp_setup;
if ((fp_setup=fopen("setup.txt","w")) == NULL)
{
printf("\nCan not open file 'setup.txt' for writing \n");
printf("Program is termainted");
exit();
}
clrscr();
/*-------2-while more customers----------separate function-------*/
do {
/*-------2-1-input customers record------separate function-------*/
customer_input(&customer);
/*-------2-2-write customer record to file*/
fprintf(fp_setup,"%4d%2d%c\n",customer);
} while (another_customer()=='y');
/*-------3-write end of file record to file */
fprintf(fp_setup,"%4d%2d%c\n",9999,99,' ');
/*-------4-close file*/
fclose(fp_setup);
return 0;
}
customer_input(cust)
/*---------------------*/
struct customer_record *cust;
{
printf("\nEnter customer number :");
scanf("%4d",&(*cust).customer_no);
printf("\nEnter number of weeks rent due :");
scanf("%2d",&(*cust).no_of_weeks);
printf("Enter type of rental -c for colors TV");
printf("\n -b for black and white TV");
printf("\n -v for video");
printf("\n -o for other : ");
scanf("\n");
scanf("%c",&(*cust).tv_type);
}
another_customer()
/*----------------*/
{
char another;
printf("\nAnother new_screen_customer for input (y or n) : ");
scanf("\n");
scanf("%c",&another);
return (another);
}