|
|
|
![]() |
|
Strumenti |
![]() |
#1 |
Senior Member
Iscritto dal: Oct 2005
Messaggi: 1023
|
[C++]Stringa Composta? Stringa -> riga in file?
Ciao a tutti, innanzitutto programmo in C++ da 1 giorno dopo che ho da sempre programmato in pascal (
![]() ![]() Allora, praticamente il mio problema è quello di creare una stringa composta da usare poi come nome per un file. Mi spiego meglio: Codice:
string dst,name; int i; cout << "Quanti file devo creare?" << endl; cin >> N; cout << "Inserisci il percorso in cui salvarli" << endl; cin >> dst; for (i=0; i<=N; i++) { --altre istruzioni del for-- name = dst + i + "." + rand()%10 + rand()%10 + rand()%10; --altre istruzioni del for-- } c:\test\255.671 Qualcuno potrebbe aiutarmi? ![]() Inoltre, come faccio a scrivere una riga in un file ? il file lo apro con Codice:
FILE *stream = fopen("c:\\programdata.txt","w"); Codice:
fprintf(stream,"%d",variabileinteraacaso); Il problema arriva quando tento di fare questo: Codice:
frpintf(stream,"%s",variabilestringaacaso); Codice:
30 D:\Documents and Settings\Nik\Documenti\C++\TUHDSS.cpp [Warning] cannot pass objects of non-POD type `struct std::string' through `...'; call will abort at runtime ![]() |
![]() |
![]() |
![]() |
#2 | ||||
Bannato
Iscritto dal: Feb 2005
Città: Roma
Messaggi: 7027
|
Quote:
Codice:
for (int i = 0; i < N; i++) { string dst = "C:\\test\\"; ostringstream oss; oss << dst << i << "." << (rand() % 10 + rand() % 10 + rand() % 10); string name = oss.str(); } Quote:
Quote:
![]() Quote:
http://www.cplusplus.com/reference/s...ing/c_str.html ma come ti ho già spiegato, in C++ usa la libreria di I/O delle STL, che è anche più facile da usare (non ci sono format string). |
||||
![]() |
![]() |
![]() |
#3 | ||
Senior Member
Iscritto dal: Oct 2005
Messaggi: 1023
|
Quote:
Codice:
26 D:\Documents and Settings\Nik\Documenti\C++\TUHDSS.cpp aggregate `std::stringstream oss' has incomplete type and cannot be defined ![]() Quote:
![]() Ho solo un problema: non riesco ad andare a capo. ![]() se io metto Codice:
outfile << "stringaperprovare" << endl << DST; Codice:
stringaperprovare[caratterestrano]c:\uindov\sistemtrentadue\ Codice:
outfile << "stringaperprovare \n" << DST; ![]() ancora questo piccolo helpettino e ce la posso fare ![]() EDIT: altro problemino, Codice:
ofstream outfile (dst,ofstream::binary); Codice:
24 D:\Documents and Settings\Nik\Documenti\C++\TUHDFS.cpp no matching function for call to `std::basic_ofstream<char, std::char_traits<char> >::basic_ofstream(std::string&, const std::_Ios_Openmode&)' Ultima modifica di demonbl@ck : 02-08-2008 alle 02:16. |
||
![]() |
![]() |
![]() |
#4 | ||||
Bannato
Iscritto dal: Feb 2005
Città: Roma
Messaggi: 7027
|
Quote:
Quote:
Codice:
ofstream outfile("nome e percorso"); Quote:
![]() Quote:
|
||||
![]() |
![]() |
![]() |
#5 | |
Senior Member
Iscritto dal: Oct 2005
Messaggi: 1023
|
Quote:
![]() Però restano due problemi: 1)Se provo ad usare OSS per aprire un file mi butta fuori Codice:
46 D:\Documents and Settings\Nik\Documenti\C++\TUHDFS.cpp invalid conversion from `void*' to `const char*' 46 D:\Documents and Settings\Nik\Documenti\C++\TUHDFS.cpp initializing argument 1 of `std::basic_ofstream<_CharT, _Traits>::basic_ofstream(const char*, std::_Ios_Openmode) [with _CharT = char, _Traits = std::char_traits<char>]' ![]() ![]() ![]() 3)se invece di OSS per aprire il file provo con DST (dandogli un valore opportuno) ottengo sempre Codice:
23 D:\Documents and Settings\Nik\Documenti\C++\TUHDFS.cpp no matching function for call to `std::basic_ofstream<char, std::char_traits<char> >::basic_ofstream(std::string&)' ![]() |
|
![]() |
![]() |
![]() |
#6 | |||
Bannato
Iscritto dal: Feb 2005
Città: Roma
Messaggi: 7027
|
Quote:
Quote:
Quote:
![]() dst non era una stringa? il file aprilo semplicemente con l'ofstream: Codice:
ofstream outfile("C:\\pippo.txt"); |
|||
![]() |
![]() |
![]() |
#7 | ||
Senior Member
Iscritto dal: Oct 2005
Messaggi: 1023
|
Quote:
![]() Quote:
![]() Quindi o apro il file direttamente con OSS oppure devo assegnare a DST2 il valore di OSS e poi aprirlo con DST2. ![]() OSS << DST << I << "." << rand()%10;<< rand()%10;<< rand()%10; I è la variabile di controllo del for e mi serve appunto per dare una sequenzialità ai nomi. ![]() |
||
![]() |
![]() |
![]() |
#8 |
Senior Member
Iscritto dal: Oct 2005
Messaggi: 1023
|
Up?
![]() |
![]() |
![]() |
![]() |
#9 |
Bannato
Iscritto dal: Feb 2005
Città: Roma
Messaggi: 7027
|
![]() allora... Codice:
for (int i = 0; i < N; i++) { string dst = "C:\\test\\"; ostringstream oss; oss << dst << i << "." << (rand() % 10 + rand() % 10 + rand() % 10); string name = oss.str(); ofstream outfile(name); if (outfile.good()) { // scrivi sul file // ... } } |
![]() |
![]() |
![]() |
#10 | |
Senior Member
Iscritto dal: Oct 2005
Messaggi: 1023
|
Quote:
Codice:
43 D:\Documents and Settings\Nik\Documenti\C++\TUHDSS.cpp no matching function for call to `std::basic_ofstream<char, std::char_traits<char> >::basic_ofstream(std::string&)' ![]() |
|
![]() |
![]() |
![]() |
#11 |
Bannato
Iscritto dal: Feb 2005
Città: Roma
Messaggi: 7027
|
ops, verissimo errore mio:
Codice:
for (int i = 0; i < N; i++) { string dst = "C:\\test\\"; ostringstream oss; oss << dst << i << "." << (rand() % 10 + rand() % 10 + rand() % 10); string name = oss.str(); ofstream outfile(name.c_str()); if (outfile.good()) { // scrivi sul file // ... } } |
![]() |
![]() |
![]() |
#12 | |
Senior Member
Iscritto dal: Oct 2005
Messaggi: 1023
|
Quote:
Comunque funziona perfettamente, grazie ![]() |
|
![]() |
![]() |
![]() |
Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 22:42.