Visualizzazione dei risultati da 1 a 7 su 7
  1. #1

    [C++] RichEdit non va a capo

    Salve.
    Sto riscontrando un fastidioso problema.

    In sintesi: ho un file txt in cui aggiungo mano mano stringhe, del tipo: "nome1 \r\n nome2\r\n nome3 \r\n" che appare, giustamente, così:
    codice:
    nome1
    nome2
    nome3
    Ora, la mia intenzione è quella di leggere il contenuto del file e riportarlo in un RichEdit.

    codice:
    void load(){
    	string line; //this will contain the data <strong class="highlight">read[/b] from the file
    	ifstream myfile ("a.txt"); //opening the file.
    	if (myfile.is_open()) //if the file is open
    	{
    		while (! myfile.eof() ) //while the end of file is NOT reached
    		{
    			getline (myfile,line); //get one line from the file
    			cout << line << endl; //and output it
    		}
    		myfile.close(); //closing the file
    	} else cout << "Unable to open file"; //if the file is not open output <--
    
    	Form1->RichEdit1->Text = "";
    	Form1->RichEdit1->Text = Form1->RichEdit1->Text + line.c_str();
    
    }
    Ottenendo però come risultato, nel RichEdit:
    codice:
    nome1nome2nome3
    Come diavolo faccio a risolvere? Grazie in anticipo.

  2. #2
    Devi aggiungere uno spazio tra il testo contenuto nel richedit e il testo che aggiungi...
    Amaro C++, il gusto pieno dell'undefined behavior.

  3. #3
    Originariamente inviato da MItaly
    Devi aggiungere uno spazio tra il testo contenuto nel richedit e il testo che aggiungi...
    Inizialemente il RichEdit è vuoto, il contenuto del file lo carico tutto subito. Però come ho già detto, lo riporta senza i ritorni a capo.

  4. #4
    Scusa, ho letto male. Comunque dovrebbe essere una cosa del genere:
    codice:
    void load(){
    	string line; //this will contain the data <strong class="highlight">read[/b] from the file
    	ifstream myfile ("a.txt"); //opening the file.
    	if (myfile.is_open()) //if the file is open
    	{
    		while (! myfile.eof() ) //while the end of file is NOT reached
    		{
    			getline (myfile,line); //get one line from the file
    			Form1->RichEdit1->Text += (line + '\n').c_str();
    		}
    		myfile.close(); //closing the file
    	} else cout << "Unable to open file"; //if the file is not open output <--
    .
    Amaro C++, il gusto pieno dell'undefined behavior.

  5. #5
    Originariamente inviato da MItaly
    Scusa, ho letto male. Comunque dovrebbe essere una cosa del genere:
    codice:
    void load(){
    	string line; //this will contain the data <strong class="highlight">read[/b] from the file
    	ifstream myfile ("a.txt"); //opening the file.
    	if (myfile.is_open()) //if the file is open
    	{
    		while (! myfile.eof() ) //while the end of file is NOT reached
    		{
    			getline (myfile,line); //get one line from the file
    			Form1->RichEdit1->Text += (line + '\n').c_str();
    		}
    		myfile.close(); //closing the file
    	} else cout << "Unable to open file"; //if the file is not open output <--
    .
    Ciao,
    sicuro sia giusto? Nel RichEdit non si visualizza niente, e il ciclo non mi sembra funzionare bene. :master:

  6. #6
    In che senso "non mi sembra funzionare bene"? Tra l'altro, che libreria stai usando per l'interfaccia grafica?
    Amaro C++, il gusto pieno dell'undefined behavior.

  7. #7
    Ok, risolto! Il ciclo funzionava bene. Ho scoperto che facendo così:
    codice:
    Form1->RichEdit1->Text +=  (line + '\n').c_str();
    non andava, mentre senza il +=, ossia così:
    codice:
    Form1->RichEdit1->Text =  Form1->RichEdit1->Text + (line + '\n').c_str();
    va perfettamente. Chissà come mai.

    PS: Comunque uso CodeGear C++ Builder 2010 per la grafica.

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.