я пытаюсь сортировать содержимое файла по имени здесь кодсортировочного содержимое файла
#include <iostream>
#include<string.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
struct Contacts
{
char name[20];
int phone;
char address[20];
};
int countLines(FILE * fp);
void sort_contact(int length , FILE * f);
int main(int argc, char** argv) {
FILE *f=fopen("D:\\eman.txt","r");
int c;
c=countLines(f);
printf("number of lines %d",c);
sort_contact(c , f);
return 0;
}
int countLines(FILE * fp){
char line[80];
int counter = 0;
while (fgets(line, sizeof line, fp)){
counter+=1;
}
return counter;
}
void sort_contact(int length , FILE * f)
{
// struct Contacts * eman [length];
int flag;
int i; // for loop counter
struct Contacts con;
struct Contacts tmp;
struct Contacts conn;
struct Contacts users[length];
//f=fopen("","r";)
//while((fscanf(f, "%s %d %s ", con.name ,&con.phone,con.address) != EOF) && (length > 0) )
while((fscanf(f, "%s %d %s ", users[length].name ,&users[length].phone,users[length].address) != EOF) && (length > 0) )
{
con=users[length];
conn=users[length--];
flag=strcmp(con.name,conn.name);
//length --;
switch(flag)
{
case 1:
tmp=users[length--];
users[length--]=users[length];
users[length]=tmp;
break;
case 0:
case -1:
break;
}
length --;
//seek(fd, 0, SEEK_END);
}//end fo while
fclose(f);
f=fopen("D:\\eman.txt","+w");
for(i=length;i>0;i--)
{
// tmp=users[i];
fprintf(f,"%s %d %s",users[i].name,users[i].phone, users[i].address);
}
fclose(f);
}
я поместить содержимое в структуре, а затем отсортировать структуру по имени и добавить его снова файл , когда я его запускаю, ничего не изменяется в файле
1) нужно перемотать. – BLUEPIXY
Что? 'iostream' для C-программы? –
2) 'Тип массива [Размер];' доступ к 'array [Size]' выходит за пределы. – BLUEPIXY