LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   printf vs std::cout and std::ofstream (http://www.linuxquestions.org/questions/programming-9/printf-vs-std-cout-and-std-ofstream-4175445867/)

grob115 01-16-2013 09:19 AM

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?

millgates 01-16-2013 11:42 AM

What do you mean by "it doesn't work"? My guess is that you just forgot to flush the stream.

dugan 01-16-2013 11:49 AM

Quote:

Originally Posted by millgates (Post 4871515)
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;


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