The_Kingpin08
asked on
How can I handle a special character, using a if ?
Hi all,
I recently discovered that my program kept bugging because the file he reads contain a special character. I'd like to bypass only this character since I know there aren't any other like this. I tried using:
int parseLine(bookInfo *out, const char *str, int noLine)
int i;
for(i=0; str[i] != '\t' && str[i] != '\n' && i < 40;i++)
if(str[i]="Ë")
{out->author[i]="E";}
...
but it don't work. Any help would be appreciated. thanks
BTW i copied my function quickly, the problem really is into the special character.
Frank
I recently discovered that my program kept bugging because the file he reads contain a special character. I'd like to bypass only this character since I know there aren't any other like this. I tried using:
int parseLine(bookInfo *out, const char *str, int noLine)
int i;
for(i=0; str[i] != '\t' && str[i] != '\n' && i < 40;i++)
if(str[i]="Ë")
{out->author[i]="E";}
...
but it don't work. Any help would be appreciated. thanks
BTW i copied my function quickly, the problem really is into the special character.
Frank
Sorry, should be:
char *CleanLine (char *Buffer)
{
char *Temp;
char *Source;
char *Dest;
Temp = (char *)malloc (strlen (Buffer) + 1);
Source = Buffer;
Dest = Temp;
while (*Source)
if (*Source >= 0x20 && *Source < 0x80)
*(Dest++) = *(Source++);
else
Source++;
*Dest = 0;
return (Temp);
}
char *CleanLine (char *Buffer)
{
char *Temp;
char *Source;
char *Dest;
Temp = (char *)malloc (strlen (Buffer) + 1);
Source = Buffer;
Dest = Temp;
while (*Source)
if (*Source >= 0x20 && *Source < 0x80)
*(Dest++) = *(Source++);
else
Source++;
*Dest = 0;
return (Temp);
}
Hi Frank,
> I'd like to bypass only this character since I know there aren't any other like this.
what do you mean by bypass? Would you like to have this character replaced by some other character at all instances? Or is it that you want to process input char by char and skip processsing this char?
If your approach is latter, then it might be niteresting to see some more code ... You are using comparison of ASCII value to be <40 ... are you sure it is not the end of string character '\0' or just 0 which is causing you the pain
As a small hint, former can be achieved by strchr in a loop
temp2 = string;
temp1 = strchr (temp2, '<bad_char>' );
while ( temp1 != NULL )
{
*temp1 = '<good_char>';
temp2 = temp1 +1;
temp1 = strchr (temp2, '<bad_char>');
}
Make sure that your bad_char is not end-of-string or the code may segfault
For the latter approach, you can depend on && check ... Though it is not guaranteed to work on every C compiler but it should work on almost any C compiler currently in use
if ( *p != '<bad_char>' && <furthur processing>)
condition after && is generally executed only if first condition is true
Few more details about the problem and desired solution will be very useful for further discussion.
cheers
sunnycoder
> I'd like to bypass only this character since I know there aren't any other like this.
what do you mean by bypass? Would you like to have this character replaced by some other character at all instances? Or is it that you want to process input char by char and skip processsing this char?
If your approach is latter, then it might be niteresting to see some more code ... You are using comparison of ASCII value to be <40 ... are you sure it is not the end of string character '\0' or just 0 which is causing you the pain
As a small hint, former can be achieved by strchr in a loop
temp2 = string;
temp1 = strchr (temp2, '<bad_char>' );
while ( temp1 != NULL )
{
*temp1 = '<good_char>';
temp2 = temp1 +1;
temp1 = strchr (temp2, '<bad_char>');
}
Make sure that your bad_char is not end-of-string or the code may segfault
For the latter approach, you can depend on && check ... Though it is not guaranteed to work on every C compiler but it should work on almost any C compiler currently in use
if ( *p != '<bad_char>' && <furthur processing>)
condition after && is generally executed only if first condition is true
Few more details about the problem and desired solution will be very useful for further discussion.
cheers
sunnycoder
Everyone went overboard on hypotheticals, but nobody pointed
out you trivial typographical error:
if(str[i]="Ë")
should be
if(str[i] == "Ë")
(note == for comparison of equality)
out you trivial typographical error:
if(str[i]="Ë")
should be
if(str[i] == "Ë")
(note == for comparison of equality)
you are storing a string in str[].
Now look at your code
for(i=0; str[i] != '\t' && str[i] != '\n' && i < 40;i++)
if(str[i]="Ë") <-- "Ë": this will return a pointer and assign to str[i], it will be truncated to a char and str[i] will
eventually contain some weird character.
{out->author[i]="E";} <-- so will this.
How do u know there is a special character in your file?in case there is please modify above as follows.
for(i=0; str[i] != '\t' && str[i] != '\n' && i < 40;i++)
if(str[i]== 'Ë')
{out->author[i]='E';}
hope this helps.
van_dy
Now look at your code
for(i=0; str[i] != '\t' && str[i] != '\n' && i < 40;i++)
if(str[i]="Ë") <-- "Ë": this will return a pointer and assign to str[i], it will be truncated to a char and str[i] will
eventually contain some weird character.
{out->author[i]="E";} <-- so will this.
How do u know there is a special character in your file?in case there is please modify above as follows.
for(i=0; str[i] != '\t' && str[i] != '\n' && i < 40;i++)
if(str[i]== 'Ë')
{out->author[i]='E';}
hope this helps.
van_dy
Hi The_Kingpin08,
Use the isprint() function. It returns 0 for special characters, 1 otherwise:
#include <ctype.h>
[...]
for(i=0; str[i] != '\t' && str[i] != '\n' && i < 40;i++)
if(!isprint(str[i]))
out->author[i]="?";
Another alternative is a translation array, like:
char tr[256]={ '.', /* \0 */
[...]
'A', /* Ä */
'E', /* Ë */
[...]
};
and then
for(i=0; str[i] != '\t' && str[i] != '\n' && i < 40;i++)
out->author[i]=tr[(unsigne d char)str[i]];
...just be careful that you need to cast your (signed) char to unsigned before using it on the array, otherwise you'll get negative indices.
Cheers!
Stefan
Use the isprint() function. It returns 0 for special characters, 1 otherwise:
#include <ctype.h>
[...]
for(i=0; str[i] != '\t' && str[i] != '\n' && i < 40;i++)
if(!isprint(str[i]))
out->author[i]="?";
Another alternative is a translation array, like:
char tr[256]={ '.', /* \0 */
[...]
'A', /* Ä */
'E', /* Ë */
[...]
};
and then
for(i=0; str[i] != '\t' && str[i] != '\n' && i < 40;i++)
out->author[i]=tr[(unsigne
...just be careful that you need to cast your (signed) char to unsigned before using it on the array, otherwise you'll get negative indices.
Cheers!
Stefan
> Everyone went overboard on hypotheticals, but nobody pointed
> out you trivial typographical error:
>
> if(str[i]="Ë")
>
> should be
>
> if(str[i] == "Ë")
>
> (note == for comparison of equality)
Correct. But even Brett overlooked this one - it should be
if(str[i] == 'E') /* single quote, not double */
Regards
> out you trivial typographical error:
>
> if(str[i]="Ë")
>
> should be
>
> if(str[i] == "Ë")
>
> (note == for comparison of equality)
Correct. But even Brett overlooked this one - it should be
if(str[i] == 'E') /* single quote, not double */
Regards
I think you just need something like this:
int i;
for(i=0; str[i] && str[i] != '\t' && str[i] != '\n' && i < 40;i++) {
if(str[i]=="Ë")
out->author[i] = 'E';
else
out->author[i] = str[i];
}
out->author[i] = 0; /* needs this ending null character */
Good luck,
Jaime.
int i;
for(i=0; str[i] && str[i] != '\t' && str[i] != '\n' && i < 40;i++) {
if(str[i]=="Ë")
out->author[i] = 'E';
else
out->author[i] = str[i];
}
out->author[i] = 0; /* needs this ending null character */
Good luck,
Jaime.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>if the compiler is not working in an environment that does not support this character ?
Sorry , working in an environment that does not support this character
Sorry , working in an environment that does not support this character
Frank,
Avik's accepted comment is just a typo correction of my solution, so I think more than one deserve points in this question.
If you don't know how to split points we can help you.
BR,
Jaime.
Avik's accepted comment is just a typo correction of my solution, so I think more than one deserve points in this question.
If you don't know how to split points we can help you.
BR,
Jaime.
I'm a bit confused by your description.
Your data file contains a special character that's outside the "normal" characterset and this character is causing a problem.
You could just copy the string to another buffer and skip all of the special characters.
char *CleanLine (char *Buffer)
{
char *Temp;
char *Source;
char *Dest;
Temp = (char *)malloc (strlen (Buffer) + 1);
Source = Buffer;
Dest = Temp;
while (*Source)
if (*Source >= 0x20 && *Source < 0x80)
*(Dest++) = *(Dest++);
else
Source++;
*Dest = 0;
return (Temp);
}
Kent