Thread: Strange Character in Programs Output:

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    277

    Strange Character in Programs Output:

    Okay, I was trying to make a simple program to list all the files with a certain termination in a folder of my system (Slackware Linux 9) following the intructions of the book "Linux Application Development by Michael K. Johnson and Erik W. Troan" I got the following piece of code:

    Code:
    #include<stdlib.h>
    #include<sys/wait.h>
    
    int main(){
    	int result;
    	
    	result = system("exec ls *.c");
    	
    	if (!WIFEXITED(result)){
    		printf("Abnormal termination\n");}
    		else 	
    			printf("%d",result);
    	
    	exit(0);
    }
    Okay result is storing perfectly all the names of *.c files but I'm also recieving a zero in the final output here is a sample of the output:
    imanewbie@blah:~/Projects/Junk Code$ ./listsc
    array.c dinamicstack.c hashtable.c queue.c struct2.c test.c
    child.c doublepointer.c linkedlist.c random.c struct.c tripointer.c
    conta.c hamash.c listsc.c senha.c swapbytes.c
    0


    I would like to know if someone can explain me what is this 0 cause the book doesn't mention is and I don't even know what to search for in the internet. Thanks a lot.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,665
    You mean the 0 printed by this

    printf("%d",result);

    Or some other zero?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jun 2004
    Posts
    277
    Quote Originally Posted by Salem
    You mean the 0 printed by this

    printf("%d",result);

    Or some other zero?
    Yes the 0 from the printf("%d",result)

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,665
    Oh, you mean why is result 0

    Well that's the return result of the system() call.
    Which MAY in turn be the exit status of the program you tried to run.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Jun 2004
    Posts
    277
    Quote Originally Posted by Salem
    Oh, you mean why is result 0

    Well that's the return result of the system() call.
    Which MAY in turn be the exit status of the program you tried to run.
    Hum... I think you are right, system() returns 0 if succesfull, well there would be a way to me remove this 0, well not remove but hide it?

  6. #6
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    What would you like it to print?
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

  7. #7
    Registered User
    Join Date
    Jun 2004
    Posts
    277
    Quote Originally Posted by chrismiceli
    What would you like it to print?
    I would like to print all the .c files but without the 0, I'm using it to list mp3's in fact just using .c to make a more simple example.

  8. #8
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    If you are using ls via system() to print the files, then why at all have the printf() statement. You can error check the return value of system if you want, but you don't have to print it. Just get rid of the printf() statement and it won't print the zero. Read this article about using system() and some alternatives: http://faq.cprogramming.com/cgi-bin/...&id=1043284392
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. "sizeof" character strings - output issue
    By bunko in forum C Programming
    Replies: 3
    Last Post: 12-03-2008, 06:10 PM
  2. Catching another program's output?
    By kenpem in forum C Programming
    Replies: 9
    Last Post: 12-18-2007, 01:43 PM
  3. strange output
    By volk in forum C++ Programming
    Replies: 2
    Last Post: 05-25-2003, 11:09 AM
  4. UNICODE and GET_STATE
    By Registered in forum C++ Programming
    Replies: 1
    Last Post: 07-15-2002, 03:23 PM
  5. How do I output one character??
    By kia_1998 in forum C++ Programming
    Replies: 1
    Last Post: 10-29-2001, 10:46 PM