我正在使用 C 字符串和流来更好地理解它们。我有这个测试程序从输入文件读取固定大小的数据块到缓冲区,将缓冲区内容存储在中间存储中(在这种情况下,我希望存储能够存储三个不同的“读取”)和然后将读取的字符串和中间存储中的字符串之一写入输出文件。
关于这一点的说明:在每次迭代中,我只使用中间存储的两个第一个位置,并将第二个“存储的字符串”写入文件。
编码:
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 3
#define BUFFER_SIZE 5
int main(int argc, char** argv) {
FILE* local_stream_test = fopen("LOCAL_INPUT_FILE","r");
FILE* local_output_test = fopen("LOCAL_OUTPUT_TEST","w");
if(!local_stream_test) {
puts("!INPUT FILE");
return EXIT_FAILURE;
}
if(!local_output_test) {
puts("!OUTPUT FILE");
return EXIT_FAILURE;
}
char my_buffer[BUFFER_SIZE];
char test[SIZE];
char* test2[SIZE];
memset(my_buffer,0,sizeof(my_buffer));
memset(test,0,sizeof(test));
memset(test2,0,sizeof(test2));
int read = fread( my_buffer, sizeof(my_buffer[0]), sizeof(my_buffer)/sizeof(my_buffer[0]), local_stream_test );
printf("FIRST READ TEST: %d\n",read);
printf("\tMY_BUFFER, SIZEOF: %lu, STRLEN: %lu\n",sizeof(my_buffer),strlen(my_buffer));
fwrite(my_buffer,sizeof(my_buffer[0]),/*strlen(aux)*/ read,local_output_test);
char* aux_test = strdup(my_buffer);
printf("\tAUX_TEST STRLEN: %lu, ## %s\n",strlen(aux_test), aux_test);
free(aux_test);
aux_test = NULL;
while(read > 0) {
if(feof(local_stream)) {
puts("BYE");
break;
}
read = fread( my_buffer, sizeof(my_buffer[0]), sizeof(my_buffer)/sizeof(my_buffer[0]), local_stream_test );
aux_test = strdup(my_buffer);
if(!aux_test) {
puts("!AUX_TEST");
break;
}
printf("READ TEST: %d\n",read);
printf("\tMY_BUFFER, SIZEOF: %lu, STRLEN: %lu\n",sizeof(my_buffer),strlen(my_buffer));
printf("\tAUX_TEST, SIZEOF: %lu, STRLEN: %lu ** SIZEOF *AUX_TEST: %lu, SIZEOF AUX_TEST[0]: %lu\n",sizeof(aux_test),strlen(aux_test),sizeof(*aux_test),sizeof(aux_test[0]));
fwrite(aux_test,sizeof(aux[0]),/*strlen(aux)*/ read,local_output_test);
printf("** AUX_TEST: %s\n",aux_test);
test2[0] = aux_test;
test2[1] = aux_test;
test2[1][3] = toupper(test2[1][3]);
fwrite(test2[1],sizeof(test2[1][0]),read,local_output_test);
printf("\n** TEST2[0] SIZEOF: %lu, STRLEN: %lu, TEST2[0]: %s\n",sizeof(test2[0]),strlen(test2[0]),test2[0]);
printf("\n** TEST2[1] SIZEOF: %lu, STRLEN: %lu, TEST2[1]: %s\n",sizeof(test2[1]),strlen(test2[1]),test2[1]);
strcpy(test2[1],aux_test);
printf("** COPIED TEST2[1]: %s\n",test2[1]);
free(aux_test);
aux_test = NULL;
puts("*******************************************");
}
return EXIT_SUCCESS;
}
输入文件:
converts a byte string to a floating point value
converts a byte string to an integer value
converts a byte string to an integer value
打印字符串时,我在第二次读取后会在结尾处得到额外的垃圾值。这是stdout
文件中第一次、第二次和第三次读取的输出:
FIRST READ TEST: 5
MY_BUFFER, SIZEOF: 5, STRLEN: 5
AUX_TEST STRLEN: 5, ## conve
READ TEST: 5
MY_BUFFER, SIZEOF: 5, STRLEN: 5
AUX_TEST, SIZEOF: 4, STRLEN: 5 ** SIZEOF *AUX_TEST: 1, SIZEOF AUX_TEST[0]: 1
** AUX_TEST: rts a
** TEST2[0] SIZEOF: 4, STRLEN: 5, TEST2[0]: rts a
** TEST2[1] SIZEOF: 4, STRLEN: 5, TEST2[1]: rts a
** COPIED TEST2[1]: rts a
*******************************************
READ TEST: 5
MY_BUFFER, SIZEOF: 5, STRLEN: 13
AUX_TEST, SIZEOF: 4, STRLEN: 13 ** SIZEOF *AUX_TEST: 1, SIZEOF AUX_TEST[0]: 1
** AUX_TEST: byte▒▒▒▒
** TEST2[0] SIZEOF: 4, STRLEN: 13, TEST2[0]: byTe▒▒▒▒
** TEST2[1] SIZEOF: 4, STRLEN: 13, TEST2[1]: byTe▒▒▒▒
** COPIED TEST2[1]: byTe▒▒▒▒
令我困扰的是,当垃圾值开始出现时,字符串的长度大于从文件中读取的字节:13
与5
. 我玩过,BUFFER_SIZE
但我总是在打印时得到垃圾值,stdout
除非大小足够大,可以一口气读取文件。
例如,当BUFFER_SIZE
等于 时500
,这是 中的输出stdout
:
FIRST READ TEST: 135
MY_BUFFER, SIZEOF: 300, STRLEN: 135
AUX_TEST STRLEN: 135, ## converts a byte string to a floating point value
converts a byte string to an integer value
converts a byte string to an integer value
BYE
并且生成的输出文件:
缓冲区大小 = 5
converts arts a byte byTe stri stRing tong To a fl a FloatinoatIng poig pOint vant Value
clue
converonvErts a ts A byte bytE strinstrIng to g tO an inan IntegertegEr valu vaLue
cone
cOnvertsverTs a by a Byte stte String rinG to anto An inte inTeger vger value
aluE
BUFFER_SIZE = 500:与输入文件相同。
所以,我正在访问越界内存,对吧?但是哪里?我找不到这个问题的根源(很可能我对如何使用 C 字符串有误解)。
PS:
我在这里读到,也许我的问题是我忘记在字符串末尾添加 NULL 标记。正在做:
test2[0] = aux_test;
test2[0][ strlen(aux_test)+1 ] = '\0';
/* OR THIS */
test2[0][read+1] = '\0';
产生相同的结果。