Go Job Hunting at the LQ Job Marketplace
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


Search this Thread
Old 01-16-2013, 09:19 AM   #1
Registered: Oct 2005
Posts: 527

Rep: Reputation: 32
printf vs std::cout and std::ofstream

Hi, am having a bit of difficulties earlier on getting something printed out to screen or file. I don't have the code in front of me so please forgive me if I can't paste the whole piece here. Essentially it's like this.

This works.....
printf("ip address : %s\n", inet_ntop(AF_INET, &(((struct sockaddr_in *)sa)->sin_addr), s, maxlen));

But the following don't.....
std::cout << inet_ntop(AF_INET, &(((struct sockaddr_in *)sa)->sin_addr), s, maxlen);

std:: ofstream ofile("file.txt");
ofile << inet_ntop(AF_INET, &(((struct sockaddr_in *)sa)->sin_addr), s, maxlen);

Any idea why?
Old 01-16-2013, 11:42 AM   #2
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 651

Rep: Reputation: 269Reputation: 269Reputation: 269
What do you mean by "it doesn't work"? My guess is that you just forgot to flush the stream.
Old 01-16-2013, 11:49 AM   #3
Senior Member
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 4,632

Rep: Reputation: 1429Reputation: 1429Reputation: 1429Reputation: 1429Reputation: 1429Reputation: 1429Reputation: 1429Reputation: 1429Reputation: 1429Reputation: 1429
Originally Posted by millgates View Post
What do you mean by "it doesn't work"? My guess is that you just forgot to flush the stream.
std::cout << inet_ntop(AF_INET, &(((struct sockaddr_in *)sa)->sin_addr), s, maxlen) << std::endl;


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
Perl: suppress std output and std error say_hi_ravi Programming 1 11-01-2012 06:22 AM
C++ simple problem, doesn't recognize some std::cout itz2000 Programming 3 06-28-2007 08:18 PM
pblm with std::cout while compiling adilturbo Programming 7 06-12-2006 07:56 AM
`cout' undeclared in namespace `std' aw_wolfe Programming 1 04-30-2005 09:25 PM
std string class and printf The_Nerd Programming 3 06-28-2004 10:46 PM

All times are GMT -5. The time now is 07:57 AM.

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