delete end character / null terminator ?

T

tntelle

I am having an issue with comparing two strings. I call two separate
functions to get each what should be the same thing. Everything looked
fine but the string compare failed. I put quotes around each and this
is what happens when I print the values:

VALUE1 = "THIS IS THE VALUE"
VALUE2 = "THIS IS THE VALUE
X"

The X ends up being one or two characters that are random and is on
the next line-- the value changes sometimes after I recompile.
I tried various things such as getting the string length of VALUE1,
creating a malloc variable and setting the array length to that value
and that did not help. I subtracted 1, 2 and so forth from the string
length of VALUE1, but the character is still there. I was thinking it
could be the null terminating character, but since every C string
should have that ( I am assuming) I figured that wasn’t it and I
should not try to remove that.

Here is the segment of code that is feeding VALUE2

(CKSUM_ATTRIBUTE = VALUE1, RETURN_CODE_2 = VALUE2)



BUFSIZE_2 = strlen(CKSUM_ATTRIBUTE);
char VARIABLES_2[256];
char *RETURN_CODE_2;
RETURN_CODE_2 = (char *)malloc(BUFSIZE_2);
unsigned int CKSUM_MATCH;


snprintf(VARIABLES_2, sizeof VARIABLES_2, "/usr/bin/cksum
'%s'",ARGV1);

FILE * f_2 = popen(VARIABLES_2, "r");
size_t r_2;
while((r_2 = fread(RETURN_CODE_2, sizeof(char), BUFSIZE_2 - 1, f_2))
RETURN_CODE_2[r_2+1] = '\0';
}
pclose(f_2);

printf ("VALUE1: \"%s\"",RETURN_CODE_2);
printf ("VALUE2:: \"%s\"\n",CKSUM_ATTRIBUTE);

CKSUM_ATTRIBUTE already contains the output of calling cksum ARGV1,
that I have already confirmed, and RETURN_CODE_2 matches up until the
next line strange character situation.

I am lost for ideas and appreciate any guidance or idears. Thank you!
 
M

Morris Keesan

I am having an issue with comparing two strings. I call two separate
functions to get each what should be the same thing. Everything looked
fine but the string compare failed. I put quotes around each and this
is what happens when I print the values:

VALUE1 = "THIS IS THE VALUE"
VALUE2 = "THIS IS THE VALUE
X"

The X ends up being one or two characters that are random and is on
the next line-- the value changes sometimes after I recompile.
I tried various things such as getting the string length of VALUE1,
creating a malloc variable and setting the array length to that value
and that did not help. I subtracted 1, 2 and so forth from the string
length of VALUE1,

Your problem is that you're thinking in terms of
subtracting, instead of adding.

If VALUE1 points to "THIS IS THE VALUE", then to allocate an array
large enough to hold a copy of VALUE1, you need to ADD ONE to the
length, in order to have a place to put the terminating value.
E.g.

buf = malloc(strlen(VALUE1) + 1);
strcpy (buf, VALUE1);
 
I

Ike Naar

while((r_2 = fread(RETURN_CODE_2, sizeof(char), BUFSIZE_2 - 1, f_2))
RETURN_CODE_2[r_2+1] = '\0';
}

The return value from fread() indicates the number of characters read.
These characters are stored in the first r_2 bytes of RETURN_CODE_2
i.e. RETURN_CODE_2[0:r_2-1] .
To null-terminate the string in RETURN_CODE_2, you should put the
null character in RETURN_CODE_2[r_2], not in RETURN_CODE_2[r_2+1] .
 
T

tntelle

tntelle   said:
 while((r_2 = fread(RETURN_CODE_2, sizeof(char), BUFSIZE_2 - 1, f_2))
       RETURN_CODE_2[r_2+1] = '\0';
}

The return value from fread() indicates the number of characters read.
These characters are stored in the first r_2 bytes of RETURN_CODE_2
i.e. RETURN_CODE_2[0:r_2-1] .
To null-terminate the string in RETURN_CODE_2, you should put the
null character in RETURN_CODE_2[r_2], not in RETURN_CODE_2[r_2+1] .




I've tried both of these solutions and there is no difference. In #1 I
dont think the problem is the array is not large enough - it has all
the data and then whatever else at the end. #2 does not have any
impact at all- same issue. Any other thoughts ? Thanx!
 
B

Ben Bacarisse

tntelle said:
tntelle   said:
 while((r_2 = fread(RETURN_CODE_2, sizeof(char), BUFSIZE_2 - 1, f_2))
1) {
       RETURN_CODE_2[r_2+1] = '\0';
}

The return value from fread() indicates the number of characters read.
These characters are stored in the first r_2 bytes of RETURN_CODE_2
i.e. RETURN_CODE_2[0:r_2-1] .
To null-terminate the string in RETURN_CODE_2, you should put the
null character in RETURN_CODE_2[r_2], not in RETURN_CODE_2[r_2+1] .




I've tried both of these solutions and there is no difference. In #1 I
dont think the problem is the array is not large enough - it has all
the data and then whatever else at the end. #2 does not have any
impact at all- same issue. Any other thoughts ? Thanx!

You don't show the code that gets both the strings (or if you did,
I missed it) but it seems clear enough that you are leaving a newline
in one and not the other. The output of a command will almost always
have a trailing newline which you must either always keep or always
remove if you are to get a match.

