| | | Author | Message |
|---|
Beket
Posts: 11 Join date: 2008-05-05
 | Subject: scanf quiz Tue May 06, 2008 6:01 pm | |
| Καλησπέρα παιδιά  Ορίστε ένα μικρό κομμάτι κώδικα. Θα ήθελα τα σχόλια σας πάνω σε αυτό. Δεν δίνω κανένα hint προς το παρόν. | Code: | int a;
do { printf("Input: "); scanf("%d", &a); } while (a != -1);
|
Φιλικά, Στάθης |
|  | | Spoofer Broadcaster

Posts: 14 Join date: 2008-04-29
 | Subject: Re: scanf quiz Wed May 07, 2008 2:30 am | |
| | Beket wrote: | Καλησπέρα παιδιά  Ορίστε ένα μικρό κομμάτι κώδικα. Θα ήθελα τα σχόλια σας πάνω σε αυτό. Δεν δίνω κανένα hint προς το παρόν.
| Code: | int a;
do { printf("Input: "); scanf("%d", &a); } while (a != -1);
|
Φιλικά, Στάθης |
Ok as mh dwsw kapoia apanthsh akoma. Dinw mia trofh gia skepsh omws. Ti tha ginei an eisagoume enan xarakthra anti gia enan akeraio? Ti tha ginei an dwsoume arithmo pera apo ta oria enos int? |
|  | | sug4r
Posts: 2 Join date: 2008-05-08
 | Subject: Re: scanf quiz Thu May 08, 2008 7:32 pm | |
| Beket , δε καταλαβα πιο ειναι το περιεργο σε αυτον τον κωδικα?Περιμενει εναν ακαιρεο σε μια επαναληψη εως οτου δωσεις "-1" οπου και τερματίζει ο βρονχος...δεν καταλαβα τι πρεπει αν δουμε σε αυτον τον κώδικα.Ειναι μια απλη συνταξσ της εντολης while δε βλεπω κατι αλλο. Spoofer, συνηθως οταν δινουμε για εισαγωγη διαφορετικο τυπο δεδομένων απο οτι ειναι η μεταβλητη που εχει ορίσει ο προγραμματιστης, crasharei το συστημα για 'υτο πρεπει να υπαρχει ελενχος σε αυτα τα πραγματα... |
|  | | Spoofer Broadcaster

Posts: 14 Join date: 2008-04-29
 | Subject: Re: scanf quiz Fri May 09, 2008 3:14 pm | |
| sug4r katarxhn kalws hrthes  kai elpizw na vohthhseis kai esy sthn anaptuxh autou tou forum, which imo einai kati diaforetiko kai xrhzei prosoxhs. Twra epi tou thematos: | sug4r wrote: | | Spoofer, συνηθως οταν δινουμε για εισαγωγη διαφορετικο τυπο δεδομένων απο οτι ειναι η μεταβλητη που εχει ορίσει ο προγραμματιστης, crasharei το συστημα για 'υτο πρεπει να υπαρχει ελενχος σε αυτα τα πραγματα... |
To thema einai oti o Beket zhtaei sxolia gia ton kwdika auton. To oti den uparxei elegxos opws polu swsta les einai ena sxolio.. tha borouses na postareis mia veltiwsh autou isws. Epishs pio panw edwsa kai allh mia eisodo pou epishs dhmiourgei provlhma sto programma. Genikotera otidhpote sou erxetai sto mualo sxetika me auto apla pes to. Egw auta katalavainw apo to arxiko post tou Beket. Twra pou thelei na katalhxei o poihths.. tha doume. |
|  | | Beket
Posts: 11 Join date: 2008-05-05
 | Subject: Re: scanf quiz Fri May 09, 2008 8:11 pm | |
