写了个简单的,仅供参考。
/*
* Author: 吾好梦中杀猪
* 2013-14 All rights reserved.
*/
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <malloc.h>
#define ID_LENGTH 6
#define NAME_LENGTH 10
#define FILE_LINE_SIZE 26
#define TRUE 1
#define FALSE 0
#define IS_VALID_NUM(s) (((s) >= '0' && (s) <= '9') ? TRUE : FALSE)
typedef struct
{
char m_Id[ID_LENGTH+1];
char m_Name[NAME_LENGTH+1];
int m_age;
}TStudent;
enum
{
upSort,
downSort,
};
typedef int (*pStrCmpFunc)(const char *src, const char *dst, int len);
const char *fileName = "list.txt";
int isStringEqual(const char *src, const char *dst, int len)
{
int i;
for (i=0; i<len; i++)
if (*src++ != *dst++)
return FALSE;
return TRUE;
}
int isLeftGreater(const char *src, const char *dst, int len)
{
int i;
for (i=0; i<len; i++)
{
if (*src < *dst)
return FALSE;
else if (*src > *dst)
return TRUE;
src++;
dst++;
}
return FALSE;
}
int isLeftSmaller(const char *src, const char *dst, int len)
{
int i;
for (i=0; i<len; i++)
{
if (*src > *dst)
return FALSE;
else if (*src < *dst)
return TRUE;
src++;
dst++;
}
return FALSE;
}
void add()
{
FILE *pFile = NULL;
TStudent student;
int nameLen = 0;
if (NULL == (pFile = fopen(fileName, "ab")))
{
printf("Open file %s error!\n", fileName);
return;
}
printf("Please input student ID: ");
scanf("%s", student.m_Id);
printf("Please input student name: ");
scanf("%s", student.m_Name);
nameLen = strlen(student.m_Name);
memset(student.m_Name + nameLen, ' ', sizeof(student.m_Name) - nameLen);
student.m_Name[NAME_LENGTH] = '\0';
printf("Please input student age: ");
scanf("%d", &student.m_age);
fprintf(pFile, "\r\n%s %s %d", student.m_Id, student.m_Name, student.m_age);
fclose(pFile);
printf("%s is added!\n", student.m_Id);
}
void del()
{
FILE *pFile = NULL;
char *pBuf = NULL;
char id[ID_LENGTH + 1];
int fileLen;
int i;
int isFound = FALSE;
if (NULL == (pFile = fopen(fileName, "rb")))
{
printf("Open file %s error!\n", fileName);
return;
}
fseek(pFile, 0, SEEK_END);
fileLen = ftell(pFile);
rewind(pFile);
printf("Please input student ID: ");
scanf("%s", id);
if (NULL == (pBuf = (char *)malloc(sizeof(char) * fileLen)))
{
printf("Malloc memory error!\n");
return;
}
fread(pBuf, sizeof(char), fileLen, pFile);
fclose(pFile);
for (i=0; i<fileLen; i++)
{
if (IS_VALID_NUM(pBuf[i]) && isStringEqual(&pBuf[i], id, ID_LENGTH))
{
isFound = TRUE;
if (i + FILE_LINE_SIZE < fileLen)
{
memcpy(&pBuf[i], &pBuf[i+FILE_LINE_SIZE], fileLen - i - FILE_LINE_SIZE);
fileLen -= FILE_LINE_SIZE;
}
else
fileLen = i;
break;
}
}
if (isFound)
{
if (NULL == (pFile = fopen(fileName, "wb")))
{
printf("Open file %s error!\n", fileName);
return;
}
fwrite(pBuf, sizeof(char), fileLen, pFile);
fclose(pFile);
printf("%s is deleted!\n", id);
}
else
{
printf("%s is not found!\n", id);
}
free(pBuf);
}
void view()
{
FILE *pFile = NULL;
char *pBuf = NULL;
int fileLen;
if (NULL == (pFile = fopen(fileName, "rb")))
{
printf("Open file %s error!\n", fileName);
return;
}
fseek(pFile, 0, SEEK_END);
fileLen = ftell(pFile);
rewind(pFile);
if (NULL == (pBuf = (char *)malloc(sizeof(char) * (fileLen + 1))))
{
printf("Malloc memory error!\n");
return;
}
fread(pBuf, sizeof(char), fileLen, pFile);
pBuf[fileLen] = '\0';
printf("%s\n", pBuf);
free(pBuf);
fclose(pFile);
}
void sort(int direction)
{
FILE *pFile = NULL;
char *pBuf = NULL;
char *pLeft, *pRight;
char tmpBuf[FILE_LINE_SIZE - 2];
int fileLen;
int i, j;
int isFound = FALSE;
int pos = 0;
int count = 0;
int result = 0;
pStrCmpFunc compare;
if (direction == upSort)
compare = isLeftGreater;
else
compare = isLeftSmaller;
if (NULL == (pFile = fopen(fileName, "rb")))
{
printf("Open file %s error!\n", fileName);
return;
}
fseek(pFile, 0, SEEK_END);
fileLen = ftell(pFile);
rewind(pFile);
if (NULL == (pBuf = (char *)malloc(sizeof(char) * (fileLen + 1))))
{
printf("Malloc memory error!\n");
return;
}
fread(pBuf, sizeof(char), fileLen, pFile);
pBuf[fileLen] = '\0';
fclose(pFile);
for (i=0; i<fileLen; i++)
if (IS_VALID_NUM(pBuf[i]))
break;
pos = i;
while (i < fileLen)
{
if (IS_VALID_NUM(pBuf[i]))
{
count++;
i += FILE_LINE_SIZE;
}
}
for (i=0; i<count; i++)
{
result = i;
for (j=i+1; j<count; j++)
{
pLeft = pBuf + pos + result * FILE_LINE_SIZE;
pRight = pBuf + pos + j * FILE_LINE_SIZE;
if (compare(pLeft, pRight, ID_LENGTH))
{
result = j;
}
}
if (result != i)
{
pLeft = pBuf + pos + i * FILE_LINE_SIZE;
pRight = pBuf + pos + result * FILE_LINE_SIZE;
memcpy(tmpBuf, pLeft, sizeof(tmpBuf));
memcpy(pLeft, pRight, sizeof(tmpBuf));
memcpy(pRight, tmpBuf, sizeof(tmpBuf));
}
}
printf("%s\n", pBuf);
free(pBuf);
}
void up()
{
sort(upSort);
}
void down()
{
sort(downSort);
}
void showMenu()
{
printf("\n");
printf("======================================================\n");
printf("= 1. Show all students info =\n");
printf("= 2. Add a student info =\n");
printf("= 3. Delete a student info =\n");
printf("= 4. Sort up student info =\n");
printf("= 5. Sort down student info =\n");
printf("= 0. Exit =\n");
printf("======================================================\n");
printf("Please enter your choice: ");
}
int main()
{
char choice;
int quit = FALSE;
while (!quit)
{
showMenu();
scanf("%c", &choice);
switch(choice)
{
case '0':
quit = TRUE;
break;
case '1':
view();
break;
case '2':
add();
break;
case '3':
del();
break;
case '4':
up();
break;
case '5':
down();
break;
default:
printf("You enter a wrong choice!\n");
printf("Press any key to continue...\n");
fflush(stdin);
getch();
break;
}
fflush(stdin);
}
return 0;
}