Forum Replies Created

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • in reply to: threads Win32 <-> Linux #3387
    Priyansh Agrawal
    Participant

    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:

    #include 
    #include

    int wait1,wait2;

    void *mythread(void *ptr)
    {
    int *parm;
    int i;

    parm=(int *)ptr;

    for (i=0;i<10;i++)
    {
    // branch for thread no 1
    if (parm[0])
    {
    printf("nLoop %in",i);
    printf("Thread 1 started and waiting for thread 2 ...n");
    wait2=0;
    while (wait1);
    wait1=1;

    printf("Thread 1 resumed.n");
    }
    // branch for thread no 2
    else
    {
    while (wait2);
    wait2=1;
    printf("Thread 2 started and telling Thread 1 to resume ...n");
    wait1=0;
    }
    }

    return NULL;
    }

    int main()
    {
    pthread_t thr1,thr2;
    int parm1[1],parm2[1];

    wait1=1;
    wait2=1;
    parm1[0]=1;
    parm2[0]=0;

    pthread_create(&thr1,NULL,mythread,(void *)parm1);
    pthread_create(&thr2,NULL,mythread,(void *)parm2);

    pthread_join(thr1,NULL);
    pthread_join(thr2,NULL);

    return 0;
    }

    in reply to: base conversion #3265
    Priyansh Agrawal
    Participant

    This 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:

    #include 
    #include
    int xy(int x,int y)
    {
    int r=1,i;

    for (i=0;i {
        r*=x;
    }

    return r;
    }

    int my_atoi(char *s,int base)
    {
    int r=0,i,j,pos=0;

    for (j=0;j<80 && s[j]!=0 && s[j]>='0' && s[j]<='9';j++);

    for (i=j-1;i>=0;i--)
    {
        r+=(s-'0')*xy(base,pos);
        pos++;
    }

    return r;
    }

    int main()
    {
    char buff[83];
    int num,b1,b2;

    printf("from base:");
    fgets(buff,80,stdin);
    b1=atoi(buff);

    printf(" to base:");
    fgets(buff,80,stdin);
    b2=atoi(buff);

    printf("Number:");
    fgets(buff,80,stdin);
    num=my_atoi(buff,b1);

    itoa(num,buff,b2);

    printf("nresult is %sn",buff);

    return 0;
    }
    in reply to: Regarding replacing line of code?? #3385
    Priyansh Agrawal
    Participant

    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:


    #include

    int main(int argc,char **argv)
    {
    const int max=1024;
    int r=0,i;
    FILE *in,*out;
    char buff[max+3];

    if (argc!=3)
    {
    r=1;
    printf("usage: lower n");
    }
    else
    {
    if ((in=fopen(argv[1],"rb"))==NULL)
    {
    r=2;
    printf("cannot open input-file!n");
    }
    else
    {
    // Output file will be overwritten when exists !!!
    if ((out=fopen(argv[2],"wb"))==NULL)
    {
    r=3;
    printf("cannot open output-file!n");
    }
    else
    {
    while (!feof(in))
    {
    buff[0]=0;
    fgets(buff,max,in);

    for (i=0;i<max && buff!=0 && buff!='r' && buff!='n' && (buff==' ' || buff=='t');i++);

    if (buff=='#')
    {
    for (i=i+1;i<max && buff!=0 && buff!='r' && buff!='n';i++)
    {
    buff=tolower(buff);
    }
    }

    fputs(buff,out);
    }

    fclose(out);
    }

    fclose(in);
    }
    }

    return r;
    }
    in reply to: Copy/Read/Write a binary file(.mp3) #3384
    Priyansh Agrawal
    Participant

    Well, that’s C++-code. In C this would look like that:

    #include 

    int main(int argc,char **argv)
    {
    int r=0,sz;
    FILE *f1,*f2;
    char buff[4096];

    if (argc!=3)
    {
    r=1;
    printf("usage: cop n");
    }
    else
    {
    if ((f1=fopen(argv[1],"rb"))==NULL)
    {
    r=2;
    printf("cannot open input-file!n");
    }
    else
    {
    if ((f2=fopen(argv[2],"wb"))==NULL)
    {
    r=3;
    printf("cannot open output-file!n");
    }
    else
    {
    while (!feof(f1))
    {
    sz=fread(buff,1,4096,f1);
    fwrite(buff,sz,1,f2);
    }

    fclose(f2);
    }

    fclose(f1);
    }
    }

    return r;
    }
    in reply to: How to count characters from text file #3374
    Priyansh Agrawal
    Participant

    Count each line, voila: (you can pass the filename/path as the first argument)

    #include 

    int main(int argc,char **argv)
    {
    const int max=1024;
    int r=0,count=0,i;
    FILE *f;
    char buff[max+3];

    if (argc==2)
    {
    if ((f=fopen(argv[1],"rb"))==NULL)
    {
    r=2;
    printf("cannot open file!n");
    }
    else
    {
    while (!feof(f))
    {
    buff[0]=0;
    fgets(buff,max,f);
    count++;

    for (i=0;i
    printf("Line %i, count %in",count,i);
    }

    fclose(f);
    }
    }
    else
    {
    r=1;
    printf("usage: count n");
    }

    return r;
    }
Viewing 5 posts - 1 through 5 (of 5 total)