| Kat' arxas sygnwmh poy to arxiko post htan grammeno me ellhnikoys xarakthres. Sto ekshs 9a grafw greeklish, an kai den ta protimw. | Quote: | Spoofer, συνηθως οταν δινουμε για εισαγωγη διαφορετικο τυπο δεδομένων απο οτι ειναι η μεταβλητη που εχει ορίσει ο προγραμματιστης, crasharei το συστημα για 'υτο πρεπει να υπαρχει ελενχος σε αυτα τα πραγματα... |
Poly kala Sug4r! H arxh toy mitoy einai h asymfwnia anamesa sta dedomena poy perimenei h scanf() kai sta dedomena poy eisagei o xrhsths. Mh 9ewreis dedomeno oti to apotelesma 9a einai na 'crasharei to systhma'. Sta specs ths C, alla kai sthn man page ths scanf()[1] anaferetai pws h synarthsh epistrefei 0 (mhden), ean symvei "matching failure".
Mexri twra exoyme ta ekshs:
Symperasma 1: Panta elegxoyme ti epistrefei mia synarthsh. Symperasma 2: Perimenoyme ap' to xrhsth na symperifer9ei me ton pio aprovlepto tropo kai merimnoyme gi' ayto.
Aporia: En telei, ti 9a symvei an o xrhsths dwsei ena ena string gia input sto sygkekrimeno code snippet? To dokimasate?
Sto diko moy systhma me gcc 4.3.0 kai glibc 2.7, mpainw se ena atermon loop apo "Input: ". Mporei kaneis na fantastei giati ? Kamia pi9anh lysh ?
Filika, Sta9hs ______________________________________________ [1] Me thn proypo9esh oti h ylopoihsh ths scanf() ths libC soy einai symvath me ta protypa! |
|  | | Beket
Posts: 11 Join date: 2008-05-05
 | Subject: Re: scanf quiz Fri May 09, 2008 8:21 pm | |
| | Quote: | To thema einai oti o Beket zhtaei sxolia gia ton kwdika auton. To oti den uparxei elegxos opws polu swsta les einai ena sxolio.. tha borouses na postareis mia veltiwsh autou isws. Epishs pio panw edwsa kai allh mia eisodo pou epishs dhmiourgei provlhma sto programma.
|
Thanks Spoofer! Einai kai h periptwsh poy o xrhsths eisagei mia timh megalyterh apo ayth poy mporei na anaparasta9ei apo enan integer. |
|  | | Spoofer Broadcaster

Posts: 14 Join date: 2008-04-29
 | Subject: Re: scanf quiz Sat May 10, 2008 7:41 pm | |
| | Beket wrote: | Kat' arxas sygnwmh poy to arxiko post htan grammeno me ellhnikoys xarakthres. Sto ekshs 9a grafw greeklish, an kai den ta protimw.
|
Beket, mporeis na grafeis se o,ti theleis. Auth einai mia apo tis eleftheries tou forum autou :p.
As far as the quiz is concerned. Loipon as dwsw mia idea gia to ena kommati tou provlhmatos, auto tou "an dwsw ena xarakthra" kai ths infinite loop pou prokalei. To provlhma genikotera emfanizetai otan dineis sthn scanf ena input diaforo apo auto pou perimenei. H scanf molis pairnei ena tetoio "bad" input( kai fusika prepei na provlepontai tetoies katastaseis) stamataei kathe diergasia pou eixe skopo na kanei kai apothhkeuei thn eisodo auth sto input stream! H scanf logw ths ulopoihshs ths den diagrafei apo to stream thn eisodo ekeinh pou tis prokalei provlhma kai etsi mpainei se mia atermonh epanalhpsh ("Input: Input:" en prokeimenw) xwris na dinei fusika thn dunatothta ston user na valei kapoio input, afou kathe fora diavazei to xarakthra pou dwsame arxika.
ps: Vevaia mporei h logikh tou na mh diagrafei to bad input na fainetai teleiws xazh alla exei ena vasimo logo. meta apo mia entolh san kai auth: scanf("%d %s",number,string); me eisodo: 31415asdf tote h printf tou number kai tou string tha edinan apotelesmata 31415 kai asdf antistoixa. An omws efarmozotan h logikh tou "diagrafw o,ti den perimenw" skefteite ti tha ginotan... ti times tha eixan oi number kai string? |
|  | | Thiseas
Posts: 5 Join date: 2008-05-02
 | Subject: Re: scanf quiz Sat May 10, 2008 10:53 pm | |
| Otan 8eloume na paroume dedomena apo ton xrhsth kai 8eloume epishs o kwdikas mas na einai (opws leme) prostatevmenos apo xaza inputs tote sxedon panta zhtame char * (kata C/C++) 'H strings (kata C#/Delphi klp). Ayto exei san apotelesma ton eukolotero elegxo apo emas (gia na min analavoun to exception) oi libraries ths glwsas kai sto telos to idio leitoyrgiko. Allos tropos (kai o poio epggelmatikos) einai na xrhsimopoihsoume dikes mas 'H etoimes routines gia input, oi opoies den epitrepoun (sto metro tou dynatou) ston xrhsth na dwsei mh apodeta dedomena. Px. routines gia eisagwgh mono akeraiwn, Hmeromhniwn klp klp Sta windows programmata ayto exei kseperastei mias kai eukola mporeis na kaneis drop apo tin paleta kapoio etoimo control... Se "command prompt" omws den einai to idio. An psakseis bebaia ligo... mporeis na vreis para polla etoima pragmata (functions, Libs etc), mias kai kapote to "command prompt" itane to "kyriws piato"! Twra, oso afora sthn symperifora mias synarthshs (opws edw h scanf) se bad input 8a elega oti 8a prepei na ka8ariseis ton proswrino kataxwrith tou pliktrologioy.... koinws to stdin buffer! Sthn Microsoft C ayto ginetai apla grafontas ton kwdika ws ekshs: int a;
do { flushall(); printf("Input: "); scanf("%d", &a); } while (a != -1); |
|  | | Spoofer Broadcaster

