Home › Forums › C Programming › Regarding replacing line of code??
- This topic has 1 reply, 2 voices, and was last updated 16 years, 8 months ago by Priyansh Agrawal.
Viewing 1 reply thread
- AuthorPosts
- April 21, 2008 at 9:42 pm #2096asfaltinabelleParticipant
Dear All,
My task is:- i have one file i have to identify the preprocessor directives and replace its equavalent lower case if already they are defined with upper case..i have wrote following code..but it is not working could you help me out to solve this?
123456789101112131415161718192021222324252627282930313233343536373839<br />#include <string.h><br />#include <ctype.h><br />int main(void)<br />{<br />// open a text file for reading<br />FILE * in_file;<br />in_file = fopen("C:\Documents and Settings\syg4kor\Desktop\Perl_Programming\CAN.c","r");<br />if ( ! in_file )<br />{<br />printf ( "file did not openn" );<br />return 1;<br />}<br />int numberOfCharacters = 0;<br />char ch = fgetc ( in_file );<br />int noc=0;<br />char *ptr;<br />while ( ch != EOF )<br />{<br />noc++;<br />if(ch=='#')<br />{ <br />ch++;<br />while(ch!='n')<br />{<br />*ptr=ch;<br />ptr++;<br />ch++;<br />}<br />tolower(*ptr);<br />}<br />ch = fgetc ( in_file );<br /><br />}<br /><br />fclose ( in_file );<br />return 0;<br />}<br /> - April 28, 2008 at 10:14 am #3385Priyansh AgrawalParticipant
Sorry, your code looks very strange to me. Where do you want to place the output-file ? You opened an input-file for read-only. There you can’t make any changes.
Perhaps this should help you:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960<br />#include <br /><br />int main(int argc,char **argv)<br />{<br />const int max=1024;<br />int r=0,i;<br />FILE *in,*out;<br />char buff[max+3];<br /><br />if (argc!=3)<br />{<br />r=1;<br />printf("usage: lower n");<br />}<br />else<br />{<br />if ((in=fopen(argv[1],"rb"))==NULL)<br />{<br />r=2;<br />printf("cannot open input-file!n");<br />}<br />else<br />{<br />// Output file will be overwritten when exists !!!<br />if ((out=fopen(argv[2],"wb"))==NULL)<br />{<br />r=3;<br />printf("cannot open output-file!n");<br />}<br />else<br />{<br />while (!feof(in))<br />{<br />buff[0]=0;<br />fgets(buff,max,in);<br /><br />for (i=0;i<max && buff!=0 && buff!='r' && buff!='n' && (buff==' ' || buff=='t');i++);<br /><br />if (buff=='#')<br />{<br />for (i=i+1;i<max && buff!=0 && buff!='r' && buff!='n';i++)<br />{<br />buff=tolower(buff);<br />}<br />}<br /><br />fputs(buff,out);<br />}<br /><br />fclose(out);<br />}<br /><br />fclose(in);<br />}<br />}<br /><br />return r;<br />}<br />
- AuthorPosts
Viewing 1 reply thread
- The forum ‘C Programming’ is closed to new topics and replies.