对文本文件中的数字进行排序

时间:2015-10-15 20:15:04

标签: c

我必须用C编写一个程序并让它读取我创建的输入文本文件并输出到输出文件并显示原始数字然后对它们进行排序。我有大部分工作,但它只是在读取原始输入时试图超过一定数量时给我一个错误。这是我的代码:

    #include <stdio.h>
#include <stdlib.h>
#include <string.h>

//function prototypes

void sortNumbers(int *num, int count);

void swap(int *nums, int i, int j);

//main function

int main(int argc, char *argv[])

{

 //declare the variables

 int *numbers;

 int count = 0, i = 0;

 char input[20], output[20];

 FILE *fileIn, *fileOut;

 //allocate the default size of array to maximum of 100

 numbers = (int *)malloc(sizeof(int));

 //check the number of arguments at command line

 if (argc < 2)

 {

      printf("Please provide the input and output text file names as ./a.out name1 name2 \n");

      return 0;

 }

 //read the file names from command line

 else

 {

      sscanf(argv[1], "%s", input);

      sscanf(argv[2], "%s", output);

 }

 //open the files

 fileIn = fopen(input, "r");

 fileOut = fopen(output, "w");

 //condition to check whether the input file and output

 //file are able to open or not. If not print the error message

 if (fileIn == NULL)

 {

      printf("Input file %s cannot be opened.\n", input);

      return 0;

 }

 else if (fileOut == NULL)

 {

      printf("Output file %s cannot be opened. \n", output);

      return 0;

 }

 //read the data from the file

 else

 {

      fscanf(fileIn, "%d", &numbers[i]);

      printf("%d\n", numbers[i]);

      fprintf(fileOut, "%d\n", numbers[i]);



      while (!feof(fileIn))

      {    i++;     

          numbers = (int *)realloc(numbers,(i)*sizeof(int));

          fscanf(fileIn, "%d", &numbers[i]);

          printf("%d\n", numbers[i]);

          fprintf(fileOut, "%d\n", numbers[i]);                                       

      }

 }

 count = i;

 //close the files

 fclose(fileIn);



 printf("\n");

 //sort the elements in the array

 sortNumbers(numbers, count);

 fprintf(fileOut,"\nElements after sorting are: \n");

 printf("\nElements after sorting are: \n");

 //print the elements to the console and to the

 //output file

 for(i=0;i<count;i++)

 {

      fprintf(fileOut,"%d \n", numbers[i]);

      printf("%d \n", numbers[i]);

 }

 fclose(fileOut);

 return 0;

}

//sort algorithm using selection sort

void sortNumbers(int *num, int count)

{

 for (int i = 0; i < count; ++i)

 {

      int minIndex = i;

      for (int j = i + 1; j < count; ++j)

      {

          if(num[j]<num[minIndex])

               minIndex = j;

      }

      swap(num, minIndex, i);

 }   

}

//swap function

void swap(int *nums, int i, int j)

    {

 int temp = nums[i];

 nums[i] = nums[j];

 nums[j] = temp;

}

3 个答案:

答案 0 :(得分:2)

i从零开始,你分配了1,你应该重新分配i + 1。当你的程序站立时,你将读入超出你分配的内存空间。此外,计数不等于i。 i是一个索引,它比数组中的计数小0到1。将计数设置为i将使其小于实际大小。

答案 1 :(得分:1)

我在你的程序中看到了几个问题:

  1. 您读取和计算成功读取数字的逻辑是错误的。它会被1点关闭。

  2. 您在realloc的调用中分配的对象数少于所需的对象数。

  3. 我会用:

    替换最终else {}块的内容
    else
    {
       while ( fscanf(fileIn, "%d", &numbers[i]) == 1 )
       {
          printf("%d\n", numbers[i]);
          fprintf(fileOut, "%d\n", numbers[i]);
          ++i;
          numbers = realloc(numbers,(i+1)*sizeof(int));
       }
    }
    

    另见Why is “while ( !feof (file) )” always wrong?

答案 2 :(得分:0)

您已将ints分配到numbers,但您正在访问i元素。这会导致内存访问错误。将您的realloc更改为:

numbers = (int *)realloc(numbers,(i+1)*sizeof(int));

相关问题