ville Posted May 24, 2008 at 02:39 PM Report Share #187144 Posted May 24, 2008 at 02:39 PM tenho que limitar o numero de strings que entram, para o programa n estourar. scanf("%50s",string); esta foi a solução que arranjei. Porém eu quero isto de uma maneira mais flexivel. O que eu queria era algo do genero scanf("%TAMSTRINGs",string); (eu sei que isto n funciona, era só para perceberem o que eu quero.) Link to comment Share on other sites More sharing options...
anolsi Posted May 24, 2008 at 02:48 PM Report Share #187147 Posted May 24, 2008 at 02:48 PM Bem uma maneira mais lenta, mas que resolve: char texto[TAMSTRINGs]; for (i=0;i<TAMSTRINGs;i++) scanf("%c", texto[i]); texto[TAMSTRINGs] = '\0' "Nós somos o que fazemos repetidamente, a excelência não é um feito, e sim, um hábito."Não respondo a questões por PM que possam ser colocadas no fórum! Link to comment Share on other sites More sharing options...
ville Posted May 24, 2008 at 03:52 PM Author Report Share #187175 Posted May 24, 2008 at 03:52 PM mas isso não discarda o resto. suponha que o buffer tem 10 de tamanho. o utilizador coloca aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa. resultado: segmentation fault. Link to comment Share on other sites More sharing options...
TheDark Posted May 24, 2008 at 04:10 PM Report Share #187182 Posted May 24, 2008 at 04:10 PM char formatstring[10]; sprintf(formatstring, "%%%ds", tamstring); scanf(formatstring, string); onde tamstring é um inteiro com o número máximo de caracteres que queres ler. Desaparecido. Link to comment Share on other sites More sharing options...
ville Posted May 24, 2008 at 07:39 PM Author Report Share #187246 Posted May 24, 2008 at 07:39 PM podia explicar o código sff? 😄 basicamente não percebo qual a intenção com o "%%%ds" Link to comment Share on other sites More sharing options...
Baderous Posted May 24, 2008 at 08:59 PM Report Share #187259 Posted May 24, 2008 at 08:59 PM Aquilo que vais utilizar como formato para guardar a string, no scanf, é o formatstring. Esse formatstring é obtido através do sprintf, que vai guardar nesse formatstring (que foi declarado como uma string), a string "%%%ds", onde o %d vai receber o valor de tamstring. Mas para apresentar numa string o caracter '%', é necessário precedê-lo de 2 '%', pois este é um caracter especial. Assim, efectivamente o que vais ter na string formatstring é "%ds", onde o %d vai ter o valor de tamstring (por ex: se tamstring=40, então formatstring="%40s"). Assim, depois no scanf, vais ter scanf("%40s",string). Link to comment Share on other sites More sharing options...
ville Posted May 25, 2008 at 02:05 AM Author Report Share #187309 Posted May 25, 2008 at 02:05 AM Entendido! Obrigado. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now