[英]How to read data from text file and sort a specific column in ascending order using the array method in C language?
我正在尝试从名为“tel.txt”的文本文件中读取并使用数组方法按部门升序对员工记录进行排序,但我无法识别代码中的错误并认为错误来自标签之间的行/* problematic code - */
电话.txt:
TP1|James|1231|sales|
TP2|chong|5324|admin|
TP3|Jack|1250|Finance|
屏幕上我想要的output(按部门排序):
TP2|chong|5324|admin|
TP3|Jack|1250|Finance|
TP1|James|1231|sales|
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
struct employee{
char id[10], name[40], department[15];
int telephone;
};
int main() {
struct employee dep_sort[20];
int count_line = 0, row, col;
char temp_store[20], c;
ptr = fopen("tel.txt", "r");
if (ptr == NULL)
{
printf("error file");
exit(1);
}
while ((c = fgetc(ptr)) != EOF)
{
if (c == '\n')
{
count_line++;
}
}
/* problematic code -start*/
while (c != EOF){
row = 0;
fscanf(ptr, "%[^|]|%[^|]|%d|%[^|]|\n", &dep_sort[row].id, &dep_sort[row].name, &dep_sort[row].telephone, &dep_sort[row].department);
row++;
}
/* problematic code -end */
for (row = 1; row <= count_line; row++) {
for (col = 0; col <= count_line - row; col++) {
if (strcmp(dep_sort[col].department, dep_sort[col + 1].department) > 0) {
strcpy(temp_store, dep_sort[col].department);
strcpy(dep_sort[col].department, dep_sort[col + 1].department);
strcpy(dep_sort[col + 1].department, temp_store);
}
}
}
for (row = 0; row <= count_line; row++) {
printf("\nID: %s\nName: %s\nTelephone: %d\nDepartment: %s\n", dep_sort[row].id, dep_sort[row].name, dep_sort[row].telephone, dep_sort[row].department);;
}
fclose(ptr);
}
我会将问题分为三个不同的步骤:读取文件、排序、显示。
请注意,在排序时,您可以直接交换结构; 无需交换每个单独的字段。
如果您使用标准qsort()
function 并提供比较标准,您也可以避免编写排序算法。
/**
gcc -std=c99 -o prog_c prog_c.c \
-pedantic -Wall -Wextra -Wconversion \
-Wwrite-strings -Wold-style-definition -Wvla \
-g -O0 -UNDEBUG -fsanitize=address,undefined
**/
#if defined _MSC_VER
# define _CRT_SECURE_NO_WARNINGS
# define strcasecmp _stricmp
#else
# undef __STRICT_ANSI__ // strcasecmp
#endif
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
typedef struct
{
char id[10], name[40], department[15];
int telephone;
} Employee;
int // employee count
read_employees(const char *file_name,
Employee *employees,
int capacity)
{
FILE *input=fopen(file_name, "r");
if(input==NULL)
{
perror("open file for reading");
exit(1);
}
int count=0;
char line[100];
while((count<capacity)&&fgets(line, 100, input))
{
Employee *e=employees+count;
int r=sscanf(line, "%[^|]|%[^|]|%d|%[^|]|",
e->id, e->name, &e->telephone, e->department);
if(r==4) // 4 fields successfully extracted
{
++count;
}
}
fclose(input);
return count;
}
void
sort_employees_by_department(Employee *employees,
int count)
{
for(int i=0; i<count; ++i)
{
Employee *ei=employees+i;
for(int j=i+1; j<count; ++j)
{
Employee *ej=employees+j;
if(strcasecmp(ei->department, ej->department)>0)
{
Employee tmp=*ei;
*ei=*ej;
*ej=tmp;
}
}
}
}
int
compare_employees_by_department(const void *lhs,
const void *rhs)
{
const Employee *le=lhs;
const Employee *re=rhs;
return strcasecmp(le->department, re->department);
}
void
show_employees(const Employee *employees,
int count)
{
for(int i=0; i<count; ++i)
{
const Employee *e=employees+i;
printf("ID: %s|Name: %s|Telephone: %d|Department: %s\n",
e->id, e->name, e->telephone, e->department);
}
}
int
main(void)
{
Employee employees[20];
int count=read_employees("tel.txt", employees, 20);
#if 1
sort_employees_by_department(employees, count);
#else
qsort(employees, (size_t)count, sizeof(Employee),
compare_employees_by_department);
#endif
show_employees(employees, count);
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.