以下是自己写的代码,没有运行成功.....
按照第一列的数字(item)进行升序排列
#include "stdio.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#include <ctype.h>
#define SIZE 33
struct store
{
int item;
int stock;
float price;
char name;
}store[SIZE];
void main()
{
int i,j;
struct store sto[SIZE],temp;
FILE *fp,*fo;
fp=fopen("G:\OUT1.TXT","r+");
fo=fopen("G:\temporary.txt","w+");
if(fp==NULL)
{
printf("\ncannot open the file!\n");
return;
}
else if(fo==NULL)
{
printf("\ncannot open the file!\n");
return;
}
else
{
for(i=0;i<SIZE;i++)
if(fread(&sto[i],sizeof(struct store),1,fp)!=1)
{
printf("read error\n");
}
for(i=0;i<SIZE;i++) /* 选择法排序 */
{
for(j=i+1;j<SIZE;j++)
if(sto[j].item>sto[i].item)
{
temp=sto[i];
sto[i]=sto[j];
sto[j]=temp;
}
}
fwrite(&sto[i],sizeof(struct store),1,fo) ;
}
fclose(fp);
fclose(fo);
}
数据如下
G:\OUT1.TXT
3012 14 97.99 Canon CanoScan Flatbed Scanner
1012 12 24.95 Mini Pocket Camera
2011 4 14.95 VHS to VHS-HQ to S-VHS Magnifying Glass
1512 9 239.99 Heavy Duty Lens Cap
1021 20 49.95 Olympus iSnap APS
1023 15 74.95 Extra Huge Video Microphone
3011 13 32.99 Super-Duper Color Kodak Film
1032 15 310.55 JVC Magical Moments Camcorder
1041 10 389.00 Minolta Maxxum 5
5031 12 55.95 NiMH Rechargeable Batteries 4-pack
1042 11 349.95 NiMH Ultra Quickoid Battery Charger
4012 10 44.98 5-Legged Tripod
1043 20 319.90 Canon Faster Blaster SLR
5012 33 2.95 Pan-X Film, 1 Roll
3112 10 19.95 El-Cheapo 28MM Lens
1045 20 89.99 Ultra-Compact Video Midget-izer
1511 7 129.95 Micro-Macro Video Enhancer
1522 4 114.95 Sony Night Vision Digital Movie Maker
2012 12 48.55 Epson Perfection Flat Bed Photo Printer
1031 9 279.99 VHS to Compact Disc Translator
2013 10 28.99 Tri-color Film, 10 rolls
3013 13 87.95 SONY 1.5 Inch Television
5021 12 6.25 Toshiba 2 MegaPixel Digital Camera
3015 7 257.95 Black Plastic Camera Case
1044 13 119.95 Nifty Nuggett Pocket WebCam
1011 20 54.95 Telephoto Pocket Camera
3111 4 67.50 X-10 Ninja Pan 'N Tilt
1521 10 219.99 Light Duty Lens Cap
3511 10 159.99 3-Legged Tripod
4011 4 35.98 4-Legged Tripod
5011 17 4.29 1-Legged Tripod
1022 13 189.95 Nikon Coolest PIX Camera
3014 8 267.95 Wireless Audio Video Transmitter