How can I do sort program for unsorted setup file?

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);
}
 
M

Michael Mair

happy said:
/* Book name : The prodessional programmers guide to C

Umh, google told me that "The pro_f_essional programmers guide to C"
is from 1989. Note that meanwhile, we have had a standard for C (C89/90)
which is widely accepted and a new one (C99) for which there is still
no conforming compiler/library combination freely available.

Something else: You have posted so much comment that it is rather
hard to find the code. This may also be a problem of your indentation,
better: the lack thereof.

[snip]
*/

#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;
};

Your comment is entirely useless. Why not tell us what no_of_weeks
_means_? Would help more than telling us nothing.
main has return type int. For clarity, tell us that you really
mean the empty argument list:
int main (void)
{
/*-------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();
exit() is prototyped/declared in said:
}
clrscr();

/*-------2-while more customers----------separate function-------*/
do {
/*-------2-1-input customers record------separate function-------*/
customer_input(&customer);
There was no prototype for customer_input(), so you run into trouble.
/*-------2-2-write customer record to file*/
fprintf(fp_setup,"%4d%2d%c\n",customer);
This will not work. First, you need to pass the values of the
respective members of customer. Second, you need to pad the
numbers with zeros if you do not accept spaces. It is better
to use a seperator (e.g. #define SEPARATOR ',') instead -- this
makes reading the records easier.
} while (another_customer()=='y');
another_customer() once more is unknown.
It makes usually sense to make a function which returns
non-zero on acceptance and zero otherwise:
} while (another_customer());
/*-------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;
This declaration style is out of fashion.
Nowadays, we use

int customer_input (struct customer_record *cust)

In addition, you would provide a prototype.
{
printf("\nEnter customer number :");
scanf("%4d",&(*cust).customer_no);

Instead of (*cust).customer_no, you could also use
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);
}

Suggestion: Have a look at the c.l.c FAQ, there are also books you
may want to look at. Rewrite the above to C89. Facilitate reading in
records from a file. Then come back with your original question and
your best shot at solving it.

The c.l.c FAQ is regularly posted here, a (slightly outdated) HTML
version can also be found here:
http://www.eskimo.com/~scs/C-faq/top.html


Cheers
Michael
 
R

Robert B. Clark

/* 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

<snip>

Y'know, I grepped this post and found not a single question mark.

Did you have a specific question about your homework assignment?
 
K

Keith Thompson

Michael Mair said:
Something else: You have posted so much comment that it is rather
hard to find the code. This may also be a problem of your indentation,
better: the lack thereof.

The lack of indentation is Google's fault. See the "groups.google.com
indentation bugs [semi-OT]" thread.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top