Forum Replies Created
Viewing 5 posts - 1 through 5 (of 5 total)
- AuthorPosts
- Priyansh AgrawalParticipant
Ok, I found the problem/solution but I’m not satisfied: All works fine when the optimizations in gcc are turned off.
Each optimization (O, O1, O2, O3, Os) will cause an endless loop.Here an example:
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556#include <br />#include <br /><br />int wait1,wait2;<br /><br />void *mythread(void *ptr)<br />{<br />int *parm;<br />int i;<br /><br />parm=(int *)ptr;<br /><br />for (i=0;i<10;i++)<br />{<br />// branch for thread no 1<br />if (parm[0])<br />{<br />printf("nLoop %in",i);<br />printf("Thread 1 started and waiting for thread 2 ...n");<br />wait2=0;<br />while (wait1);<br />wait1=1;<br /><br />printf("Thread 1 resumed.n");<br />}<br />// branch for thread no 2<br />else<br />{<br />while (wait2);<br />wait2=1;<br />printf("Thread 2 started and telling Thread 1 to resume ...n");<br />wait1=0;<br />}<br />}<br /><br />return NULL;<br />}<br /><br />int main()<br />{<br />pthread_t thr1,thr2;<br />int parm1[1],parm2[1];<br /><br />wait1=1;<br />wait2=1;<br />parm1[0]=1;<br />parm2[0]=0;<br /><br />pthread_create(&thr1,NULL,mythread,(void *)parm1);<br />pthread_create(&thr2,NULL,mythread,(void *)parm2);<br /><br />pthread_join(thr1,NULL);<br />pthread_join(thr2,NULL);<br /><br />return 0;<br />}Priyansh AgrawalParticipantThis code could do this from base 2-10 to base 2-10. There is a nice function called itoa that converts int to char* and you can specify the base. Unfortunatelly the function atoi has no base-parameter. So I recoded it:
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152#include <stdio.h><br />#include <stdlib.h><br />int xy(int x,int y)<br />{<br />int r=1,i;<br /><br />for (i=0;i<y;i++)<br />{<br />r*=x;<br />}<br /><br />return r;<br />}<br /><br />int my_atoi(char *s,int base)<br />{<br />int r=0,i,j,pos=0;<br /><br />for (j=0;j<80 && s[j]!=0 && s[j]>='0' && s[j]<='9';j++);<br /><br />for (i=j-1;i>=0;i--)<br />{<br />r+=(s-'0')*xy(base,pos);<br />pos++;<br />}<br /><br />return r;<br />}<br /><br />int main()<br />{<br />char buff[83];<br />int num,b1,b2;<br /><br />printf("from base:");<br />fgets(buff,80,stdin);<br />b1=atoi(buff);<br /><br />printf(" to base:");<br />fgets(buff,80,stdin);<br />b2=atoi(buff);<br /><br />printf("Number:");<br />fgets(buff,80,stdin);<br />num=my_atoi(buff,b1);<br /><br />itoa(num,buff,b2);<br /><br />printf("nresult is %sn",buff);<br /><br />return 0;<br />}Priyansh AgrawalParticipantSorry, 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 />Priyansh AgrawalParticipantWell, that’s C++-code. In C this would look like that:
1234567891011121314151617181920212223242526272829303132333435363738394041424344#include <br /><br />int main(int argc,char **argv)<br />{<br />int r=0,sz;<br />FILE *f1,*f2;<br />char buff[4096];<br /><br />if (argc!=3)<br />{<br />r=1;<br />printf("usage: cop n");<br />}<br />else<br />{<br />if ((f1=fopen(argv[1],"rb"))==NULL)<br />{<br />r=2;<br />printf("cannot open input-file!n");<br />}<br />else<br />{<br />if ((f2=fopen(argv[2],"wb"))==NULL)<br />{<br />r=3;<br />printf("cannot open output-file!n");<br />}<br />else<br />{<br />while (!feof(f1))<br />{<br />sz=fread(buff,1,4096,f1);<br />fwrite(buff,sz,1,f2);<br />}<br /><br />fclose(f2);<br />}<br /><br />fclose(f1);<br />}<br />}<br /><br />return r;<br />}Priyansh AgrawalParticipantCount each line, voila: (you can pass the filename/path as the first argument)
12345678910111213141516171819202122232425262728293031323334353637383940#include <stdio.h><br /><br />int main(int argc,char **argv)<br />{<br />const int max=1024;<br />int r=0,count=0,i;<br />FILE *f;<br />char buff[max+3];<br /><br />if (argc==2)<br />{<br />if ((f=fopen(argv[1],"rb"))==NULL)<br />{<br />r=2;<br />printf("cannot open file!n");<br />}<br />else<br />{<br />while (!feof(f))<br />{<br />buff[0]=0;<br />fgets(buff,max,f);<br />count++;<br /><br />for (i=0;i<max && buff!=0 && buff!='r' && buff!='n';i++);<br></max><br />printf("Line %i, count %in",count,i);<br />}<br /><br />fclose(f);<br />}<br />}<br />else<br />{<br />r=1;<br />printf("usage: count <file>n");<br />}<br /><br />return r;<br />}</file> - AuthorPosts
Viewing 5 posts - 1 through 5 (of 5 total)