从文本文件中读取整数,并将它们分类为另一个文本文件和二进制文件

时间:2012-03-20 15:18:59

标签: c sorting io

我忘了包含编程语言(它应该是C语言)。

我在做这个程序时需要帮助。我们非常感谢示例代码。

程序读取包含以下内容的文件:

一个。要排序的整数数,后跟  湾要排序的整数(每行一个整数)(必须与指定的整数数相同)。

然后将从另一个文本文件和二进制文件中从最低到最高进行排序。

其他规格:

  1. 使用动态内存分配
  2. 终端中的格式应为:

    ./program.out  original-file.txt  output-file.txt  output-file.bin
    
  3. 其中program.out是程序本身,original-file.txt是文本文件,其中包含要排序的整数数和未排序的整数,output-file.txtoutput-file.bin包含排序整数。

    错误检查:

    1. 检查malloc()是否成功返回
    2. 原始文件将如下所示:

      3 #number of integers to be sorted
      3 #the integers-separated by new line
      2
      1
      

      输出文件:

      3
      1
      2
      3
      

      提前非常感谢:)上帝保佑!

2 个答案:

答案 0 :(得分:0)

以下是一些示例代码。如果你谷歌这些功能名称,如果你在课堂上保持清醒,我相信你会做得很好。你可能需要:

  • 打开一个文件:

    FILE* inFile = fopen("input.txt", "r");

  • 从输入中读取单个值

    int numConverted = fscanf(inFile, "%d", &value);

  • 分配一些内存:

    int *pInts = malloc(n * sizeof(int));

  • 对数组进行排序

    // The parameters are tricky! Check your class notes or text book qsort(...)

  • 将单个值写为文本

    fprintf(outFile, "%d\n", i);

  • 将单个值写为二进制:

    fwrite( &i, sizeof i, 1, outBinFile);

答案 1 :(得分:0)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct tree {
  int num, cnt;
  struct tree *left, *right;
};
struct tree *add(struct tree *t, int val) {
  if (!t) {
    if (!(t = malloc(sizeof(struct tree))))
      perror("Not enough memory"), exit(-1);
    memset(t, 0, sizeof(struct tree));
    t->num = val;
    ++t->cnt;
    return t;
  }
  if (val < t->num)
    t->left = add(t->left, val);
  else if (val > t->num)
    t->right = add(t->right, val);
  else
    ++t->cnt;
  return t;
}
int walk(struct tree *t, int (*f)(struct tree *, void*), void *data) {
  int rc;
  if (!t)
    return 0;
  rc = walk(t->left, f, data);
  rc += f(t, data);
  rc += walk(t->right, f, data);
  return rc;
}
struct tree *clean(struct tree *t) {
  if (!t)
    return NULL;
  t->left = clean(t->left);
  t->right = clean(t->right);
  free(t);
  return NULL;
}
int save(struct tree *t, void *data) {
  int i, rc = 0;
  FILE *fp = (FILE *) data;
  for (i = 0; i < t->cnt; ++i)
    rc += (fprintf(fp, "%d\n", t->num) < 0);
  return rc;
}
int saveb(struct tree *t, void *data) {
  int i, rc = 0;
  FILE *fp = (FILE *) data;
  for (i = 0; i < t->cnt; ++i)
    rc += (fwrite((void *) &t->num, sizeof t->num, 1, fp) != 1);
  return rc;
}
int main(int argc, char **argv) {
  int rc = 0;
  struct tree *t = NULL;
  char buff[0x200];
  FILE *fin, *fout, *foutb;
  if (argc < 4) {
    fprintf(stderr, "Usage: %s SRCFILE OUTFILE.TXT OUTFILE.BIN\n", argv[0]);
    exit(0);
  }
  if (!((fin = fopen(argv[1], "r")) && (fout = fopen(argv[2], "w"))
        && (foutb = fopen(argv[3], "wb")))) {
    perror("fopen");
    exit(-1);
  }
  while (fgets(buff, sizeof buff, fin))
    t = add(t, atoi(buff));
  rc += walk(t, save, (void *) fout);
  rc += walk(t, saveb, (void *) foutb);
  t = clean(t);
  fclose(fin);
  fclose(fout);
  fclose(foutb);
  return rc;
}

刚刚注意到“文件的第一个字符串中的整数数”规范;我认为你的老师会把它们放入阵列并对其进行排序;但无论如何要自己做:)

#include <stdio.h>
#include <stdlib.h>
int cmp(const void *a, const void *b) {
  return *(int*)a > *(int*)b ? 1 : (*(int*)a < *(int*)b ? -1 : 0);
}
int main(int argc, char **argv) {
  char buff[0x200];
  FILE *fin, *fout, *foutb;
  int i, *arr, sz = 0, rc = 0;
  if (argc < 4) {
    fprintf(stderr, "Usage: %s SRCFILE OUTFILE.TXT OUTFILE.BIN\n", argv[0]);
    exit(0);
  }
  if (!((fin = fopen(argv[1], "r")) && (fout = fopen(argv[2], "w"))
        && (foutb = fopen(argv[3], "wb")))) {
    perror("fopen");
    exit(-1);
  }
  if (fgets(buff, sizeof buff, fin)) {
    sz = atoi(buff);
    if (!(arr = malloc(sizeof(int) * sz)))
      perror("Not enough memory"), exit(-1);
    for (i = 0; i < sz && fgets(buff, sizeof buff, fin); ++i)
      arr[i] = atoi(buff);
  }
  qsort(arr, sz, sizeof(int), cmp);
  for (i = 0; i < sz; ++i)
    rc += (fprintf(fout, "%d\n", arr[i]) < 0);
  for (i = 0; i < sz; ++i)
    rc += (fwrite((void *) &arr[i], sizeof(int), 1, foutb) != 1);
  fclose(fin);
  fclose(fout);
  fclose(foutb);
  return rc;
}