Pagina 1 di 2 1 2 ultimoultimo
Visualizzazione dei risultati da 1 a 10 su 18
  1. #1

    [c++] scrivere var. int su file

    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??

  2. #2
    Utente di HTML.it L'avatar di oregon
    Registrato dal
    Jul 2005
    residenza
    Roma
    Messaggi
    36,462
    Per "byte vuoti" intendi degli spazi?
    No MP tecnici (non rispondo nemmeno!), usa il forum.

  3. #3
    sisi

  4. #4
    Utente di HTML.it L'avatar di shodan
    Registrato dal
    Jun 2001
    Messaggi
    2,381
    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.

  5. #5
    ti ringrazio shodan, ma il tuo pezzo programma, quando lo vado a compilare, mi da errore:
    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)'
    alla riga:
    of << setw(5) << setfill(0x20) << 1 << endl;


    inoltre non so il suo funzionamento.

  6. #6
    Utente di HTML.it L'avatar di oregon
    Registrato dal
    Jul 2005
    residenza
    Roma
    Messaggi
    36,462
    Originariamente inviato da Salvatore_91
    of << setw(5) << setfill(0x20) << 1 << endl;
    Scrivi cosi'

    of << setw(5) << setfill((char)0x20) << 1 << endl;
    No MP tecnici (non rispondo nemmeno!), usa il forum.

  7. #7
    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

  8. #8
    Utente di HTML.it L'avatar di oregon
    Registrato dal
    Jul 2005
    residenza
    Roma
    Messaggi
    36,462
    Allora cosi' ...

    codice:
    #include <string>
    #include <sstream>
    e quindi

    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.

  9. #9
    Utente di HTML.it L'avatar di shodan
    Registrato dal
    Jun 2001
    Messaggi
    2,381
    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.

  10. #10
    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");
    }

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.