Can I use getline to get two lines of string data?

for example:

string line;
string myfile;
ifstream file;

file.open(myfile.c_str());

while(!myfile.eof())
{
       getline(myfile, line)
}

The while statement is incorrect.

while( getline(myfile, line) )
{
    // do something with this line
}

Well I have this code and I need getline to get two lines from the file to put into the Uni string, Im guessing that is not possible with string? Here is the whole code.

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

using namespace std;

int main()
{
	ifstream file;
	string SetA;
	string SetB;
	string Uni;
	string myfile;

	char ch[]={0};
	const char *A = ch;
	const char *B = ch;
	const char *U = ch;

    int n = 0;
	int number[100];//for Universal set
	int bit[25];//for set a
	int bit2[25];
	int count=0;
	int count2=0;

	cout<<"Enter file name.\n";
	cin>>myfile;

	file.open(myfile.c_str());

	 while(!file)// Prompt for new file name if not able to open
   {
      cout << "Unable to open file. Enter a different name: ";
	  file.clear();
      cin >> myfile;
	  file.open(myfile.c_str());
	  
   }
	 while (getline(file, Uni))
	 {
		//getline(file, Uni);
		cout<<"Universal Set: {"<<Uni<<"}"<<endl;
	 }
	 U = Uni.c_str();

	//**********************************************************************
	
	for(int x=0;x<10;x++)
		number[x]=1;

	file.clear( );
    file.seekg( 0 );
 
	while(getline(file, SetA, '\n'))
	{
		getline(file, SetB, '\n');
		cout<<"Set A: {"<<SetA<<"}"<<endl;
		cout<<"Set B: {"<<SetB<<"}"<<endl;
	}
	//*********************************************************************	
	//cout<<allwords[8];
	A = SetA.c_str();
	B = SetB.c_str();

	for(int x=0;x<10;x++)
		cout<<U[x];

	cout<<endl;

	for(int x=0;x<10;x++)
		cout<<A[x];
	
	cout<<endl;

	for(int x=0;x<10;x++)
		cout<<B[x];
	//************************************************************************
	
	for(int x=0;x<10;x++)//for set a
	{
		if (A[x]==U[x])//for set a
			bit[x]=1;
		else
			bit[x]=0;
	}	
	
	//for(int j=0;j<count;j++)// for set a
		//bit[j]=1;
	//for(int j=count;j<10;j++)//for set a
		//bit[j]=0;
//*******************************************************************************
	for(int x=0;x<10;x++)//for set b
	{
		if (B[x]==U[x])
			bit2[x]=1;
		else
			bit2[x]=0;//for set b	
	}	
	
	
	//for(int j=0;j<count;j++)// for set b
		//bit2[j]=0;
	//for(int j=count;j<10;j++)//for set b
		//bit2[j]=1;

//*******************************************************************************
	//for(int t=0;t<n-1;t++)
		//cout<<number[t];
	cout<<endl;
	for(int u=0;u<10;u++)//for set a
		cout<<bit[u];
	cout<<endl;
	for(int u=0;u<10;u++)//for set b
		cout<<bit2[u];

	
	return 0;
}

No, getline doesn't work with STL string. You can use a char array however; e.g.

string x;
char buff[1024];

while( getline(buff, sizeof(buff)) ){
   x = buff;   // convert to string
...

I'm not sure exactly what you want to do, but you could just concantinate the strings

std::string temp;
while (getline(file, temp))
{
    Uni += temp;
    cout<<"Universal Set: {"<<Uni<<"}"<<endl;
}

No, getline doesn't work with STL string.

What! Who the hell told you such a silly thing. There are two versions of getline() -- one works with std::string and the other works with character arrays. And for character arrays your example is 100% wrong! What you posted is the syntax for std::string, not character arrays.

// getline using std::string
std::string textstring;
std::getline(file, textstring);

// getline with character array
char textstring[255];
file.getline(textstring, sizeof(textstring));

I'm totally wrong. I don't think it's the first time.

I was only aware of the char array version, as every time I've tried file.getline( /*string*/ str) it failed. I'm happy to learn that the string version is available.

The getline function for the istream class returns the line in a char[]

istream& getline (char* s, streamsize n );
istream& getline (char* s, streamsize n, char delim );

There is another getline for the string class which returns the array in a string.

istream& getline( istream& is, string& s, char delimiter = '\n' );

It took me a while to figure that out also :)

The getline function for the istream class returns the line in a char[]

istream& getline (char* s, streamsize n );
istream& getline (char* s, streamsize n, char delim );

There is another getline for the string class which returns the array in a string.

istream& getline( istream& is, string& s, char delimiter = '\n' );

It took me a while to figure that out also :)

Yes, and that was the second example in my previous post. There is another getline() that's not part of fstream -- it is declared in <string> header file.

Be a part of the DaniWeb community

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