Quote:
Originally Posted by xpi0t0s View Post
Heh, well, I guess I shouldn't be too harsh because "full detail" and "reasonably short" are slightly contradictory if you're struggling with basic comprehension skills, which is clearly the case here. You'll never make it as a programmer if you can't read a spec and generate appropriate output, so probably your best bet is to work on that first, then come back to programming once you can read and understand.

Anyway, here's an example program that reads a string and assigns values to parts of a structure. Built and tested in Visual Studio 2005 but there's nothing Windows-specific in the code.

Code:
void go4e_15987()
{
    char *ex_str="14,hello,23.7,23/12/1987";
    struct dt { int dd,mm,yy; };
    struct tag_foo { int num; char greet[10]; double f; struct dt bar; } foo;
    int stat=0,offset=0;
    double deci=0.1;
    memset(&foo,0,sizeof(foo));
    for (int i=0; ex_str[i]; i++)
    {
        char c=ex_str[i];
        switch (stat)
        {
        case 0: if (c==',') stat++; else foo.num=foo.num*10+c-'0'; break;
        case 1: if (c==',') { offset=0; stat++; } else foo.greet[offset++]=c; break;
        case 2: if (c=='.') stat++; else foo.f=foo.f*10.0+double(c-'0'); break;
        case 3: if (c==',') stat++; else { foo.f+=deci*double(c-'0'); deci/=10; } break;
        case 4: if (c=='/') stat++; else foo.bar.dd=foo.bar.dd*10+c-'0'; break;
        case 5: if (c=='/') stat++; else foo.bar.mm=foo.bar.mm*10+c-'0'; break;
        case 6: foo.bar.yy=foo.bar.yy*10+c-'0'; break;
        }
    }
    printf("%s\n%d '%s' %e %d/%d/%d\n",ex_str,foo.num,foo.greet,foo.f,foo.bar.dd,foo.bar.mm,foo.bar.yy);
}
And here's what I mean by "full detail" and "reasonably short": The program reads a comma delimited string and assigns variables to the foo struct, which contains an integer, a greeting, and double and a date. The date is broken down into three ints.

The example string is "14,hello,23.7,23/12/1987", and the structure details are as follows:
Code:
struct dt { int dd,mm,yy; };
struct tag_foo { int num; char greet[10]; double f; struct dt bar; } foo;
Hi,
i don't think u understood my question fully. the details i provided may be short and minimum because i thought it is self explaining.

My question is how to treat a string read from a file as a structure variable ie; in ur example code consider num, i will read from file as "foo.num" and this should correspond to exact "int num" variable of ur structure.

there is no problem in assigning values.

i send u the entire header file to show how the structures are nested and how complex a switch statement will be when treated as a solution to this since u suggested it in ur first reply.

thanx for ur advice but for ur kind notice i can judge myself better than anybody and i know what i am