Note that while loop will go horribly wrong if it loops more than
once. Also not that the fact the advice did not fix the problem
does not mean you don't need to take it. For example, the
null-termination of RETURN_CODE_2 is wrong in the snippet you show.
You need to drop the +1 or change it to -1 if you want to remove the
newline (after checking that it is OK to do so, of course).
 
T

tntelle

tntelle said:
 while((r_2 = fread(RETURN_CODE_2, sizeof(char), BUFSIZE_2 - 1, f_2))
1) {
       RETURN_CODE_2[r_2+1] = '\0';
}
The return value from fread() indicates the number of characters read.
These characters are stored in the first r_2 bytes of RETURN_CODE_2
i.e. RETURN_CODE_2[0:r_2-1] .
To null-terminate the string in RETURN_CODE_2, you should put the
null character in RETURN_CODE_2[r_2], not in RETURN_CODE_2[r_2+1] .
I've tried both of these solutions and there is no difference. In #1 I
dont think the problem is the array is not large enough - it has all
the data and then whatever else at the end. #2 does not have any
impact at all- same issue. Any other thoughts ? Thanx!

You don't show the code that gets both the strings (or if you did,
I missed it) but it seems clear enough that you are leaving a newline
in one and not the other.  The output of a command will almost always
have a trailing newline which you must either always keep or always
remove if you are to get a match.

Note that while loop will go horribly wrong if it loops more than
once.  Also not that the fact the advice did not fix the problem
does not mean you don't need to take it.  For example, the
null-termination of RETURN_CODE_2 is wrong in the snippet you show.
You need to drop the +1 or change it to -1 if you want to remove the
newline (after checking that it is OK to do so, of course).

I changed the code to:

snprintf(VARIABLES_2, sizeof VARIABLES_2, "/usr/bin/cksum
'%s'",ARGV1);

FILE * f_2 = popen(VARIABLES_2, "r");
size_t r_2;
while((r_2 = fread(RETURN_CODE_2, sizeof(char), BUFSIZE_2, f_2)) >
1) {
RETURN_CODE_2[r_2] = '\0';
}
pclose(f_2);


So I removed the -1 from BUFSIZE_2 - 1 and also I removed the +1 from
RETURN_CODE_2[r_2]
and that seems to have fixed the problem. Thanks for all the help!
 
F

Fred

tntelle said:
On Aug 18, 12:48 am, (e-mail address removed) (Ike Naar) wrote:
 while((r_2 = fread(RETURN_CODE_2, sizeof(char), BUFSIZE_2 - 1, f_2))
1) {
       RETURN_CODE_2[r_2+1] = '\0';
}
The return value from fread() indicates the number of characters read.
These characters are stored in the first r_2 bytes of RETURN_CODE_2
i.e. RETURN_CODE_2[0:r_2-1] .
To null-terminate the string in RETURN_CODE_2, you should put the
null character in RETURN_CODE_2[r_2], not in RETURN_CODE_2[r_2+1] .
I've tried both of these solutions and there is no difference. In #1 I
dont think the problem is the array is not large enough - it has all
the data and then whatever else at the end. #2 does not have any
impact at all- same issue. Any other thoughts ? Thanx!
You don't show the code that gets both the strings (or if you did,
I missed it) but it seems clear enough that you are leaving a newline
in one and not the other.  The output of a command will almost always
have a trailing newline which you must either always keep or always
remove if you are to get a match.
Note that while loop will go horribly wrong if it loops more than
once.  Also not that the fact the advice did not fix the problem
does not mean you don't need to take it.  For example, the
null-termination of RETURN_CODE_2 is wrong in the snippet you show.
You need to drop the +1 or change it to -1 if you want to remove the
newline (after checking that it is OK to do so, of course).
- Show quoted text -

I changed the code to:

  snprintf(VARIABLES_2, sizeof VARIABLES_2, "/usr/bin/cksum
'%s'",ARGV1);

  FILE * f_2 = popen(VARIABLES_2, "r");
  size_t r_2;
  while((r_2 = fread(RETURN_CODE_2, sizeof(char), BUFSIZE_2, f_2)) >
1) {
        RETURN_CODE_2[r_2] = '\0';}

pclose(f_2);

So I removed the -1 from BUFSIZE_2 - 1 and also I removed the +1 from
RETURN_CODE_2[r_2]
and that seems to have fixed the problem. Thanks for all the help!- Hide quoted text -

- Show quoted text -

You still don't have enough room allocated for RETURN_CODE_2.

You malloc'd BUFSIZE_2 bytes for it. If the fread reads that many
bytes,
you will overflow when you put '\0' into position r_2.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,996
Messages
2,570,238
Members
46,826
Latest member
robinsontor

Latest Threads

Top