Posts: 14 Join date: 2008-04-29
 | Subject: Re: scanf quiz Sun May 11, 2008 12:30 am | |
| | Thiseas wrote: | Sthn Microsoft C ayto ginetai apla grafontas ton kwdika ws ekshs:
| Code: | int a;
do { [color=red]flushall();[/color] printf("Input: "); scanf("%d", &a); } while (a != -1);
|
|
Thiseas h fflush(stdin) einai undefined, opws epishs parousiazei arketa provlhmata ta opoia einai ikana na crasharoun kai oloklhro to susthma( as mhn mpoume se leptomereies auths ths xrhshs ths fflush). H flushall() prwton ektos tou provlhmatos oti den uparxei kan se kapoious compilers fflush-es ta panta kai katepektash profanws kai to stdin, epomenws einai genikotera undefined ( opws kai ta apotelesmata ths)ws sunarthsh kai kalo tha htan na mh xrhsimopoietai. Polloi empeiroi programmers akolouthoun to standard pista, den nomizw pws einai tuxaio.
ps: Me ligo googling boreite na vreite polla apo ta problhmata ths fflush(stdin) kai gnwmes apo pollous giati tha prepei na thn apofeugete. Gia mena, imho, apla apagoreuetai.
Last edited by Spoofer on Sun May 11, 2008 12:23 pm; edited 1 time in total |
|  | | Thiseas
Posts: 5 Join date: 2008-05-02
 | Subject: Re: scanf quiz Sun May 11, 2008 11:09 am | |
| | Spoofer wrote: | Thiseas h fflush(stdin) einai undefined, opws epishs parousiazei arketa provlhmata ta opoia einai ikana na crasharoun kai oloklhro to susthma( as mhn mpoume se leptomereies auths ths xrhshs ths fflush). |
Gia to udefined... I know file mou Spoofer... gia ayto anaferw apla ka8arismo toy prowsorinou kataxwrhth toy pliktrologioy kai meta anaferw thn ylopoihsh me MS C. H flushall() den ypostirizete apo to Ansi protypo ....
Twra oso afora sto 8ema toy crasharismatos olou toy systhmatos.... logo ths flush? 8a i8ela na anafer8oume se lepromeries (an den exeis antirhsh) mias kai to 8ema einai endiferon.
Ti ennoeis "oloy" toy systhmatos? Poioy systhmatos? Toy idioy toy leitoyrgikoy 'H mono toy programmatos poy trexei...? logw ths flush...?... ypervoliko!! Den to exw psaksei para polu oute omws exw synanthsei tetoia problhmata pote... Mporeis na dikaiologhseis ayto...? An anaderese sta gnwsta ths provlimata edw, 8a elega oti einai ka8ara platform specific. Esy se ti anaferse? Egw anaferomai se Win32.
Epishs, den 8a symfwnhsw oti h fflush, flusarei ta panta... H flushall nai, alla an 8es mporeis kalista na flushareis sygkekrimenous handles... opws toy stdin 'H stdout klp klp: p.x. fflush(stdin).
Ta links, genika, apoteloun kalo 8ema gia problhmatismo, xwris omws na einai kai.... Bible ;-)
Na dieykrinhsw bebaia oti egw anaferomoun sto sygkekrimeno Code Fragment pou edwse o filos, alla genikotera symbainei to ekshs:
H flushall ka8arizei to buffer olwn twn arxeiwn poy anoixtikan gia I/O ka8ws kai olwn pou anoixtikan aytomata kata thn enarksh toy programmatos: stdin, stdout, stderr, stdaux kai stdprn. Ayto to gegonos an den prosextei apo kapoious, pragmati mporei na dimhoyrghsei problems...
Alla epeidh, pragmati, to 8ema pou e8kses einai endiaferon, eimai olos "aytia" , gia na akousw ton antilogo sou.
PS: Please (xwris parekshghsh), mi mou anafereis apla mono links,... oxi oti einai kako, alla oi proswpikes anafores apo proswpikes empeiries boh8ane toso thn koyventa oso kai thn poiothta toy ka8e forum... ;-) thnx |
|  | | Beket
Posts: 11 Join date: 2008-05-05
 | Subject: Re: scanf quiz Sun May 11, 2008 12:18 pm | |
