【问题标题】:Read integers from a text file and sort them into another text file and a binary file从文本文件中读取整数并将它们排序到另一个文本文件和二进制文件中
【发布时间】:2012-03-20 15:18:58
【问题描述】:

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

我在执行此程序时需要帮助。示例代码将不胜感激。

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

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

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

其他规格:

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

    ./program.out  original-file.txt  output-file.txt  output-file.bin
    

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

错误检查:

  1. 检查 malloc() 是否成功返回

原始文件如下所示:

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

输出文件:

3
1
2
3

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

【问题讨论】:

  • 那么,到目前为止,您尝试了什么?
  • 这在我看来就像一个课程作业。是吗?
  • output-file.bin 是什么?二进制格式的整数(使用Int 类型)?

标签: c sorting io


【解决方案1】:
#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;
}

【讨论】:

  • 非常感谢!我现在对该怎么做有了一个想法。 :) 上帝保佑!
  • 想法? :) 你需要做的就是复制粘贴第二个代码并编译它:) 希望你有时间真正阅读和理解它。
  • 尽管我想复制和粘贴,但我想创建自己的代码 :)) 我尝试运行程序,但要排序的整数数(在上面的示例中是数字 3 ) 没有写在输出文件上,我应该怎么做才能让它出现在输出文件上?
  • @John 好吧,您应该将其写入输出文件。您可能注意到,它存储在 sz 变量中。
  • 哦,我明白了,非常感谢!如果我在代码中遇到一些问题,我会再问一些问题,如果你不介意的话。再次感谢!
【解决方案2】:

这里有一些示例代码。如果你在谷歌上搜索这些函数名称,并且在课堂上保持清醒,我相信你会做得很好。您可能需要:

  • 打开一个文件:

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

  • 从输入中读取单个值

    int numConverted = fscanf(inFile, "%d", &amp;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( &amp;i, sizeof i, 1, outBinFile);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多