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

    [C++]Problema di I/0 su file

    Voglio leggere una determinata stringa che ho precedentemente salvato e leggerla fino ad un carattere stabilito( %end ).
    Ho provato cosi:
    codice:
    #include <iostream>
    #include <sstream>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
        return 0;
    }
    
    void scrivi(string nome, string text)
    {
        string cosa;
        stringstream temp_write;
        ofstream out(nome.c_str());
        temp_write << text;
        text = temp_write.str();
        out << text << "%end" << text;
        out.close();
    }
    
    string leggi(string nome)
    {
        string cosa;
        stringstream temp_read;
        ifstream in(nome.c_str());
        while(in.get() != "%end")
        {
            temp_read << in.put();
        }
        cosa = temp_read.str();
        return cosa;
    }
    Ma mi da

    C:/Documents and Settings/Admin/Desktop/Test.cpp:29: ISO C++ forbids comparison
    between pointer and integer
    C:/Documents and Settings/Admin/Desktop/Test.cpp:31: no matching function for
    call to `std::basic_ifstream<char, std::char_traits<char> >:ut()'



    .-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.
    Ciao da Kleidemos
    C++ Programmer
    .-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.
    La stupidità umana e l'universo sono infinite.
    Della seconda non sono certo(Einstein)

    Gnu/Linux User

  2. #2
    Utente di HTML.it
    Registrato dal
    Dec 2001
    Messaggi
    167
    Che è 'sta linea ?
    while(in.get() != "%end")
    Ovvio che ti dia errore.

  3. #3
    Originariamente inviato da Level
    Che è 'sta linea ?
    while(in.get() != "%end")
    Ovvio che ti dia errore.
    e che cerco di legere il files fino all simbolo, da me definito, %end!
    In pratica il files sara:

    ioioio%endioioio

    E la var che stampero sara: ioioio !

    La stupidità umana e l'universo sono infinite.
    Della seconda non sono certo(Einstein)

    Gnu/Linux User

  4. #4
    Utente di HTML.it
    Registrato dal
    Dec 2001
    Messaggi
    167
    Ma poi, scusa una cosa. L'ho guardato di sfuggita, ma che senso ha il main vuoto che non chiama le funzioni ?

  5. #5
    codice:
    #include <iostream>
    #include <cstdio>
    #include <sstream>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    void scrivi(string, string);
    string leggi(string);
    
    int main()
    {
        string test = "this.txt", txt = "ioioio%endioioio";
        scrivi(test, txt);
        cout << leggi(test) << endl;
        system("PAUSE");
        return 0;
    }
    
    void scrivi(string nome, string text)
    {
        stringstream temp_write;
        ofstream out(nome.c_str());
        temp_write << text;
        text = temp_write.str();
        out << text << "%end" << text;
        out.close();
    }
    
    string leggi(string nome)
    {
        string cosa, tmp;
        bool trovato = false;
        stringstream temp_read;
        ifstream in(nome.c_str());
        while(in)
        {
            temp_read << in;
        }
        while(temp_read)
        {
            if(temp_read.str() == "%end")
            {
                cosa = temp_read.str();
                trovato = true;
            }
            if(trovato)
            {
                cosa = tmp;
                continue;
            }
        }
        cosa = temp_read.str();
        return cosa.c_str();
    }
    Prova a dirmi cosa ti da............... a me nulla
    La stupidità umana e l'universo sono infinite.
    Della seconda non sono certo(Einstein)

    Gnu/Linux User

  6. #6
    Cosi nn compare nulla
    codice:
     #include <iostream>
    #include <cstdio>
    #include <sstream>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    void scrivi(string, string);
    string leggi(string);
    
    int main()
    {
        int sc;
        string test = "this.txt", txt = "ioioio";
        scrivi(test, txt);
        cin >> sc;
        if(sc == 1)
        {
        cout << leggi(test) << endl;
        }
        system("PAUSE");
        return 0;
    }
    
    void scrivi(string nome, string text)
    {
        stringstream temp_write;
        ofstream out(nome.c_str());
        temp_write << text;
        text = temp_write.str();
        out << text << "%end%" << "ciao";
        out.close();
    }
    
    string leggi(string nome)
    {
        string out, tmp, temp_read;
        bool trovato = false;
        ifstream in(nome.c_str());
            while (in.good())  
            {
                temp_read = in.get();   
            }
            while(temp_read != "\0")
            {
                if(temp_read == "%end%")
                {
                    out = temp_read;
                    trovato = true;
                }
                if(trovato)
                    continue;
            }
        in.close();    
        return out.c_str();
    }
    La stupidità umana e l'universo sono infinite.
    Della seconda non sono certo(Einstein)

    Gnu/Linux User

  7. #7
    codice:
    #include <string>
    
    int main()
    {
    	string s = "asjkhasdlgbajshakshg%endaskjhaslkjdgja";
    
    	cout << s.substr( 0, s.find( "%end" ) ) << '\n';
    
    	return 0;
    }

  8. #8
    tnk!
    Ma cosi non va:
    codice:
    #include <iostream> 
    #include <cstdio> 
    #include <sstream> 
    #include <fstream> 
    #include <string> 
    
    using namespace std; 
    
    void scrivi(string, string); 
    string leggi(string); 
    
    int main() 
    { 
        int sc; 
        string test = "this.txt", txt = "ioioio"; 
        scrivi(test, txt); 
        cin >> sc; 
        if(sc == 1) 
        { 
        cout << leggi(test) << endl; 
        } 
        system("PAUSE"); 
        return 0; 
    } 
    
    void scrivi(string nome, string text) 
    { 
        stringstream temp_write; 
        ofstream out(nome.c_str()); 
        temp_write << text; 
        text = temp_write.str(); 
        out << text << "%end%" << "ciao"; 
        out.close(); 
    } 
    
    string leggi(string nome) 
    { 
        string out, temp_read; 
        bool trovato = false; 
        ifstream in(nome.c_str()); 
            while (in)  
            { 
                temp_read = in.get();    
            } 
            out = temp_read.substr( 0, temp_read.find( "%end" ) );
        in.close();    
        return out.c_str(); 
    }
    La stupidità umana e l'universo sono infinite.
    Della seconda non sono certo(Einstein)

    Gnu/Linux User

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 © 2025 vBulletin Solutions, Inc. All rights reserved.