2015-10-15 46 views
0

我必须在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; 

} 
+0

请将您的代码格式化为 – Les

回答

2

i开始是零,你分配1,你应该ReAlloc如果我+ 1。当你的程序站立时,你将读入超出你分配的内存空间。此外,伯爵不等于我。 i是一个索引,它的运行范围从0到1小于数组中的计数。设置计数到i将使它比实际尺寸少一个。

+0

这正是我所需要的。谢谢! – Joe

0

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

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

1

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

  1. 你的逻辑读取和计数的成功的读取数量的数字是错误的。它将被关闭。

  2. 您正在分配一个小于realloc的呼叫中所需的对象数量。

我会代替最终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?

相关问题