Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!ucbvax!TAURUS.BITNET!asaph From: asaph@TAURUS.BITNET Newsgroups: comp.sys.amiga Subject: Re: C scanf question Message-ID: <1059@taurus.BITNET> Date: 25 Jul 89 21:41:39 GMT References: <20201@louie.udel.EDU> <455@tardis.Tymnet.COM> Sender: daemon@ucbvax.BERKELEY.EDU Reply-To: asaph%math.tau.ac.il@CUNYVM.CUNY.EDU (Zemach Asaph) Organization: Tel-Aviv Univesity Math and CS school, Israel Lines: 34 In article <455@tardis.Tymnet.COM> jms@tardis.Tymnet.COM (Joe Smith) writes: >If you have "char line[80]" and use fscanf(f,"%s",line), what happens if >the input has more than 80 consecutive characters without a tab, blank >or newline? >The only safe way to use the scanf family on untrustworthy data is to >use sscanf on a string read by fgets or equivalent. > >Joe Smith (408)922-6220 | SMTP: JMS@F74.TYMNET.COM or jms@tymix.tymnet.com according to the manx 3.2 book you can limit the amount of chars read from a file via scanf by using a line like this char buffer[50] : : fscanf (file,"%50s",buffer) this should read no more then 50 chars including the null terminator. for a more general read you could do char buffer[BUFFSIZE] fscanf (file,"%*s",BUFFSIZE,buffer) <-- I'm not sure about this one though! If it doesn't work you might have to create the string "%" + num + "s", using something like sprintf, for instance. (Talk about overkill !). BTW, the the %+num+char is good for all types %2d will read a decimal number of no more then 2 chars. As for the parsing usefullness of scanf I have this to contribute: We where just recently given a project to do here at TAU, a sort of assembler program, needles to say some line parsing is nessaccery there, I know that some of my friend used sscanf extensively for this. after all the ability to say %[ ~abcd] is pretty nice. I actualy wrote a small pattern matcher but thats another story altogether. asaph asaph@taurus.bitnet - or - asaph@math.tau.ac.il