[SOLVED] Removing junk characters through C program
Linux - SoftwareThis forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hi,
I am trying to remove junk characters from a file through C program. But code is not working in desired way.No output is coming int the output_file.
Here is the code.
Junk characters in junk_file i have entered by typing numeric values >255
(1) char and int can be freely mixed. BUT if you mean the numeric values 0 and 255, then you should remove quotes around them.
(2) The so called junk characters are those < 32 or those > 127.
OK
while((ch=getc(fp))!=EOF)
{
if(ch < '0' || ch > '255') /*unget all the characters beyond the ascii bounds */
ungetc(ch,fp);
else
putc(ch,fp1); /*get rest of the characters in the output file */
}
fclose(fp);
fclose(fp1);
return(0);
}
First in your original program why you are using ungetc(int, FILE *) function. I have run and tested it, it is creating an infinite loop. It never terminates possibly because it first get character then unget it and situation repeats itself.
Also compiling your original code gives this warning. number.c: In function ‘main’:
number.c:12:21: warning: multi-character character constant [-Wmultichar]
if(ch < '0' || ch > '255') /*unget all the characters beyond the ascii bounds */
So I am agreed with AnanthaP that you have to remove quotes. Write 255 not '255'.
Agree with AnanthaP for replacing the code.
Last edited by s.verma; 03-08-2014 at 02:36 AM.
Reason: Ref. recent post of AnanthaP.
Instead of using "printf" (which is slow) why not use fputc?
And instead of all the "ifs" used, why not a switch?
Code:
...
while (EOF != (c = fgetc(stream))) {
switch (c) {
case 10:
case 32...58:
case 60...126:
fputc(c, stdout);
break;
case 59:
fputc('\n',stdout);
break;
default:
fputc(' ',stdout);
}
}
And watch out for those "ch >255" tests -they are invalid.
ch is declared a "char", and that means it is an 8 bit signed value. There is no signed character that is 255... signed characters go from -127 to 128, so your test of "ch > 255" is never true.
Using "c" (from the example preceding mine) is declared an integer - which allows for testing for EOF (value is -1), and all values returned are then between 0 and 255...
yes u r very right pollard.
Problem with my code was ,after removing the junk characters from original file i was not able to preserve their empty space in the output file.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.