简体   繁体   English

读取文件并按升序排列整数后打印的垃圾数据

[英]Garbage data printed after reading a file and putting integers in ascending order

I am working on this code to read the file test_numbers.txt , and take the numbers, and print them in ascending order.我正在处理此代码以读取文件test_numbers.txt ,并获取数字,并按升序打印它们。 When I run my program it outputs:当我运行我的程序时,它输出:

7

8

9

10

40

41

42

43

50

-1843443791

This would be correct.这是正确的。 The numbers from 7-50 are correctly placed, but the problem pertains to the -1843443791. 7-50 的数字放置正确,但问题与 -1843443791 相关。 I have tried checking the text file and making sure there are no errors but I can't seem to find the issue.我试过检查文本文件并确保没有错误,但我似乎无法找到问题。 This is why it makes me think there is an issue with the code that I cannot spot.这就是为什么它让我认为代码存在我无法发现的问题。 Here is the text file: https://github.com/untamedspud/testNumbers .这是文本文件: https : //github.com/untamedspud/testNumbers I am only concerned about the test_number.txt file currently.我目前只关心 test_number.txt 文件。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void insert_in_array_ascending(int array[], int size);
int readfile(int array[], char *fname);
void print_int_array(int array[], int size);
int main()
{
    int primary[100];
    int count = 0;
    count += readfile(primary, "test_numbers.txt");

    insert_in_array_ascending(primary, count);
    count++;

    print_int_array(primary, count);

}
void insert_in_array_ascending(int array[], int size)
{
    int i, j, temp;
    for (i = 0;i<size;++i)
    {
        for (j=i+1;j<size;++j)
        {
            if (array[i]>array[j])
            {
                temp =  array[i];
                array[i] = array[j];
                array[j] = temp;
            }
        }
    }
}
int readfile(int array[], char *fname)
{
    int i = 0;
    FILE *file = fopen(fname, "r");
    while(fscanf(file, "%d", &array[i]) != EOF)
    {
        insert_in_array_ascending(array, i);
        i++;
    }
    return i;
}
void print_int_array(int array[], int size)
{
    for(int i=0;i<size;i++)
    {
        printf("%d", array[i]);
        printf("\n");
    }
}

In your original file, you did not have the "readfile" function, and the reading happened in the main function.在您的原始文件中,您没有“readfile”函数,读取发生在 main 函数中。 There, you needed a counter - and that was "count", incremented after each read.在那里,您需要一个计数器——这是“计数”,每次读取后递增。

Then you moved that section of code into a separate function, but you maintained the cycle framework (for example, you add readfile's return to a count initialized to zero, which is equivalent to assigning readfile's return to count).然后,您将该部分代码移动到一个单独的函数中,但您维护了循环框架(例如,您readfile 的返回添加到初始化为零的计数,这等效于将 readfile 的返回分配给计数)。 More importantly, you forgot the count increment :更重要的是,您忘记了计数增量

int count = 0;
count += readfile(primary, "test_numbers.txt");

insert_in_array_ascending(primary, count);
count++;
print_int_array(primary, count);

The net result is that print_int_array prints one item more than it read, and that last item's content is indefinite.最终结果是 print_int_array 打印的一项多于读取的一项,并且最后一项的内容是不确定的。 Hence your garbage.因此你的垃圾。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM