I am currently trying to manipulate a char pointer variable using strtok. Then using that in a conditional statement to append to an array depending on the current pointers content.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 48
#define sizel 10
#define sizer 128
void show_bits(unsigned int x[]);
void clear(unsigned int x[]);
int main(void)
{
char line[sizel][sizer];
unsigned int s[N];
unsigned int d[N];
unsigned int b[N];
FILE *fp1 = NULL;
int i = 0;
int tot = 0;
fp1 = fopen("LineEncoded.txt", "r");
while(fgets(line[i], sizer, fp1))
{
line[i][strlen(line[i]) - 1] = '\0';
i ;
}
clear(s);
i = 0;
char * pch;
pch = strtok (line," ");
while (pch != NULL)
{
if(pch == '0'){
s[i]=1;
i ;
}
else if(pch == "--" || pch == " "){
s[i]=0;
i ;
}
pch = strtok (NULL, " ");
}
show_bits(s);
return 0;
}
void show_bits(unsigned int x[]){
int i;
for (i = 0; i < N; i )
{
if (i % 32 == 0) printf ("\n");
fprintf(stdout, "%d", x[i]);
}
fprintf(stdout, "\n");
}
void clear(unsigned int x[]){
int i;
for (i = 0; i < N; i )
x[i] = 0;
}
The LineEncoded.txt file's contents are:
-- 0 0 -- 0 -- 0 0 0 0 -- 0 -- 0 -- 0 0 0 0 0 -- 0 -- 0 0 -- 0 0 -- 0 0 -- 0 0 -- 0 0 --
What I am trying to do is decode the file. I first placed the textfile contents into a char array(called line) then used strtok to split the text by space but then I get stuck when putting the if statement block as the output turns out to be wrong as seen below:
00000000000000000000000000000000
0000000000000000
I even tried replacing the while loop with a for loop but I do not know if it really did anything.
The following decoded output should be equal to this:
1010101110001110000001111000101010101010101010101
Any help would be greatly appreciated. Thanks
CodePudding user response:
This statement
pch = strtok (line," ");
does not make a sense because the array line is a two-dimensional array that contains strings. You need to apply the function strtok to each element of the two-dimensional array.
And this if statement
if(pch == '0'){
does not make a sense because there is compared a pointer with the character '0'.
This if statement
else if(pch == "--" || pch == " "){
will always evaluate to logical false because the used string literals have their own extents of memory. So their addresses are not equal to the address stored in pch.
That is the above if statement is equivalent to
else if ( pch == &"--"[0] || pch == &" "[0] ){
Instead you need to use the standard string function strcmp.
Instead of the function clear you could use the standard string function memset. For example
memset( s, 0, sizeof( s ) );
CodePudding user response:
==
isn't defined for comparing strings to each other (or the contents of any array type).
To compare strings against each other, you need to use the strcmp
library function:
if ( strcmp( pch, "0" ) == 0 )
{
// handle case where token is "0"
}
else if ( strcmp( pch, "--" ) == 0 || strcmp( pch, " " ) == 0 )
{
// handle case where token is " " or "--"
}
line
is defined as a 2D array - you need to specify which row of the array you're attempting to tokenize:
pch = strtok( line[i], " " );