Specifying a Delimiter with the getline Function - C++ File Stream

C++ examples for File Stream:stream

Description

Specifying a Delimiter with the getline Function

Demo Code

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

using namespace std;

string ReadString(ifstream &file)/*from  w  w w  .j  a  va2s.co m*/
{
    char buf[1024]; // Make sure this is big enough!
    file.getline(&(buf[0]), 1024, ';');
    return string(buf);
}

int main()
{
    ifstream delimfile("../delims.txt");

    while (1)
    {
        string words = ReadString(delimfile);
        if (delimfile.eof() == true)
            break;
        cout << words << endl;
    }
    delimfile.close();
    return 0;
}

Result


Related Tutorials