LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 08-05-2006, 07:31 PM   #1
RHLinuxGUY
Member
 
Registered: Oct 2003
Distribution: Ubuntu 7.04
Posts: 889
Blog Entries: 1

Rep: Reputation: 30
Strange behavior: string within scope return 0 if not within a while loop.


Here is a more detailed explanation of my problem: I have a function that reads characters from a file and inputs them into a string that is within the scope of the function. Though, after inputing the information into the string and exiting the while loop, I return the string to another string, but I get 0, or no characters from the string that should have been holding that return value of my file reading function. After using gdb, and putting cout statements here and there, I finaly got my function to return the proper string. I have to return from the function from within the loop that reads from file and inputs into the local string. That contradicts from what I have understood variables in a local function to do. I thought that if I had the variable within the function It would not be destroyed UNTIL I exit from that scope, unless I threw it into heap and/or had a pointer, or made the variable global. Here is the code that should have worked, followed by the one that does work:

Code:
string ReadFromFile()
{
	string fromfile;
	
	ifstream rfile ( "example.txt" );
	
	if ( rfile == 0 )
	{ cout << "Failed to open \"example.txt\"!  Exit failure!\n"; exit(-1); }
	
	if ( rfile.is_open() )
	{
		while (! rfile.eof() )
		{
			getline(rfile,fromfile);
		}
		rfile.close();
	}
	else { cout << "\"example.txt\" is not open! Exiting failure?!\n"; }
	
	return fromfile;
}
Code:
string ReadFromFile( )
{
	string fromfile;
	
	ifstream rfile ( "example.txt" );
	
	if ( rfile == 0 )
	{ cout << "Failed to open \"example.txt\"!  Exit failure!\n"; exit(-1); }
	
	if ( rfile.is_open() )
	{
		while (! rfile.eof() )
		{
			getline(rfile,fromfile);
			return fromfile;
		}
		rfile.close();
	}
	else { cout << "\"example.txt\" is not open! Exiting failure?!\n"; }
	
	return 0;
}
I compiled with the following if anyone is wondering:

Code:
g++ -g IOFile.cc -Wall -Wextra -o IOFile
Thank you in advance!
 
Old 08-05-2006, 10:30 PM   #2
vharishankar
Senior Member
 
Registered: Dec 2003
Distribution: Debian
Posts: 3,178
Blog Entries: 4

Rep: Reputation: 138Reputation: 138
It's quite simple. In the first instance, the while loop runs to the end of the file and the variable keeps getting reset to the next line and next line and so forth until it comes to the last line which is an empty line. So it returns an empty string.

In the second case you just read one line and return it. It's bad logic you've used but it works because the while loop executes just once.

The good solution to this is that if you want the full file returned in a string, it should append to the string in the while loop instead of overwriting it each time.
Code:
                string temp;
                while (! rfile.eof() )
		{
			getline(rfile,temp);
                        fromfile = fromfile + temp;
		}
Or something similar

Last edited by vharishankar; 08-05-2006 at 10:32 PM.
 
Old 08-05-2006, 11:05 PM   #3
RHLinuxGUY
Member
 
Registered: Oct 2003
Distribution: Ubuntu 7.04
Posts: 889

Original Poster
Blog Entries: 1

Rep: Reputation: 30
Thank you very much. Stupid mistakes like this happen often to me when using something new. Makes being a functional programmer further away then I would hope to be.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
string return >minimalist< Programming 3 01-08-2006 04:49 AM
odd pointer/string behavior nadroj Programming 19 11-25-2005 07:42 PM
can a function return a string? hubabuba Programming 13 03-06-2005 02:51 PM
String return question k1ll3r_x Programming 3 11-10-2004 02:46 PM
string as return value in c raven Programming 6 02-10-2002 06:09 AM

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

All times are GMT -5. The time now is 10:11 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration