What's wrong with this simple program in APUE?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers What's wrong with this simple program in APUE?
# 1  
Old 12-22-2009
What's wrong with this simple program in APUE?

I start wetting my toes in Linux programming.
I tried the first program myls.c in Advanced Programming in the Unix Environment.

Code:
#include <sys/types.h>
#include <dirent.h>              
#include "apue.h"

int main(int argc, char *argv[]) 
{
    DIR *dp;                     
    struct dirent *dirp;       

    if (argc != 2)
        err_quit("a single argument (the directory name) is required");

    if ((dp = opendir(argv[1])) == NULL)  

        err_sys("can't open %s", argv[1]);

    while ((dirp = readdir(dp)) != NULL){
            printf("%s\n", dirp->d_name);
        }

    closedir(dp);
    exit(0);                    
}

The book says that this code displays the files under somewhat directory.

but my result is:
Code:
[abc@manager apue]$ ls -la apue.src/
total 12
drwxr-xr-x  3 abc box 4096 12-21 15:57 .
drwxr-xr-x  3 abc box 4096 12-21 21:00 ..
drwxr-xr-x 32 abc box 4096 12-21 15:58 apue.2e
[abc@manager apue]$ ./myls apue.src/
6299739
6299771
6299795
[liurui@manager apue]$

why my result is numbers rather than the name of files?

my platform is RHEL 5.0.
# 2  
Old 12-22-2009
There is nothing wrong with the code you posted. What caused the problem is:

correct:
Code:
printf("%s\n", dirp->d_name);

is really this in your code
Code:
printf("%d\n", dirp->d_name);

What you posted and what you compiled are not the same.... IMO.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Wrong test interpretation for simple function.

Hello everyone, I have written simple script below to check if ip is added to interface #!/usr/local/bin/bash IFCONFIG="/sbin/ifconfig" SERVICE="/usr/sbin/service" IP="79.137.X.X" GREP=$(${IFCONFIG} | grep ${IP}) ip_quantity_check () { echo ${GREP} | wc -l } if ];... (2 Replies)
Discussion started by: bryn1u
2 Replies

2. Shell Programming and Scripting

I have no idea what is wrong with my simple script.

I am doing a simple "recycle bin" script. It is to check and see if the directory .TRASH exists within the home directory. If not then it makes the directory then appends a date and time to file and then finally moves the file in. However, when I run this script, it is not making the directory as... (5 Replies)
Discussion started by: iamdeman
5 Replies

3. Programming

A simple C program query ...

Given the following code inside the function ext3_write_super(): (It's there in Linux kernel 2.6.27.59) static void ext3_write_super (struct super_block * sb) { if (mutex_trylock(&sb->s_lock) != 0) BUG(); sb->s_dirt = 0; } The conditional test at if... (2 Replies)
Discussion started by: Praveen_218
2 Replies

4. UNIX for Dummies Questions & Answers

Help with creating a simple program!!

i am new to shell scripting!! i am making this program in bourne shell, that asks the user to input "Hello (their name)" or "question (their name)", any other input, "ERROR" will be outputted. if they input "Hello (name)", i want to out saying Hello (name) but if they input "question (name)", i... (4 Replies)
Discussion started by: bshell_1214
4 Replies

5. Shell Programming and Scripting

Simple issue, what is wrong?

I had this working a few days ago but I since changed it. Heres the code x=1 while 1 2 3 4 5 6 1=$(ps -ef | grep process | awk '{ print $2}') if then echo "The database is accepting connections..." echo "Now I will check the next process" 2=$(ps -ef | grep process1 |... (10 Replies)
Discussion started by: jeffs42885
10 Replies

6. Programming

Xlib simple program.

I don't know if it is right to ask you this. Can someone help me write a simple Xlib program,with button on it,and all that button do is switch 2 messages. I have tried and tried,but never get past Hello World. Can someone help me please? ---------- Post updated at 10:17 PM ---------- Previous... (2 Replies)
Discussion started by: megane16v
2 Replies

7. UNIX for Dummies Questions & Answers

does APUE fit for linux?

i am reading APUE now,and the OS in the book seems different from RH9,which i installed in my computer. can you tell me if a linux environment can help me really understand the APUE?If not,which OS i should try,the BSD? THANKS (1 Reply)
Discussion started by: mayuhao
1 Replies

8. Shell Programming and Scripting

What's wrong with this simple statement?

I know that this is a ridiculously simple statement, but I am getting an error when I execute it, and I can't figure out what it is. Can anyone point me in the right direction? #!/bin/ksh integer dateMonth=0 integer intZero=0 if then dateMonth = 1 fi echo $dateMonth (7 Replies)
Discussion started by: mharley
7 Replies

9. UNIX for Dummies Questions & Answers

What is wrong with this simple script?

The script below is supposed to list file and folder names and list the type besides each - It has no practical value - I am just trying to learn KSH (so far sounds like a bad idea!) Expected output should look like this: folder 1 *** dir *** file1 * file * The code runs but produces... (9 Replies)
Discussion started by: GMMike
9 Replies

10. Programming

QUESTION...simple program?

I am new to the unix/linux environment. AND........ I need to create a mini shell..that displays prompt (i.e., READY:$), accepts a command from std-in, and prints the command and any parameters passed to it. HELP!!!! (8 Replies)
Discussion started by: jj1814
8 Replies
Login or Register to Ask a Question