C++ Text File read line by line

Description

C++ Text File read line by line

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

using namespace std;

string ReadString(ifstream &file)//  w  w  w .  j a v  a  2s  .com
{
    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;
}
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
using namespace std;
int main()/*w w  w  .  j  av a  2  s.c  om*/
{
   string filename = "prices.dat";
   string line;
   ifstream inFile;
   inFile.open(filename.c_str());
   if (inFile.fail())  // check for successful open
   {
      cout << "\nThe file was not successfully opened\n Please check that the file currently exists." << endl;
      exit(1);
   }
   // Read and display the file's contents
   while (getline(inFile,line))
      cout << line << endl;
   inFile.close();
   return 0;
}



PreviousNext

Related