LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-16-2014, 03:11 PM   #1
brick2
LQ Newbie
 
Registered: Feb 2014
Distribution: Debian
Posts: 19

Rep: Reputation: Disabled
reading multiple lines into a string c++


Ok so I have a client server program that I wrote in c++. Took me a while! As I am not really an expert.
The problem that I encountered was following:
When I read from a file in to a string it only loads the first line into a string and that is what is will send.
I can t really figure out why doesn't it read all the lines.
If need be I can post the whole code but I figured it would be more efficient if I posted the part of the code where the problem is.
Code:
string readFile()
{
    ifstream read("LOG.TXT");
    string str;
    int index = 0;
    if (read.is_open())
    {
        while (!read.eof()) //Tried using read.good()
        {
            getline(read, str);
            cout << str << endl;
            return str;
        }
    }
    else
        cout << "\nEror file is closed\n";
}
Any help any advice is welcomed.
 
Old 05-16-2014, 03:40 PM   #2
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: Fedora
Posts: 4,140

Rep: Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263
The problem is that after reading one line you call return. That immediately ends your loop and exits the function.
 
Old 05-16-2014, 03:48 PM   #3
brick2
LQ Newbie
 
Registered: Feb 2014
Distribution: Debian
Posts: 19

Original Poster
Rep: Reputation: Disabled
How stupid can I get.
Thank you so much for your response.

Code:
string readFile()
{
	ifstream read("LOG.TXT");
	string str, str1;

	int index = 0;
	if (read.is_open())
	{
		getline(read, str);
		while (read)
		{
			str1 += str;
			getline(read, str);
		}
		//cout << str1 << endl;
		return str1;
	}
	else
		cout << "\nEror file is closed\n";
}
This is how I made the function now in case somebody else needs it.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell script to find a string and print x lines before and y lines after the string igorza Linux - Newbie 6 04-18-2013 04:31 PM
[SOLVED] bash script reading specified lines from multiple files and concatenating into one TullyGirl Linux - Newbie 6 03-22-2013 10:29 PM
[SOLVED] [Perl] Reading multiple lines from a file Ziddy Programming 3 10-30-2012 07:27 AM
[SOLVED] Need to remove dynamic string across multiple lines and files. vandigroup Linux - Server 2 05-14-2011 11:50 PM
search for a string spread in multiple lines finder255 Linux - Software 2 10-22-2008 03:16 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:28 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration