Ciao raga, dovrei scrivere su un file, il contenuto di una variabile intera.
Ma indipendentemente dal contenuto di questa variabile, essa deve essere stampata nel seguente modo:
1(+4byte vuoti)
12(+3byte vuoti)
444(+2byte vuoti)
Mi potete aiutare??
Ciao raga, dovrei scrivere su un file, il contenuto di una variabile intera.
Ma indipendentemente dal contenuto di questa variabile, essa deve essere stampata nel seguente modo:
1(+4byte vuoti)
12(+3byte vuoti)
444(+2byte vuoti)
Mi potete aiutare??
Per "byte vuoti" intendi degli spazi?
No MP tecnici (non rispondo nemmeno!), usa il forum.
sisi
Usando C++ puoi fare: (ho scritto il carattere spazio come esadecimale perché non era molto visibile).
codice:#include <fstream> #include <iomanip> using namespace std; int main() { ofstream of("d:/zxc.txt"); of << setw(5) << setfill(0x20) << 1 << endl; of << setw(5) << setfill(0x20) << 12 << endl; of << setw(5) << setfill(0x20) << 123 << endl; of << setw(5) << setfill(0x20) << 1234 << endl; of << setw(5) << setfill(0x20) << 12345 << endl; of.close(); return 0; }
This code and information is provided "as is" without warranty of any kind, either expressed
or implied, including but not limited to the implied warranties of merchantability and/or
fitness for a particular purpose.
ti ringrazio shodan, ma il tuo pezzo programma, quando lo vado a compilare, mi da errore:
alla riga:codice:no match for 'operator<<' in 'std::operator<< [with _CharT = char, _Traits = std::char_traits<char>](((std::basic_ostream<char, std::char_traits<char> >&) ((std::basic_ostream<char, std::char_traits<char> >*)(&of))), std::setw(5)) << std::setfill [with _CharT = int](32)'
of << setw(5) << setfill(0x20) << 1 << endl;
inoltre non so il suo funzionamento.
Scrivi cosi'Originariamente inviato da Salvatore_91
of << setw(5) << setfill(0x20) << 1 << endl;
of << setw(5) << setfill((char)0x20) << 1 << endl;
No MP tecnici (non rispondo nemmeno!), usa il forum.
ok grazie oregon, in questo modo funziona, però questo piccolo programma, non e' coretto secondo la mia richiesta.
prima di tutto, lascia gli spazi (vuoti) a sinistra,
io chiedevo se c'era qualke funzione che su variabili intere, lasciasse byte vuoti:
ESEMPIO:
12(3 spaz vuoti) // la somma dei byte e' uguale a 5
1(4 spazi vuoti) // la somma dei byte e' uguale a 5
789(2 spazi vuoti)
tipo come questa funzione:
fprintf(w, "%-20s",a.nome);
//che lascia (20-nome) spazi bianchi
Allora cosi' ...
e quindicodice:#include <string> #include <sstream>
codice:stringstream s; string ss; int v = 123; s << v; ss = s.str(); ss.resize(5); cout << ss;
No MP tecnici (non rispondo nemmeno!), usa il forum.
O anche così:
codice:#include <fstream> #include <iomanip> using namespace std; int main() { ofstream of("d:/zxc.txt"); of << left; of << setw(5)<< 1 << endl; of << setw(5)<< 12 << endl; of << setw(5)<< 123 << endl; of << setw(5)<< 1234 << endl; of << setw(5)<< 12345 << endl; of.close(); return 0; }
This code and information is provided "as is" without warranty of any kind, either expressed
or implied, including but not limited to the implied warranties of merchantability and/or
fitness for a particular purpose.
senti, oregon, il tuo programma mi da errore su questa riga quando lo compilo:
string stream s
errore:
`string' undeclared (first use this function)
codice:# include <iostream> #include <string> #include <sstream> int main() { string stream s; string ss; int v = 123; s << v; ss = s.str(); ss.resize(5); cout << ss; system("PAUSE"); }