home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Power-Programmierung
/
CD2.mdf
/
c
/
library
/
mslang
/
cp1
/
rfile.c
< prev
next >
Wrap
Text File
|
1993-06-10
|
2KB
|
69 lines
===========================================================================
BBS: The Abacus * HST/DS * Potterville, MI
Date: 06-06-93 (12:39) Number: 85
From: JOHN GREEP Refer#: 47
To: JEFFERY FOY Recvd: NO
Subj: Reverse sorting? Conf: (36) C Language
---------------------------------------------------------------------------
JF>Say you have a text file like this:
JF>first
JF>second
JF>third
JF>...
JF>six-hundred-ninety-eight
JF>six-hundred-ninety-nine
JF>seven thousand
JF>How would you reverse the lines (i.e. in the example, seven thousand
JF>would become the first line, six-hundred-ninety-nine would become the
JF>second line ... first would become the last line).
This could be done by sorting the file in descending order. If it's
already sorted or in another desired order, you would have to read the
file from the end and work toward the beginning...Read a record and
write to a temporary file. When it's done, delete the original, and
rename the temporary file. You've got it tough though if each line is
a different length.
On a different thought, recursion would be perfect for this job.
This is just off the top of my head, so if there are any bugs,
you can let me know eh?
.
.
.
___---------Cut-Here-----------8<-----------------------------
/*
Recursive function to reverse a file.
Donated to the Public Domain by John Greep June 6, 1993
fp is a pointer to a file open for reading, positioned at the
beginning of the file.
tm is a pointer to a temporary file open for writing at the
beginning of the file.
Returns:
-1 on error or file empty.
0 on success.
*/
int reverse (FILE *fp, FILE *tm)
{
char temp[81]
if(NULL != fgets (temp, 81, fp))
return -1;
reverse (fp, tm);
fputs (temp,tm);
return 0;
}
___---------Cut-Here-----------8<-----------------------------
_____________
_/ () |-| /\/
* OLX 2.1 TD * Press any key to continue or any other key to quit
--- Maximus 2.01wb
* Origin: SECRET C::The C Source*HST DS*(403)256-7019,Calgary,Ab (1:134/21)
SEEN-BY: 1/211 11/2 4 13/13 101/1 108/89 109/25 110/69 114/5 123/19 124/1
SEEN-BY: 153/752 154/40 77 157/110 159/100 125 430 575 950 203/23 209/209
SEEN-BY: 261/1023 280/1 390/1 396/1 5 15 2270/1 2440/5 3603/20