|
|
|
![]() |
|
Strumenti |
![]() |
#1 |
Senior Member
Iscritto dal: Nov 2003
Città: Pordenone - Tarvisio
Messaggi: 2449
|
[C++]Dubbio matrici
Salve a tutti,
ho appena fatto il seguente programma: Codice:
#include <iostream.h> #include <stdio.h> int main() { const int righe=5,colonne=20; //crea una matrice da 5 righe e 20 colonne; le colonne sono le lettere char matrice[righe][colonne],scelta; int i; for(i=0;i<righe;i++) { cout<<"Inserire la parola di posto "<<i<<endl; gets(matrice[i]); } do { cout<<"Desideri vedere la matrice? "<<endl; cin>>scelta; }while(scelta!='s'); if(scelta=='s') { for(i=0;i<righe;i++) { cout<<matrice[i]<<" "; } } getchar(); } Codice:
In file included from /usr/include/c++/3.3.2/backward/iostream.h:31, from stringhe_matrici.cpp:1: /usr/include/c++/3.3.2/backward/backward_warning.h:32:2: warning: #warning This file includes at least one deprecated or antiquated header. Please consider using one of the 32 headers found in section 17.4.1.2 of the C++ standard. Examples include substituting the <X> header for the <X.h> header for C++ includes, or <sstream> instead of the deprecated header <strstream.h>. To disable this warning use -Wno-deprecated. stringhe_matrici.cpp:29:10: warning: no newline at end of file /home/antonio/tmp/ccSqqUJT.o(.text+0x8c): In function `main': : warning: the `gets' function is dangerous and should not be used. ![]() ![]()
__________________
Me? The Chosen One? They chose me, and i didn't even graduate from fuckin' high school Wind FTTE Vula 100/20 - Stats Retelit / Valcanale 20Mbit/2Mbit // Wind 100/20+Wind4G con EdgerouterX |
![]() |
![]() |
![]() |
#2 | |||
Moderatore
Iscritto dal: Nov 2003
Messaggi: 16211
|
Re: [C++]Dubbio matrici
Quote:
Probabilmente si riferisce al fatto che mescoli input-output in stile C++ (iostream.h) e in stile C (stdio.h). Inoltre, ti dice che se non vuoi che ti dia questo messaggio di avviso, devi passare il parametro -Wno-deprecated a gcc quando lo lanci. Quote:
Perche' questo editor ha l'abitudine di non aggiungere automaticamente un carattere di fine riga alla fine di un file di testo. Roba che si corregge in un attimo, comunque. Quote:
__________________
Ubuntu è un'antica parola africana che significa "non so configurare Debian" ![]() Scienza e tecnica: Matematica - Fisica - Chimica - Informatica - Software scientifico - Consulti medici REGOLAMENTO DarthMaul = Asus FX505 Ryzen 7 3700U 8GB GeForce GTX 1650 Win10 + Ubuntu |
|||
![]() |
![]() |
![]() |
#3 |
Senior Member
Iscritto dal: Nov 2003
Città: Pordenone - Tarvisio
Messaggi: 2449
|
Ti ringrazio.
Ho messo fgets, pero': Codice:
In file included from /usr/include/c++/3.3.2/backward/iostream.h:31, from stringhe_matrici.cpp:1: /usr/include/c++/3.3.2/backward/backward_warning.h:32:2: warning: #warning This file includes at least one deprecated or antiquated header. Please consider using one of the 32 headers found in section 17.4.1.2 of the C++ standard. Examples include substituting the <X> header for the <X.h> header for C++ includes, or <sstream> instead of the deprecated header <strstream.h>. To disable this warning use -Wno-deprecated. /usr/include/stdio.h: In function `int main()': /usr/include/stdio.h:526: error: too few arguments to function `char* fgets(char*, int, FILE*)' stringhe_matrici.cpp:12: error: at this point in file stringhe_matrici.cpp:31:10: warning: no newline at end of file ![]() P.S. Come editor uso Kwrite
__________________
Me? The Chosen One? They chose me, and i didn't even graduate from fuckin' high school Wind FTTE Vula 100/20 - Stats Retelit / Valcanale 20Mbit/2Mbit // Wind 100/20+Wind4G con EdgerouterX |
![]() |
![]() |
![]() |
#4 |
Senior Member
Iscritto dal: Nov 2003
Città: Pordenone - Tarvisio
Messaggi: 2449
|
UP
__________________
Me? The Chosen One? They chose me, and i didn't even graduate from fuckin' high school Wind FTTE Vula 100/20 - Stats Retelit / Valcanale 20Mbit/2Mbit // Wind 100/20+Wind4G con EdgerouterX |
![]() |
![]() |
![]() |
#5 | |
Senior Member
Iscritto dal: Mar 2004
Messaggi: 1449
|
Quote:
__________________
Ciao ~ZeRO sTrEsS~ |
|
![]() |
![]() |
![]() |
#6 | |
Senior Member
Iscritto dal: Nov 2003
Città: Pordenone - Tarvisio
Messaggi: 2449
|
Quote:
Codice:
#include <iostream.h> #include <stdio.h> int main() { const int righe=5,colonne=20; //crea una matrice da 5 righe e 20 colonne; le colonne sono le lettere char matrice[righe][colonne],scelta; int i; for(i=0;i<righe;i++) { cout<<"Inserire la parola di posto "<<i<<endl; fgets(matrice[i]); } do { cout<<"Desideri vedere la matrice? "<<endl; cin>>scelta; }while(scelta!='s'); if(scelta=='s') { for(i=0;i<righe;i++) { cout<<matrice[i]<<" "; } } getchar(); } ![]()
__________________
Me? The Chosen One? They chose me, and i didn't even graduate from fuckin' high school Wind FTTE Vula 100/20 - Stats Retelit / Valcanale 20Mbit/2Mbit // Wind 100/20+Wind4G con EdgerouterX |
|
![]() |
![]() |
![]() |
#7 | |
Senior Member
Iscritto dal: Mar 2004
Messaggi: 1449
|
Quote:
__________________
Ciao ~ZeRO sTrEsS~ |
|
![]() |
![]() |
![]() |
#8 | |
Senior Member
Iscritto dal: Nov 2003
Città: Pordenone - Tarvisio
Messaggi: 2449
|
Quote:
__________________
Me? The Chosen One? They chose me, and i didn't even graduate from fuckin' high school Wind FTTE Vula 100/20 - Stats Retelit / Valcanale 20Mbit/2Mbit // Wind 100/20+Wind4G con EdgerouterX |
|
![]() |
![]() |
![]() |
#9 |
Senior Member
Iscritto dal: Sep 2002
Città: Celano (AQ) Segno_Zodiacale: Leone Ascendente: Cammello Segni_Particolari: Quello
Messaggi: 9302
|
ti faccio un esempio d'uso di fgets per leggere 30 caratteri (massimo) dalla tastiera (stdin, cioè standard input) e memorizzarli nel vettore buffer (che chiaramente deve essere lungo almeno 30 char):
fgets(buffer, 30, stdin); ok? |
![]() |
![]() |
![]() |
#10 |
Senior Member
Iscritto dal: May 2002
Città: Torino
Messaggi: 616
|
se usi fgets(bla,bla,stdin) è come usare gets(...)
__________________
[INSERIRE QUI LA FIRMA] |
![]() |
![]() |
![]() |
#11 |
Senior Member
Iscritto dal: Mar 2004
Messaggi: 1449
|
char *fgets(char *str, int n, FILE *stream);
Reads a line from the specified stream and stores it into the string pointed to by str. It stops when either (n-1) characters are read, the newline character is read, or the end-of-file is reached, whichever comes first. The newline character is copied to the string. A null character is appended to the end of the string. On success a pointer to the string is returned. On error a null pointer is returned. If the end-of-file occurs before any characters have been read, the string remains unchanged. Ovviamente puoi mettere anche standard input come stream.
__________________
Ciao ~ZeRO sTrEsS~ |
![]() |
![]() |
![]() |
#12 | |
Senior Member
Iscritto dal: Sep 2002
Città: Celano (AQ) Segno_Zodiacale: Leone Ascendente: Cammello Segni_Particolari: Quello
Messaggi: 9302
|
Quote:
|
|
![]() |
![]() |
![]() |
#13 |
Senior Member
Iscritto dal: May 2002
Città: Torino
Messaggi: 616
|
la mia non era una critica!
non intendevo dire che usando fgets() si hanno gli stessi problemi di vulnerabilità come se si usasse la gets() ![]()
__________________
[INSERIRE QUI LA FIRMA] |
![]() |
![]() |
![]() |
#14 |
Senior Member
Iscritto dal: Nov 2003
Città: Pordenone - Tarvisio
Messaggi: 2449
|
Vi credo sulla parola
![]() Nel mio caso..potrei mettere Codice:
fgets(matrice[i],stdin);
__________________
Me? The Chosen One? They chose me, and i didn't even graduate from fuckin' high school Wind FTTE Vula 100/20 - Stats Retelit / Valcanale 20Mbit/2Mbit // Wind 100/20+Wind4G con EdgerouterX |
![]() |
![]() |
![]() |
#15 |
Senior Member
Iscritto dal: Nov 2003
Città: Pordenone - Tarvisio
Messaggi: 2449
|
Ho risolto cosi:
Codice:
#include <iostream.h> #include <stdio.h> int main() { const int righe=5,colonne=20; //crea una matrice da 5 righe e 20 colonne; le colonne sono le lettere char matrice[righe][colonne],scelta; int i; for(i=0;i<righe;i++) { cout<<"Inserire la parola di posto "<<i<<endl; fgets(matrice[i],colonne,stdin); } do { cout<<"Desideri vedere la matrice? "<<endl; cin>>scelta; }while(scelta!='s'); if(scelta=='s') { for(i=0;i<righe;i++) { cout<<matrice[i]<<" "; } } getchar(); } ![]() Bello, ma perchè gets è considerato poco sicuro? Se ho capito bene, è perchè se per esempio io ho una stringa da 20 caratteri e ne metto 30, questa scrive gli altri 10 caratteri in un altra area di memoria. Ho detto una cazzata? ![]()
__________________
Me? The Chosen One? They chose me, and i didn't even graduate from fuckin' high school Wind FTTE Vula 100/20 - Stats Retelit / Valcanale 20Mbit/2Mbit // Wind 100/20+Wind4G con EdgerouterX |
![]() |
![]() |
![]() |
#16 | |
Senior Member
Iscritto dal: May 2002
Città: Torino
Messaggi: 616
|
Quote:
Codice:
fgets(matrice[i],MAXBUFF,stdin);
__________________
[INSERIRE QUI LA FIRMA] Ultima modifica di MauryWeb : 13-01-2005 alle 21:04. |
|
![]() |
![]() |
![]() |
#17 |
Senior Member
Iscritto dal: Nov 2003
Città: Pordenone - Tarvisio
Messaggi: 2449
|
ho risposto su
![]()
__________________
Me? The Chosen One? They chose me, and i didn't even graduate from fuckin' high school Wind FTTE Vula 100/20 - Stats Retelit / Valcanale 20Mbit/2Mbit // Wind 100/20+Wind4G con EdgerouterX |
![]() |
![]() |
![]() |
#18 |
Senior Member
Iscritto dal: Apr 2000
Città: Vicino a Montecatini(Pistoia) Moto:Kawasaki Ninja ZX-9R Scudetti: 29
Messaggi: 53971
|
Metti:
#include <iostream> #include <cstdio> using namespace std; al posto di: #include <iostream.h> #include <stdio.h> Il modo di includere che hai usato è deprecato...studiati qualcosa sui namespace... |
![]() |
![]() |
![]() |
#19 | |
Senior Member
Iscritto dal: Nov 2003
Città: Pordenone - Tarvisio
Messaggi: 2449
|
Quote:
![]()
__________________
Me? The Chosen One? They chose me, and i didn't even graduate from fuckin' high school Wind FTTE Vula 100/20 - Stats Retelit / Valcanale 20Mbit/2Mbit // Wind 100/20+Wind4G con EdgerouterX |
|
![]() |
![]() |
![]() |
#20 |
Senior Member
Iscritto dal: Apr 2000
Città: Vicino a Montecatini(Pistoia) Moto:Kawasaki Ninja ZX-9R Scudetti: 29
Messaggi: 53971
|
Se metti using namespace std; vedrai che funziona tutti... Gli include del C si mettono con la "c" davanti per includerle nel namespace std... Fai una ricerchina sui namespace...
|
![]() |
![]() |
![]() |
Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 06:18.