| 
 | |||||||
| 
 | 
|  | 
|  | 
|  | Strumenti | 
|  27-02-2013, 18:22 | #1 | 
| Junior Member Iscritto dal: Feb 2013 
					Messaggi: 4
				 | 
				
				PICCOLO PROBLEMA IN C++
			 
		Sto costruendo un programma in c++ e quando tento di creare un file txt nel desktop ricavando prima il nome utente ricevo un errore da parte del compilatore. Vi posto il pezzo di codice indicandovi dove lo riscontro(inizialmente ho incluso la libreria "iostream", "fstream", "conio.h", "windows.h" e "Lmcons.h"): TCHAR name[UNLEN + 1]; DWORD size = UNLEN + 1; if (GetUserName( (TCHAR*)name, &size )){ cout<<utente<<endl; _getch(); ofstream scrivi("C:\\Users\\" + name + "\\Desktop\\risultati.txt"); */ QUI SI VERIFICA L'ERRORE, NEL "+ name + " /* scrivi<<d<<".\n"; scrivi.close(); cout<<"File salvato correttamente!"<<endl; _getch(); } Ultima modifica di TnxElektro : 27-02-2013 alle 18:26. | 
|   |   | 
|  27-02-2013, 18:28 | #2 | 
| Senior Member Iscritto dal: Nov 2005 Città: Texas 
					Messaggi: 1722
				 | 
		
Non puoi sommare le stringhe, intese come char*!!!
		 
				__________________ In God we trust; all others bring data | 
|   |   | 
|  27-02-2013, 18:45 | #3 | 
| Junior Member Iscritto dal: Feb 2013 
					Messaggi: 4
				 | 
		Come devo fare allora? Mi puoi postare il codice esatto? Devo usare a posto del "+" l'operatore "&&"?
		 | 
|   |   | 
|  27-02-2013, 19:06 | #4 | 
| Senior Member Iscritto dal: Nov 2005 Città: Texas 
					Messaggi: 1722
				 | 
		Hai diverse soluzioni. Per esempio, potresti passare attraverso delle std::string Codice: /* La tiro per le lunghe, sperando che si capisca meglio */
std::string filename("C:\\Users\\");
filename += name;
filename +=  "\\Desktop\\risultati.txt";
ofstream scrivi(filename.c_str());Codice:      char str[255];
     sprintf (str, "C:\\Users\\%s\\Desktop\\risultati.txt", name);
     ofstream scrivi(str);Per ovviare a questa sgradevole situazione, puoi usare la int snprintf ( char * s, size_t n, const char * format, ... ); invece della sprintf: Codice:      char str[255];
     snprintf (str, sizeof(str),  "C:\\Users\\%s\\Desktop\\risultati.txt", name);
     ofstream scrivi(str);
				__________________ In God we trust; all others bring data | 
|   |   | 
|  27-02-2013, 20:05 | #5 | 
| Junior Member Iscritto dal: Feb 2013 
					Messaggi: 4
				 | 
		Molte grazie!! Ora il programma funziona! Ecco il codice che sto usando, anche se non comprende quello di tutto il software("scrivi" l'ho già dichiarato prima! E avevo precedentemente incluso la libreria "string"): TCHAR name[UNLEN + 1]; DWORD size = UNLEN + 1; if (GetUserName( (TCHAR*)name, &size )){ string dest="C:\\Users\\"; dest+=name; dest+="\\Desktop\\risultati.txt"; scrivi.open(dest.c_str()); scrivi<<d<<".\n"; scrivi.close(); cout<<"File salvato correttamente!"<<endl; _getch(); } } | 
|   |   | 
|  27-02-2013, 20:15 | #6 | |
| Senior Member Iscritto dal: Nov 2005 Città: Texas 
					Messaggi: 1722
				 | Quote: 
 Se questo non e' il tuo caso, allora va tutto bene   
				__________________ In God we trust; all others bring data | |
|   |   | 
|  27-02-2013, 21:20 | #7 | 
| Junior Member Iscritto dal: Feb 2013 
					Messaggi: 4
				 | 
		No, non è il mio caso.    | 
|   |   | 
|   | 
| Strumenti | |
| 
 | 
 | 
Tutti gli orari sono GMT +1. Ora sono le: 06:29.









 
		 
		 
		 
		







 
  
 



 
                        
                        