| | Quote: | | H scanf logw ths ulopoihshs ths den diagrafei apo to stream thn eisodo ekeinh pou tis prokalei provlhma kai etsi mpainei se mia atermonh epanalhpsh |
Apsogos spoofer! O pyrhnas toy provlhmatos einai aytos edw akrivws!
| Quote: | | Thiseas h fflush(stdin) einai undefined |
Poly swsta. Etsi gia thn istoria, sthn paragrafo 7.19.5.2 toy spec gia thn ISO C, anaferetai:
| Quote: | If stream points to an output stream or an update stream in which the most recent operation was not input, the fflush function causes any unwritten data for that stream to be delivered to the host environment to be written to the file; otherwise, the behavior is undefined. |
Sto dia tayta: Lysh 1 (Ka9arismos toy input stream apo bad input):
| Code: | do { printf("Input: "); rv = scanf("%d", &a); if (rv == 0) scanf("%[^\n]"); } while (a != -1);
|
Lysh 2 (Syndyasmos fgets/sscanf):
| Code: | char buffer[100];
do { printf("Input: "); fgets(buffer, sizeof buffer, stdin); rv = sscanf(buffer, "%d", &a); } while (rv == 0 || a != -1);
|
Filika, Sta9hs
Last edited by Beket on Sun May 11, 2008 12:32 pm; edited 1 time in total |
|  | | Spoofer Broadcaster

Posts: 14 Join date: 2008-04-29
 | Subject: Re: scanf quiz Sun May 11, 2008 12:21 pm | |
| | Thiseas wrote: | Epishs, den 8a symfwnhsw oti h fflush, flusarei ta panta... H flushall nai, alla an 8es mporeis kalista na flushareis sygkekrimenous handles... opws toy stdin 'H stdout klp klp: p.x. fflush(stdin).
Na dieykrinhsw bebaia oti egw anaferomoun sto sygkekrimeno Code Fragment pou edwse o filos, alla genikotera symbainei to ekshs:
H flushall ka8arizei to buffer olwn twn arxeiwn poy anoixtikan gia I/O ka8ws kai olwn pou anoixtikan aytomata kata thn enarksh toy programmatos: stdin, stdout, stderr, stdaux kai stdprn. Ayto to gegonos an den prosextei apo kapoious, pragmati mporei na dimhoyrghsei problems...
|
Den egrapsa gia thn fflush() alla gia thn flushall().. ekana ena lathos viasunhs kai thn egrapsa ws "fflushall" kai isws gi'auto nomizes oti anaferomoun se auth. Oriste kai to antistoixo shmeio tou post mou:
| Spoofer wrote: | H fflushall() prwton ektos tou provlhmatos oti den uparxei kan se kapoious compilers fflush-es ta panta kai katepektash profanws kai to stdin, epomenws einai genikotera undefined ( opws kai ta apotelesmata ths)ws sunarthsh kai kalo tha htan na mh xrhsimopoietai.
|
Oso gia to crashing tou susthmatos ennow kapoia mikrosusthmata, h opoia pragmatika einai mia akraia periptwsh kai mporei na sumvei se sugekrimena susthmata pou logw ths meiwmenhs mnhmhs pou mporoun na diathesoun ginontai paraxwrhseis ( opws oti files pou einai input-only den uparxei kan stdout buffer).[ Einai mia periptwsh pou isws na mh sumvei h na mhn mporei na sumvei se gnwsta OSs alla thewrw oti pragmata ta opoia mporoun na antikatastathoun isaxia apo synarthseis kai kanones pou sumfwnoun me to standard den tha eprepe na xrhsimopoiountai]
Paradexomai oti den exw proswpikh empeiria olwn twn provlhmatwn ths sunarthshs kai auto dioti mphka ston kopo na diavasw gi' auth kai tis enallaktikes luseis ths prin thn xrhsimopoihsw( isws to kanw kapoia stigmh na testarw kai egw o idios to ti mporei na proklhthei alla gia opoiondhpote allo logo den tha to ekana, den einai anagh na anaklupsw pali ton troxo alla oute kai na akolouthhsw kati pou den einai orismeno). Opws epishs den vriskw to logo na perioristw sto win32 efoson mporw na epektathw parapera. Telos estw kai an se kapoia pragmata douleuei.. den einai endeixh h opoia kanei auth thn sunarthsh reliable, alla oute kai logos na tn xrhsimopoioume. Sthn telikh prepei na sunhthizoume na grafoume swsta gia o,ti grafoume, dioti pote den xereis.. isws tn xrhsimpoihseis kapou pou den prepei apla apo sunhtheia.
Summing up, oi parapanw logoi emena me peithoun na mh xrhsimopoiw to sunduasmo fflush kai stdin kai katepektash thn flushall.
ps: Epeidh to thema xefuge ( pros ofelos olwn mas vevaia) ligaki apo ton arxiko skopo autou tou thread, isws tha borouse na dhmiourghthei ena neo thread milwntas kai me paradeigmata pleon gia ta provlhmata tou fflush(stdin). [na mpw kai egw ston kopo na dokimasw merika.]  |
|  | | Beket
Posts: 11 Join date: 2008-05-05
 | Subject: Re: scanf quiz Sun May 11, 2008 4:02 pm | |
