短文件上的C fread()在文件结束后生成垃圾

时间:2017-12-09 20:08:12

标签: c file fread

我的教授指定file_1.txt一次只读100个字节到char array。我制作了以下代码段:

int key;
key = atoi(argv[1]);
FILE *file_pointer;
file_pointer = fopen(argv[2], "rb");
char buffer[100];
char output[sizeof(int)][100];
int output_counter = 0;
int read_counter;
int read_elements;

while(read_elements = fread(buffer, 1, 100, file_pointer) > 0)
{  
    read_counter = 0;    
    while(read_counter < 100)
    {

        printf("xor'ing %d and %d\n", key, buffer[read_counter]);
        output[output_counter][read_counter] = buffer[read_counter] ^ key;
        read_counter = read_counter + 1;
    }

    output_counter = output_counter + 1;
}

fclose(file_pointer);

file_1.txt读取:“项目的测试文件\ n”。我使用了gcc encrypt.c 然后:

./a.out 111 file_1.txt file_2.txt

然后:     ./a.out 111 file_2.txt file_3.txt

cat file_3.txt reads: 

项目的测试文件:

v▒▒▒s▒▒@////////▒

在使用file_1.txtfile_2.txt的程序首次运行期间,打印嵌套while循环中的print语句:

xor'ing 111 and 116

xor'ing 111 and 101

xor'ing 111 and 115

xor'ing 111 and 116

xor'ing 111 and 32

xor'ing 111 and 102

xor'ing 111 and 105

xor'ing 111 and 108

xor'ing 111 and 101

xor'ing 111 and 32

xor'ing 111 and 102

xor'ing 111 and 111

xor'ing 111 and 114

xor'ing 111 and 32

xor'ing 111 and 116

xor'ing 111 and 104

xor'ing 111 and 101

xor'ing 111 and 32

xor'ing 111 and 112

xor'ing 111 and 114

xor'ing 111 and 111

xor'ing 111 and 106

xor'ing 111 and 101

xor'ing 111 and 99

xor'ing 111 and 116

xor'ing 111 and 10

xor'ing 111 and 0

xor'ing 111 and 0

xor'ing 111 and 0

xor'ing 111 and 0

xor'ing 111 and 0

xor'ing 111 and 0

xor'ing 111 and 118

xor'ing 111 and 0

xor'ing 111 and 0

xor'ing 111 and 0

xor'ing 111 and 0

xor'ing 111 and 0

xor'ing 111 and 0

xor'ing 111 and 0

xor'ing 111 and -88

xor'ing 111 and -31

xor'ing 111 and -94

xor'ing 111 and 2

xor'ing 111 and -120

xor'ing 111 and 127

xor'ing 111 and 0

xor'ing 111 and 0

xor'ing 111 and 0

xor'ing 111 and 0

xor'ing 111 and 0

xor'ing 111 and 0

xor'ing 111 and 0

xor'ing 111 and 0

xor'ing 111 and 0

xor'ing 111 and 0

xor'ing 111 and -57

xor'ing 111 and 115

xor'ing 111 and -33

xor'ing 111 and 2

xor'ing 111 and -120

xor'ing 111 and 127

xor'ing 111 and 0

xor'ing 111 and 0

xor'ing 111 and 1

xor'ing 111 and 0

xor'ing 111 and 0

xor'ing 111 and 0

xor'ing 111 and 0

xor'ing 111 and 0

xor'ing 111 and 0

xor'ing 111 and 0

xor'ing 111 and -19

xor'ing 111 and 9

xor'ing 111 and 64

xor'ing 111 and 0

xor'ing 111 and 0

xor'ing 111 and 0

xor'ing 111 and 0

xor'ing 111 and 0

xor'ing 111 and 47

xor'ing 111 and 47

xor'ing 111 and 47

xor'ing 111 and 47

xor'ing 111 and 47

xor'ing 111 and 47

xor'ing 111 and 47

xor'ing 111 and 47

xor'ing 111 and 0

xor'ing 111 and 0

xor'ing 111 and 0

xor'ing 111 and 0

xor'ing 111 and 0

xor'ing 111 and 0

xor'ing 111 and 0

xor'ing 111 and 0

xor'ing 111 and -96

xor'ing 111 and 9

xor'ing 111 and 64

xor'ing 111 and 0

这些random元素来自哪里?

1 个答案:

答案 0 :(得分:0)

在使用fread()之前将缓冲区char数组的所有元素初始化为0,摆脱了垃圾。