Как сравнивать строки
Добавлено: 04 дек 2005, 06:47
Не могу нормально построить индексный массив, как мне правильно выполнить сравнение строк в структуре?
Листинг:
Вот в функции indexmas() через обычное сравнение не работает, оно там что-то не то походу сравнивает, там строка в shop_list всегда меньше, чем в shop_list[i+1]
Листинг:
Код: Выделить всё
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <string.h>
#define input_file "c:/temp/SAOD_8I.txt"
#define output_file "c:/temp/SAOD_8O.txt"
int b[20];
struct shop
{
char name[20];
char street[20];
char cc[10];
char item[20];
} shop_list[20];
int read()
{
int i,s=0,j;
char *c=new char[500],*c1;
FILE *rf=fopen(input_file,"r");
if (rf==NULL)
{
printf("\nCant open the input file (%s)!",input_file);
getche();
exit(0);
}
while (!feof(rf))
{
fgets(c,500,rf);
c1=strtok(c,";");
for (i=0;i<strlen(c1);i++)
shop_list[s].name[i]=c1[i];
c1=strtok(NULL,";");
for (i=0;i<strlen(c1);i++)
shop_list[s].street[i]=c1[i];
c1=strtok(NULL,";");
for (i=0;i<strlen(c1);i++)
shop_list[s].cc[i]=c1[i];
c1=strtok(NULL,";");
for (i=0;i<strlen(c1)-1;i++)
shop_list[s].item[i]=c1[i];
s++;
}
delete[] c;
fclose(rf);
return s;
}
void indexmas()
{
int i,a,j;
for (i=0;i<20;i++)
b[i]=i;
for (i=0;i<20;i++)
for (j=19;j>i;j--)
if (shop_list[b[j-1]].name > shop_list[b[j]].name)
{
a=b[j];
b[j]=b[j-1];
b[j-1]=a;
}
}
int write()
{
int s=0,i;
FILE *wf=fopen(output_file,"w+");
if (wf==NULL)
{
printf("\nCant open the output file (%s)!",output_file);
getche();
exit(0);
}
return s;
}
main()
{
clrscr();
int c,i;
c=read();
printf("Schitano %d strok iz faila %s\n",c,input_file);
indexmas();
printf(" | | | |\n");
for (i=0;i<20;i++)
{
printf("%10s | %17s | %9s | %9s |\n",shop_list[b[i]].name,shop_list[b[i]].street,shop_list[b[i]].cc,shop_list[b[i]].item);
}
getche();
return 1;
}