Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
printf driving me nuts
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
anonazyet
n00b
n00b


Joined: 16 Apr 2002
Posts: 38

PostPosted: Wed Aug 20, 2003 1:22 pm    Post subject: printf driving me nuts Reply with quote

Hey!
Learnin C and Network programmin on my Gentoo box, GCC version gcc version 3.2.1 20021207 (Gentoo Linux 3.2.1-20021207) The program is below


Code:

#include<resolv.h>
#include<fcntl.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<stdio.h>
#include<netdb.h>
#include<errno.h>

int main(int argc,char ** argv){
   struct hostent *hptr;
   struct sockaddr_in servaddr;
   char name[200];
   extern int h_errno;

   if(argc!=2){
      printf("Usage: browser www.sitename.com\n");
      _exit(-1);
   }

   if( (hptr=gethostbyname2(argv[1],AF_INET)) ==NULL){
      herror("Name Resolving error");
      _exit(-2);
   }

   inet_ntop(AF_INET,hptr->h_addr_list[0],name,sizeof(name));
   printf("The IP of %s is %s\n",argv[1],name);
   printf("Can't print anything beyond here");
   _exit(0);
}


If I do a cc -o browser, followed by ./browser www.google.com,
It prints Google's IP, but any printf following that line is completely ignored, If it doesn't end with a "\n". puts, works, whassup?
thanx,
Abhi
Back to top
View user's profile Send private message
orb9
Tux's lil' helper
Tux's lil' helper


Joined: 02 Sep 2002
Posts: 82
Location: Germany

PostPosted: Wed Aug 20, 2003 1:44 pm    Post subject: Reply with quote

IMHO, printf output is buffered and buffers aren't flushed until a newline is issued. According to man puts:
Quote:
puts() writes the string s and a trailing newline to stdout.

In C++ you can do
Code:
cout << flush;

to flush the buffer. IMHO thats fflush in C.
Code:
man fflush

_________________
"Without music, life would be a mistake - I would only believe in a god who knew how to dance." (Nietzsche)
Back to top
View user's profile Send private message
pilla
Bodhisattva
Bodhisattva


Joined: 07 Aug 2002
Posts: 7731
Location: Underworld

PostPosted: Wed Aug 20, 2003 5:16 pm    Post subject: Reply with quote

Or you can flush in C with

Code:

fflush(stdout);

_________________
"I'm just very selective about the reality I choose to accept." -- Calvin
Back to top
View user's profile Send private message
Qball
Apprentice
Apprentice


Joined: 25 Nov 2002
Posts: 196

PostPosted: Wed Aug 20, 2003 6:23 pm    Post subject: Reply with quote

it often just works if you end the last printf string with a line break "\n" (then it gets flushed)
Back to top
View user's profile Send private message
zhenlin
Veteran
Veteran


Joined: 09 Nov 2002
Posts: 1361

PostPosted: Thu Aug 21, 2003 2:09 am    Post subject: Reply with quote

Perhaps you should 'return' from main as opposed to 'exit'ing.

Code:

$ cat /tmp/sockdemo.c
                                                                               
#include<resolv.h>
#include<fcntl.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<stdio.h>
#include<netdb.h>
#include<errno.h>
                                                                               
int main(int argc,char ** argv){
   struct hostent *hptr;
   struct sockaddr_in servaddr;
   char name[200];
   extern int h_errno;
                                                                               
   if(argc!=2){
      printf("Usage: browser www.sitename.com\n");
      _exit(-1);
   }
                                                                               
   if( (hptr=gethostbyname2(argv[1],AF_INET)) ==NULL){
      herror("Name Resolving error");
      _exit(-2);
   }
 
   inet_ntop(AF_INET,hptr->h_addr_list[0],name,sizeof(name));
   printf("The IP of %s is %s\n",argv[1],name);
   printf("Can't print anything beyond here");
   return 0;
}
$ /tmp/sockdemo www.google.com
The IP of www.google.com is 216.239.33.99
Can't print anything beyond here$ _
Back to top
View user's profile Send private message
anonazyet
n00b
n00b


Joined: 16 Apr 2002
Posts: 38

PostPosted: Thu Aug 21, 2003 2:45 am    Post subject: Reply with quote

Thanx alot guyz...
Apparently _exit() closes all open file descriptors and whether or not it flushes data is implementation specific. So I guess buffered printf wasn't getting printed as either the stdout file descriptor was closed or the buffer was discarded. The ANSI function exit() does flush the buffer. exit() worked!!
Back to top
View user's profile Send private message
grant.mcdorman
Apprentice
Apprentice


Joined: 29 Jan 2003
Posts: 295
Location: Toronto, ON, Canada

PostPosted: Thu Aug 21, 2003 4:34 pm    Post subject: Reply with quote

anonazyet wrote:
Thanx alot guyz...
Apparently _exit() closes all open file descriptors and whether or not it flushes data is implementation specific. So I guess buffered printf wasn't getting printed as either the stdout file descriptor was closed or the buffer was discarded. The ANSI function exit() does flush the buffer. exit() worked!!
_exit() terminates the program without calling cleanup functions. This typically includes flushing file buffers, and functions registered by atexit().

Functions with leading underscores (_) are usually internal functions that you should only need to call in unusual circumstances. They are also usually very unportable (especially between different flavours of Unix - or non Linux/Unix systems).

You should stick to ANSI standard functions for the most part, especially since you're a beginner.

(Rant: I often see application code that uses a leading underscore for 'special' identifiers. Technically, this is wrong: in the ANSI C standard, all identifiers with a single leading underscore are reserved for the compiler and system.)
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum