filestream

Sources d'information

Librairie(s) requise(s)

iostream, ifstream, ofstream

Comment ça marche

Similaire aux stream (sortie standard)

#include <iostream>
#include <string>
#include <fstream>

int main ()
{
    std::string     filename = "./test.out";
    std::string     str_from_file;
    std::ifstream   ifs("numbers");
    unsigned int    dst;
    unsigned int    dst2;

    ifs >> dst >> dst2; 
    std::cout << dst << " " << dst2 << std::endl;
    ifs.close();
    // -------------------------------------------
    std::ofstream   ofs("./test.out");
    ofs << "I like ponies a whole damn lot" << std::endl;
    ofs.close();
    // -------------------------------------------
    // developpement personnel
    // -------------------------------------------
    std::ifstream   ifs2;
    int             j = 0;
    ifs2.open("./test.out");
    if (ifs2.fail())
    {
        std::cout << "Error file failed to open!" << std::endl;
    }
    else
    {
        std::string text;
        while(getline(ifs2, text))
        {
            std::cout << text << std::endl;
            j++;
        }
        std::cout << j << " line read ---" << std::endl;
        ifs2.close();
    }
        // -------------------------------------------
    char            c;
    int             i = 0;
    std::ifstream   ifs3;
    ifs3.open("numbers");
    if (ifs3.fail())
    {
        std::cout << "Error file failed to open!" << std::endl;
    }
    else
    {
        std::string text;
        while(ifs3.get(c))
        {
            std::cout << c;
            i++;
        }
        std::cout << std::endl << i << " char read ---" << std::endl;
        ifs3.close();
    }
    // -------------------------------------------
    std::ifstream   ifs4;
    ifs4.open("numbers");
    if (ifs4.fail())
    {
        std::cout << "Error file failed to open!" << std::endl;
    }
    else
    {
        std::string ifs4texte;
        // get word
        ifs4 >> ifs4texte;
        std::cout << ifs4texte << std::endl;
        ifs4.close();
    }
}        
    
12 42
I like ponies a whole damn lot
1 line read ---
12 42
other line 1
and more
27 char read ---
12   

Egalement sur les strings

#include <iostream>
#include <sstream>
#include <string>

// C++ program to count words in 
// a string using stringstream.
// and number of K into the string
    
int countWords(std::string str)
{
    // Breaking input into word
    // using string stream
    
    // Used for breaking words
    std::stringstream s(str);
    
    // To store individual words
    std::string word;
    
    int count = 0;
    while (s >> word)
        count++;
    return count;
}

int countk(std::string str)
{
    // Breaking input into word
    // using string stream
    
    // Used for breaking words
    std::stringstream s2(str);
    
    // To store individual words
    char carractere;
    
    int countc = 0;
    while (s2.get(carractere))
    {
        if (carractere == 'k')
        {
            std::cout << carractere << " ";
            countc++;
        }
    }
    return countc;
}
    
// Driver code
int main()
{
    std::string s = "geeks for geeks geeks "
                "contribution placements";
    std::cout << " Number of words are: " << countWords(s) << std::endl;
    std::cout << " Number of k are: " << countk(s);

    std::cout << s << std::endl;
    // pure string management
    std::string repl1 = "for";
    std::string repl2 = "to";
    std::cout << s << std::endl;
    int pos = s.find(repl1, 1);
    s.erase(pos, repl1.size());
    std::cout << s << std::endl;
    s.insert(pos, repl2);
    std::cout << "1234567890123456789" << std::endl;
    std::cout << "      ---" << std::endl;
    std::cout << s << std::endl;
    pos = s.find(repl2, 1);
    s.replace(pos, repl2.size(), repl1);
    std::cout << s << std::endl;


    return 0;
}
 Number of words are: 6
 Number of k are: k k k 3geeks for geeks geeks contribution placements
geeks for geeks geeks contribution placements
geeks  geeks geeks contribution placements
1234567890123456789
        ---
geeks to geeks geeks contribution placements
geeks for geeks geeks contribution placements

retour


© 2022, vroch
Revisé le : 11 nov 2022
URL : http://vroch.ch/vr_tech_039.html
Main : http://vroch.ch