Tema: Re: Reikia helpo su C stringais
Autorius: Kestas
Data: 2010-11-04 11:24:07
Grazinti pointeri i lokalu f-jos kintamaji  yra negerai. Reikia arba f-jai 
Time2Txt "is virsaus" paduoti pointeri kur ji turi padeti rezultata arba 
atminti kintamajam 'a' isskirineti su new (arba malloc).


"Tomis"  wrote in message news:op.vlm108eixe6kaj@tomasg.jurby.com...

Padekit zaliam. Uzsiknisau visai su tais C stringais. Pripratau mat prie
visokiu paskaliu, php ir beisuku kur su stringais gan paprasta dirbt.
Gerai butu jei kas nors paprastai ukiskai paaiskintu kaip su tais
stringais dirbt. Kada naudot char tekstas[34];  o kada char * tekstas. Ar
yra skirtumas kurioj vietoj ta zvaigzdute? nes pvz matau ivairiai
prideliota: uz char be tarpo, tarp char ir kintamojo, pries kintamojo
pavadinima be tarpo...

O siaip dabar turiu tokia problemele

_main iskvieciamas kas sekunde. Pirma karta iskviestas rezultata gaunu
visokiu kringelius, sekancius kartus viskas ok. Itariu kazka neteisingau
su stringais darau.

Cia kodas:
-------------------------------------
  char* _main(char* lpszPictureName, char* lpszObjectName, char*
lpszPropertyName)
{
#define Status "F022.Status"
#define Act "F022.TimeAct"
#define Def "F022.TimeDef"
char txt[100]="";
char d[100]="";
char a[100]="";
TSandFilter F;
        printf("#########_main started\r\n");
F.raw=GetTagDWord(Status);
strcpy(d,Time2Txt(GetTagDWord(Def)));
strcpy(a,Time2Txt(GetTagDWord(Act)));
       printf("_main: result a:%s\r\n",a);
       printf("_main: result d:%s\r\n",d);
sprintf(txt,"Stadija: %s\r\nUzduota: %s\r\nPraejo:
%s",SandFilterStages[F.bits._StageNr],d,a);
        printf("_main: result txt:%s\r\n",txt);
        printf("##########_main end\r\n");
return txt;
}
------------------------------
char *Time2Txt(long TimeS)
{
long time;
short days, hours, minutes, seconds;
char a[150];
char d[9];
char h[9];
char m[9];
char s[9];
unsigned char Q;
time=TimeS;
        printf("Time2Txt TimeS: %u\r\n",TimeS);
        printf("Time2Txt time: %u\r\n",time);
days=time/86400;
time=time-(days*86400);
hours=time/3600;
time=time-(hours*3600);
minutes=time/60;
seconds=time-(minutes*60);
sprintf(d, "%02dd ",days);
sprintf(h, "%02dh ",hours);
sprintf(m, "%02dm ",minutes);
sprintf(s, "%02ds",seconds);
       printf("Time2Txt formated: %s%s%s%s \r\n",d,h,m,s);
strcpy(a,"");
if (days>0) strcat(a,d);
if (hours>0) strcat(a,h);
if (minutes>0) strcat(a,m);
if (seconds>0) strcat(a,s);
if (TimeS==0) strcpy(a,"0ñ");
       printf("Time2Txt result: %s \r\n",a);
return a;
}
------------------------
Printf Outputas:
------------------
#########_main started
Time2Txt TimeS: 86400
Time2Txt time: 86400
Time2Txt formated: 01d 00h 00m 00s
Time2Txt result: 01d
Time2Txt TimeS: 69502
Time2Txt time: 69502
Time2Txt formated: 00d 19h 18m 22s
Time2Txt result: 19h 18m 22s
_main: result a:ˆ÷GAÌx°÷G (:     <------------Va cia tie kringeliai
_main: result d:ˆ÷GAÌx°÷G (:
_main: result txt:Stadija: Darbas
Uzduota: ˆ÷GAÌx°÷G (:
Praejo: ˆ÷GAÌx°÷G (:
##########_main end
#########_main started
Time2Txt TimeS: 86400
Time2Txt time: 86400
Time2Txt formated: 01d 00h 00m 00s
Time2Txt result: 01d
Time2Txt TimeS: 69503
Time2Txt time: 69503
Time2Txt formated: 00d 19h 18m 23s
Time2Txt result: 19h 18m 23s
_main: result a:19h 18m 23s
_main: result d:01d
_main: result txt:Stadija: Darbas
Uzduota: 01d
Praejo: 19h 18m 23s
##########_main end
#########_main started
Time2Txt TimeS: 86400
Time2Txt time: 86400
Time2Txt formated: 01d 00h 00m 00s
Time2Txt result: 01d
Time2Txt TimeS: 69504
Time2Txt time: 69504
Time2Txt formated: 00d 19h 18m 24s
Time2Txt result: 19h 18m 24s
_main: result a:19h 18m 24s
_main: result d:01d
_main: result txt:Stadija: Darbas
Uzduota: 01d
Praejo: 19h 18m 24s
##########_main end