Reputation: 1627
I've came across a strange behavior. While debugging, when a while
-loop loops at first time: after going though /* "data-url" */
and /* "data-author" */
parts of code I have the next result in Debugging windows -> Watches
:
(I'm using Code::Blocks IDE, Ubuntu 13.04)
The length of dataUrl_tempString
is 8 bytes,
the length of dataAuthor_tempString
is 11 bytes,
the length of dataName_tempString
is 9 bytes...
But after going through /* data-name */
part of code I have the the result that confuses me:
Now they are not of size 8, 11 and 9 bytes!
What is the matter?
Could you help me finding the reason of such behavior?
Here is the code of that function:
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;
}
Upvotes: 1
Views: 360
Reputation: 70901
You might like to change the call to
int strcmp(const char *s1, const char *s2);
to become calls to
int memcmp(const void *s1, const void *s2, size_t n);
This shall fix the issue, as long as you do not use other members of the str*()
family of function on those (non 0
-terminated) char
arrays.
Note: However memcmp()
always compares the number of characters passed as 3rd parameter (n
). This might not be what you want.
Update:
Alternativly (as mixure of both calls above) there also is:
int strncmp(const char *s1, const char *s2, size_t n);
Which compares up until it finds a 0
-terminator in either s1
or s2
and to a maximum of n
characters.
Upvotes: 1
Reputation: 46365
A string needs to have space for a '\0'
termination - you only allocated 8 bytes for a string with 8 characters (which therefore needs 9 bytes minimum). Depending on what follows in memory, you will get unpredictable results.
Upvotes: 5