LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 01-16-2013, 09:19 AM   #1
grob115
Member
 
Registered: Oct 2005
Posts: 540

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
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 838

Rep: Reputation: 368Reputation: 368Reputation: 368Reputation: 368
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
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: Slackware
Posts: 6,570

Rep: Reputation: 2229Reputation: 2229Reputation: 2229Reputation: 2229Reputation: 2229Reputation: 2229Reputation: 2229Reputation: 2229Reputation: 2229Reputation: 2229Reputation: 2229
Quote:
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.
Code:
std::cout << inet_ntop(AF_INET, &(((struct sockaddr_in *)sa)->sin_addr), s, maxlen) << std::endl;
 
  


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
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 08:58 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