473,765 Members | 2,037 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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,invento ry 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=fope n("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_setu p,"%4d%2d%c\n", customer);
} while (another_custom er()=='y');
/*-------3-write end of file record to file */
fprintf(fp_setu p,"%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_wee ks);
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",&(*c ust).tv_type);
}

another_custome r()
/*----------------*/
{
char another;
printf("\nAnoth er new_screen_cust omer for input (y or n) : ");
scanf("\n");
scanf("%c",&ano ther);
return (another);
}

Nov 14 '05 #1
3 2992
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=fope n("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_setu p,"%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_custom er()=='y'); another_custome r() once more is unknown.
It makes usually sense to make a function which returns
non-zero on acceptance and zero otherwise:
} while (another_custom er());
/*-------3-write end of file record to file */
fprintf(fp_setu p,"%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).custome r_no, you could also use
cust->customer_no.
printf("\nEnter number of weeks rent due :");
scanf("%2d",&(* cust).no_of_wee ks);
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",&(*c ust).tv_type);
}

another_custome r()
/*----------------*/
{
char another;
printf("\nAnoth er new_screen_cust omer for input (y or n) : ");
scanf("\n");
scanf("%c",&ano ther);
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**********@i nvalid.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_Keit h) 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
5318
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}; @l2={'a','b','c'}; @l1 = sort {$a <=> $b} (@l1); # sort list @l1 to be {1,2,3}
1
2068
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 to sort it. Eventually I want to filter it based on the "when" element (and/or others) but I cannot proceed until I get the sort to work. I have tried several approaches (specific XPATHs, data-type on the sort) none of which have worked (or have...
2
2118
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 the problem lies when i declare union Allrecords h1,h2,h3. and then use them to toupper record_type. the debug when i use it can not go past the switch statement. thank you for you help
19
2631
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 = "file2"; myArray = "file3"; myArray = "file4"; myArray = "file5";
7
25173
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 (Bubble, insertion, selection..). I have an array of int's and am passing them to each sort function.
0
375
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 : 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
3
1863
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 the PHP manual or Google. I have a tab-delimited text file containing numerous lines of data, which is searched for matches to a query from an HTML form. I want to be able to sort the resulting two-dimensional array of matches by one column...
75
4202
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 elements are made. I was thinking of using a function, that kept an index, to selectively iterate over unsorted elements. But I am unable to think of a way to do it. Any ideas to improve the routine and get a better grade? Thanks to all.
0
1933
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. It basically prints out the user prompt, then after you enter the number of names, such as 3... it prints out: Enter a name: Enter a name: Enter a name: instead of Enter a name:
0
10156
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9832
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8831
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6649
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5275
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5419
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3924
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3531
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2805
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.