我遇到了一个奇怪的行为。在调试时,当while
第一次循环循环时:经过部分代码后,我得到了下一个/* "data-url" */
结果:/* "data-author" */
Debugging windows -> Watches
(我正在使用 Code::Blocks IDE,Ubuntu 13.04)
的长度dataUrl_tempString
为8字节, 的长度dataAuthor_tempString
为11字节, 的长度dataName_tempString
为9字节...
但是在浏览了/* data-name */
部分代码之后,我得到了让我感到困惑的结果:
现在它们的大小不是 8、11 和 9 字节!
有什么事?
你能帮我找出这种行为的原因吗?
这是该函数的代码:
int SubString_Search(char *fnameNew, char *strUrl, char *strAuthor, char *strName) {
FILE *fp;
FILE *ofp_erase;
FILE *ofp;
char ch_buf;
int count = 0;
char dataUrl[8] = "";
char dataAuthor[11] = "";
char dataName[9] = "";
char *dataUrl_tempString = &dataUrl[0];
char *dataAuthor_tempString = &dataAuthor[0];
char *dataName_tempString = &dataName[0];
if( (fp = fopen("output_temp.txt", "r")) == NULL) {
printf("File could not be opened.\n");
return (-1);
}
else {
/* Erasing 'NEW' file if exists */
ofp_erase = fopen(fnameNew, "w");
fclose(ofp_erase);
}
ofp = fopen(fnameNew, "a");
rewind(fp);
while(!feof(fp)) {
/* "data-url" */
fread(dataUrl_tempString, 8, sizeof(char), fp);
if(memcmp(dataUrl_tempString, strUrl) == 0) {
fseek(fp, 2, SEEK_CUR); // going up to required place to copy a string
while( (ch_buf = getc(fp)) != '"') {
fputc(ch_buf, ofp);
}
fputc('\n', ofp);
}
fseek(fp, -8, SEEK_CUR);
/* "data-author" */
fread(dataAuthor_tempString, 11, sizeof(char), fp);
if(memcmp(dataAuthor_tempString, strAuthor) == 0) {
fseek(fp, 2, SEEK_CUR); // going up to required place to copy a string
while( (ch_buf = getc(fp)) != '"') {
fputc(ch_buf, ofp);
}
fputc(' ', ofp);
fputc('-', ofp);
fputc(' ', ofp);
}
fseek(fp, -11, SEEK_CUR);
/* "data-name" */
fread(dataName_tempString, 9, sizeof(char), fp);
if(memcmp(dataName_tempString, strName) == 0) {
fseek(fp, 2, SEEK_CUR); // going up to required place to copy a string
while( (ch_buf = getc(fp)) != '"') {
fputc(ch_buf, ofp);
}
//fputc() not needed
}
fseek(fp, -8, SEEK_CUR); // jumping over 1 symbol from the beginning: `-8` instead of `-9`...
count++;
if(count == 5)
break;
}
rewind(fp);
fclose(fp);
fclose(ofp);
return 0;
}