LinuxQuestions.org
Visit Jeremy's Blog.
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-02-2007, 08:36 PM   #1
tntcoder
Member
 
Registered: Sep 2003
Distribution: Arch
Posts: 89

Rep: Reputation: 15
C++: cout problem


Hi,

I have a very strange problem with 'cout' failing to write to the terminal at a certain point in my program, my debugger suggests the statement is being executed, and if i replace it with printf() the texts gets written to the terminal fine.

heres my code:

Code:
  cout<<"Starting Work:\n";  // WORKS FINE
  
  if(!cf.ReadFileHeaders())
  {
  	cout<<"Error Occurred"<<endl; // DOESN'T WORK - DEFIANTLY EXECUTED
  }
ReadFileHeaders() Relevant Segment:
Code:
	do
	{
		if(!istr.eof())
		{
			istr.getline(buffer, 1024);	
			cout<<buffer<<endl;

			// Check line starts with =fxbegin*
			header_line = strstr(buffer, "=fxbegin");
		}

	} while(header_line == NULL && !istr.eof());


	// If no header -> return error
	if(header_line == NULL)
	{
		istr.close();
		return false; // DEFIANTLY EXECUTED (RETURN TO COUT PROBLEM)
	}

So anyone have any ideas whats stopping cout from working where in its place printf works fine? I even tried putting an std::flush in there but that didnt change anything

Thanks for any help,

Jack
 
Old 08-02-2007, 08:58 PM   #2
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
What do you mean by "std::flush"? Do you mean std::cout.flush()? If not, try calling that right after the output call. Formatted output in GNU is supposed to automatically call flush at the end of the line, but std::cout might not actually wrap stdout from C or it might be opened as a binary stream to provide more control. I've had problems where std::cout could not keep up with the output it was given and it didn't automatically flush after newline, so I had to flush after every output.
ta0kira
 
Old 08-02-2007, 09:04 PM   #3
tntcoder
Member
 
Registered: Sep 2003
Distribution: Arch
Posts: 89

Original Poster
Rep: Reputation: 15
Thanks, I have tried flushing as below but it made no difference sadly , any other ideas?

std::cout.flush();
cout<<"Error Occured"<<endl;
std::cout.flush();
 
Old 08-02-2007, 10:13 PM   #4
Lothar Schwab
LQ Newbie
 
Registered: Aug 2007
Location: Minnesota, USA
Distribution: Lubuntu
Posts: 19

Rep: Reputation: 2
Quote:
Originally Posted by tntcoder
Thanks, I have tried flushing as below but it made no difference sadly , any other ideas?

std::cout.flush();
cout<<"Error Occured"<<endl;
std::cout.flush();
What does ReadFileHeaders return? Does it return
something != 0 so that the if statement in your
code does not even try to do the streaming to cout?
 
  


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
C++ simple problem, doesn't recognize some std::cout itz2000 Programming 3 06-28-2007 08:18 PM
how does cout works under different compilers ricka343 Programming 5 06-11-2007 11:18 AM
c++ cout not working :( Longinus Programming 10 08-01-2004 07:32 PM
C++ boolean in cout statement AMMullan Programming 2 06-08-2004 12:44 PM
cout queues miguetoo Programming 4 03-17-2004 02:02 AM


All times are GMT -5. The time now is 10:41 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration