2015-10-15 97 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?

相關問題