i want to read each line of my file in seprated string
what i have achieved till now is i can read the whole lines in one string
and now i want to read each line in a sprated string
any help;

#include <cstdlib>
#include <iostream>
#include <fstream>

using namespace std;

int main(int argc, char *argv[])

    ofstream myfile ("example.txt",ios::trunc);
    if (myfile.is_open())
      myfile<<"Hossam Hassan Hossney "<<endl;
      myfile<<"20070123 "<<endl; 
      myfile<<"hossambob2003@hotmail.com "; 


    cout<<"unable to open the file";

   ifstream myReadFile;
   string name;
   string temp;
   char output[100];
   if (myReadFile.is_open()) {
   while (!myReadFile.eof()) {


    return EXIT_SUCCESS;

Recommended Answers

All 5 Replies

Member Avatar


is generally the preferred way to do this.

can you tell me by example how to use this function

Member Avatar

Can't test this but...

while ( getline ( read, line, '\n' ) )


read is ifstream read ( "samplefile.txt" )line is a std::string

Arrays maybe?

Or better yet, vectors, since the number of lines in the file is not known during compilation.

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

using namespace std;

int main()
    ifstream fin("main.cpp");

    vector<string> lines;

    string curLine;

    while (getline(fin, curLine)) lines.push_back(curLine);

    for (size_t i = 0, size = lines.size(); i < size; ++ i)
        cout << lines[i] << endl;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.