| Thiseas wrote:| Quote: | | Epishs, den 8a symfwnhsw oti h fflush, flusarei ta panta... H flushall nai, alla an 8es mporeis kalista na flushareis sygkekrimenous handles... opws toy stdin 'H stdout klp klp: p.x. fflush(stdin). |
Asfalws k mporeis na kaleseis thn fflush() me opoiodhpote stream san orisma. Sthn periptwsh omws poy den einai reyma eksodoy (dhladh stdout, stderr), tote 9a exeis 'undefined behaviour'. Ayto shmainei pws h symperifora ths libC mporei na kyman9ei apo "epaikse mia xara" mexri "moy esvhse ola ta dedomena sto sklhro disko".
Edw para9etw ena kommati kwdika apo thn ylopoihsh ths fflush() sthn uclibc:
| Code: | #if 0 if (stream && (stream->__modeflags & (__MASK_READING|__FLAG_READONLY))) { /* ANSI/ISO says behavior in this case is undefined but also says you * shouldn't flush a stream you were reading from. As usual, glibc * caters to broken programs and simply ignores this. */ __UNDEFINED_OR_NONPORTABLE; __STDIO_STREAM_SET_ERROR(stream); __set_errno(EBADF); return EOF; } #endif
|
To block ayto einai apenergopoihmeno gia logoys symvatothtas me thn glibc, h opoia opws leei to sxolio endiaferetai gia ta 'broken programmata'. Dhladh gia ekeina poy den akoloy9oyn to protypo k vazoyn thn fflush na drasei panw se reymata eisodoy. Koinws KAI oi dyo ylopoihseis (glibc, uclibc) "to kanoyn gargara".
9a mporoyse na einai energopoihmeno kai na soy epistrefei error 'h na soy anatinazei to pc. Kai ola ayta me tis eylogies toy ANSI/ISO protypoy!
To riskareis? 
Filika, Sta9hs |
|  | | Thiseas
Posts: 5 Join date: 2008-05-02
 | Subject: Re: scanf quiz Sun May 11, 2008 6:04 pm | |
| 8a symfwnhsw toso me ton Spoofer oso kai me ton Beket. 8a prepei na pw oti h empeiria mou sto sygkekrimeno 8ema pou eipa, anaferete kalws 'H kakws  se Win 32. File mou Spoofer, kai bebaia den eipa oti prepei na perioristei kapoios sto Win32... alimomo!! 8a pesei fwtia kai 8a me kapsei....  .... apla anafera ayto dioti se ayto exw empeireia gia to sygkekrimeno 8ema. Twra, gia to an 8a thn xrhsimopoiusa thn flush 'H oxi.... xm... Nai 8a elega. 8a thn xrhsimopoiousa efoswn mou ekane thn "douleia mou" xwris side efffects. 8a mou peite.... ma pws? Try & Error.... as usual. To kserw oti mporei na min symfwnoun poloi me ayto to skeptiko,... alla ok, einai opws exei ma8ei o ka8enas mas. Ena akoma 8ema poly endiaferon poy 8ixtike apo ton Beket einai to ekshs: 8a prepei PANTA na akolou8oume ta ANSI protypa?.... 8a i8ela na pw ki edww.... ououou... alla opws swsta eipate to 8ema paei ta ksefygei.... isws anoiksoume ki alla threads... na ta legame pio clear... in any case nice talk... |
|  | | Spoofer Broadcaster

Posts: 14 Join date: 2008-04-29
 | |  | | |
| Page 1 of 1 |
| | Permissions of this forum: | You cannot reply to topics in this forum
| |
| |
| |