正如其他人提到的,Unix 中有 sort 命令:
sort myfile.txt
请参阅man sort 了解更多选项。
Windows 上有SORT 命令:
sort myfile.txt
或
SORT myfile.txt
如需更多帮助,请参阅:http://www.computerhope.com/sorthlp.htm
这样你就可以编写简单的 C 程序来使用这些命令。
// sort.system.c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, char **argv) {
if (argc != 2) { // expecting one argument, file which we will sort
fprintf(stderr, "Usage: sort [FILE]\n");
return EXIT_FAILURE;
}
int length = 5 + strlen(argv[1]) + 1; // length of "sort " is 5.
char *command = (char *) calloc(length, sizeof(char));
sprintf(command, "sort %s", argv[1]);
int ret = system(command);
return ret;
}
这里我们只期望用户输入他想要排序的文件,然后我们正在使用这个文件并将它交给sort 命令。但首先我们需要确保用户实际输入了一些内容。这就是为什么我们在main 函数的开头有if。
第二种方法是自己编写sort 命令。乐趣从这里开始。 :D
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX_LINE_LENGTH 1000000
// compare function for qsort function
int compare (const void *a, const void *b) {
const char **s1 = (const char **) a;
const char **s2 = (const char **) b;
return strcmp(*s1, *s2);
}
int main(int argc, char **argv) {
if (argc != 2) { // expecting one argument, file which we will sort
fprintf(stderr, "Usage: sort [FILE]\n");
return EXIT_FAILURE;
}
char *filename = argv[1]; // name of file to sort
FILE *file = fopen(filename, "r"); // trying to open that file
if (file == NULL) {
fprintf(stderr, "Could not open file %s\n", filename);
return EXIT_FAILURE;
}
int size = 16; // initial size of lines container
char **lines = (char **) calloc(size, sizeof(char *)); // lines container
if (lines == NULL) { // safety check
fprintf(stderr, "Could not allocate the requested block of memory\n");
return EXIT_FAILURE;
}
// buffer where we will store every line before putting it into line container
char *buffer = (char *) calloc(MAX_LINE_LENGTH + 1, sizeof(char));
if (buffer == NULL) { // safety check
fprintf(stderr, "Could not allocate the requested block of memory\n");
return EXIT_FAILURE;
}
int counter = 0; // line counter, counts how many lines we have readed
// reading whole line into buffer
while(fgets(buffer, MAX_LINE_LENGTH + 1, file) != NULL) {
// measuring line length with strlen and adding 1 for '\0'
int length = strlen(buffer);
if (buffer[length - 1] == '\n') { // remove '\n' on end of line
// replace '\n' with '\0', thus '\0' already included into length
buffer[length - 1] = '\0';
} else {
length++; // include '\0'
}
// if our lines container is not large enough we will double its size
if (counter + 1 == size) {
size *= 2;
lines = (char **) realloc(lines, size*sizeof(char *));
if (lines == NULL) { // safety check
fprintf(stderr, "Could not allocate the requested block of memory\n");
return EXIT_FAILURE;
}
}
// allocating space for line
lines[counter] = (char *) malloc(length*sizeof(char));
strcpy(lines[counter], buffer);
counter++;
}
// using qsort function from stdlib.h
qsort(lines, counter, sizeof(char *), compare);
int i;
for (i = 0; i < counter; i++) {
puts(lines[i]);
}
return EXIT_SUCCESS;
}
想法是有一些初始大小为 16 的动态“行容器”。每次我们发现需要更多空间来存储行时,我们将“行容器”的大小加倍。
此外,我们有一些缓冲区用于存储每一行,然后我们将该行复制到下一行容器槽中。
在这里,我将一行的大小限制为 10^6 个字符。但是您也可以使用ftell 和fseek 函数为缓冲区动态分配空间。我建议你这样做是为了练习。 :)
对于字符串的实际排序,我们使用来自stdlib.h 的qsort 函数:
http://www.cplusplus.com/reference/cstdlib/qsort/