是否有一个函数可以快速对C中的文件进行排序?

时间:2014-12-29 16:14:37

标签: c file function sorting

是的,我正在使用数据库编写一些代码,我学校的某个人正在讨论一些可以自动获取文件并对其进行排序的特定命令。

我想我已经掌握了如何手动对这个文件进行排序,但它需要做很多工作,所以我想知道是否有人知道这个功能。

编辑:使用DEV C ++,Library#stdio.h和#conio.h,(我可以根据需要添加额外的库)不知道具体的编译器是什么。

2 个答案:

答案 0 :(得分:2)

假设您的输入文件是data.txt(Linux),您可以使用系统命令sort

sort data.txt > output.txt

答案 1 :(得分:2)

正如其他人所说,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命令。但首先我们需要确保用户实际输入了一些内容。这就是我们if函数开头main的原因。

第二种方法是自己编写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;
}

想法是拥有一些动态的&#34;行容器&#34;它的初始大小为16.每次我们看到我们需要更多空间来存储我们的线条时,我们将&#34; line container&#34;的大小加倍。

另外,我们有一些缓冲区用于存储每一行​​,然后将该行复制到下一行容器槽中。

这里我将一行的大小限制为10 ^ 6个字符。但是,您也可以使用ftellfseek函数为缓冲区动态分配空间。我建议你这样做练习。 :)

对于字符串的实际排序,我们使用qsort中的stdlib.h函数:

http://www.cplusplus.com/reference/cstdlib/qsort/