View previous topic :: View next topic |
Author |
Message |
anonazyet n00b

Joined: 16 Apr 2002 Posts: 38
|
Posted: Wed Aug 20, 2003 1:22 pm Post subject: printf driving me nuts |
|
|
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 |
|
 |
orb9 Tux's lil' helper


Joined: 02 Sep 2002 Posts: 82 Location: Germany
|
Posted: Wed Aug 20, 2003 1:44 pm Post subject: |
|
|
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
to flush the buffer. IMHO thats fflush in C.
_________________ "Without music, life would be a mistake - I would only believe in a god who knew how to dance." (Nietzsche) |
|
Back to top |
|
 |
pilla Bodhisattva


Joined: 07 Aug 2002 Posts: 7731 Location: Underworld
|
Posted: Wed Aug 20, 2003 5:16 pm Post subject: |
|
|
Or you can flush in C with
_________________ "I'm just very selective about the reality I choose to accept." -- Calvin |
|
Back to top |
|
 |
Qball Apprentice


Joined: 25 Nov 2002 Posts: 196
|
Posted: Wed Aug 20, 2003 6:23 pm Post subject: |
|
|
it often just works if you end the last printf string with a line break "\n" (then it gets flushed) |
|
Back to top |
|
 |
zhenlin Veteran

Joined: 09 Nov 2002 Posts: 1361
|
Posted: Thu Aug 21, 2003 2:09 am Post subject: |
|
|
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 |
|
 |
anonazyet n00b

Joined: 16 Apr 2002 Posts: 38
|
Posted: Thu Aug 21, 2003 2:45 am Post subject: |
|
|
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 |
|
 |
grant.mcdorman Apprentice


Joined: 29 Jan 2003 Posts: 295 Location: Toronto, ON, Canada
|
Posted: Thu Aug 21, 2003 4:34 pm Post subject: |
|
|
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 |
|
 |
|