473,378 Members | 1,426 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,378 software developers and data experts.

How can I do sort program for unsorted setup file?

/* 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);
}

Nov 14 '05 #1
3 2956
happy wrote:
/* 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>
<conio.h> is no standard C header.
/*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 () 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 <stdlib.h>. Moreover, exit
needs an int argument (usually one of 0, EXIT_SUCCESS, EXIT_FAILURE). }
clrscr();
from <conio.h>, non-standard.
/*-------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
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Nov 14 '05 #2
On 12 Jan 2005 03:17:57 -0800, "happy" <eh***********@yahoo.com> wrote:
/* 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?

--
Robert B. Clark (email ROT13'ed)
Visit ClarkWehyr Enterprises On-Line at http://www.3clarks.com/ClarkWehyr/
Nov 14 '05 #3
Michael Mair <Mi**********@invalid.invalid> writes:
[...]
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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Brett | last post by:
I have two arrays and i wish to sort the first one numerically, but after sorting, I would like the second array to be in the same matching order as the first array. ie. @l1={3,1,2};...
1
by: Derek Tinney | last post by:
Hi, I'm having difficulty building an XLST file that allows me to sort a list of log records. I put together an XSL file that allows me to output a copy of the input file and then I attempted...
2
by: JasBascom | last post by:
I thought it was too much to put this program in with my last message. The program compiles ok, but when I execute, I get access errors. can someone put it through their compiler please. I think...
19
by: David | last post by:
Hi all, A while back I asked how to sort an array of strings which would have numerals and I wanted to put them in sequential numerical order. For example: myArray = "file1"; myArray =...
7
by: ritchie | last post by:
Hi all, I am new to this group and I have question that you may be able to help me with. I am trying to learn C but am currently stuck on this. First of all, I have a function for each sort...
0
by: happy | last post by:
/* 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 ...
3
by: Peter J Ross | last post by:
Greetings. I'm quite new to PHP and have very little knowledge of programming in general, so I may be missing something obvious, but I can't find a solution to the following difficulty in either...
75
by: At_sea_with_C | last post by:
Hello all, I have written an ascending sort routine for floats. This seems to do the job, but for elements over 10,000, it gets awfully slow. A lot of useless comparisions with previously sorted...
0
by: Randeh | last post by:
Using Visual Studio 2005. Finally got this thing to compile, but now it prints an empty blank row for the first pass, so I guess there's an error somewhere in the function I have to show the Array. ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.