Sponsored Content
Full Discussion: ARGV help in C
Top Forums Programming ARGV help in C Post 302419823 by fpmurphy on Sunday 9th of May 2010 11:22:46 AM
Old 05-09-2010
Here is a minimalist example:
Code:
#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>

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

   if (argc == 2) {
       if ((dir = opendir(argv[1]))) {
           while((ent = readdir(dir)) != NULL)
               puts(ent->d_name);
       } else
           fprintf(stderr, "Error opening directory %s\n", argv[1]);
   }

   return 0;
}

 

10 More Discussions You Might Find Interesting

1. Programming

argv

I have a program which I wish to modify. It used to be run from the command line, but now I wish to change this so it can be used as a function. The program has complex argument processing so I want to pass my paramters to as if it were being called by the OS as a program. I have tried to... (2 Replies)
Discussion started by: mbb
2 Replies

2. Programming

Using argv argc

I searched on the forums. No advises. I am using a previous source code. I changed the main function main(int argc, char **argv) in a function misc(int argc, char **argv). How do you use the argc and argv parameters? This is how I am calling the function : char param; strcat(param,"wgrib ");... (4 Replies)
Discussion started by: Akeson Chihiro
4 Replies

3. Shell Programming and Scripting

Perl: Getting $ARGV's to operate like while(<>)

I have a script that asks a bunch of questions using the following method for input: print "Name:"; while(<>){ chomp; $name=$_; } So for example, if the questions asked for name, age, & color (in that order)... I want to be able to easily convert $ARGV into the input expected by... (2 Replies)
Discussion started by: jjinno
2 Replies

4. Programming

help for argv argc

Hi C experts, I have the following code for adding command line option for a program int main (argc, argv) int argc; char *argv; { char *mem_type; //memory type char *name; //name of the memory int addr; //address bits int data; ... (5 Replies)
Discussion started by: return_user
5 Replies

5. Shell Programming and Scripting

if #argv = (this OR that) then...

this is in one of my scripts... if ($#argv == 0) then echo 'blah bla' exit 0 endif I want it to be something like this... if ($#argv == 0 OR $argv >=3) echo 'blah bla' exit 0 endif so when the arguments are none, or greater than three I want this "if then" to take over. how? I... (5 Replies)
Discussion started by: ajp7701
5 Replies

6. Shell Programming and Scripting

$#Argv in Csh

Hello all, Had a quick question: In a typical csh script should inputting via stdin (i.e. set i = $< ) increase the value of $#argv ? echo enter an value: set val= "$<" if($#argv == 0) then echo No args else echo The arg is $argv so if a value is inputted #argv... (1 Reply)
Discussion started by: new2C
1 Replies

7. Programming

help with C, argv

when i run my program, i have a parameter, that i want to set the value to another string i am using int main(int argc, char **argv) { char my_str=argv; printf("%s",my_str); return 0; } and i get Segmentation fault ran using ./my_prog /usr/share/dict/words hello1 ... (2 Replies)
Discussion started by: omega666
2 Replies

8. Programming

How do I copy or rewind *argv[]

I'm working on my own pow function and I need to make a copy of *argv but I think that I am having trouble with the size of *argv and the size of any array that I make. The code below isn't working for me. and I want to accept any number no matter the size with pow -f 2 2. I was working out... (16 Replies)
Discussion started by: Errigour
16 Replies

9. UNIX for Advanced & Expert Users

O argv, argv, wherefore art thou argv?

All of my machines (various open source derivatives on x86 and amd64) store argv above the stack (at a higher memory address). I am curious to learn if any systems store argv below the stack (at a lower memory address). I am particularly interested in proprietary Unices, such as Solaris, HP-UX,... (9 Replies)
Discussion started by: alister
9 Replies

10. UNIX for Dummies Questions & Answers

ARGV how to use it?

So i am trying to read in file readFile <GivenFile> modFile looking for a regular file under the directories in the GivenFile and print them out is my over all goal. basically I am looking for anything that looks like a directory in the given file and printing it out. Since I am trying to do... (2 Replies)
Discussion started by: squidGreen
2 Replies
explain_readdir(3)					     Library Functions Manual						explain_readdir(3)

NAME
explain_readdir - explain readdir(2) errors SYNOPSIS
#include <libexplain/readdir.h> const char *explain_readdir(DIR *dir); const char *explain_errno_readdir(int errnum, DIR *dir); void explain_message_readdir(char *message, int message_size, DIR *dir); void explain_message_errno_readdir(char *message, int message_size, int errnum, DIR *dir); DESCRIPTION
These functions may be used to obtain explanations for errors returned by the readdir(2) system call. explain_readdir const char *explain_readdir(DIR *dir); The explain_readdir function is used to obtain an explanation of an error returned by the readdir(2) system call. The least the message will contain is the value of strerror(errno), but usually it will do much better, and indicate the underlying cause in more detail. The errno global variable will be used to obtain the error value to be decoded. This function is intended to be used in a fashion similar to the following example: errno = 0; struct dirent *dep = readdir(dir); if (!dep && errno != 0) { fprintf(stderr, "%s ", explain_readdir(dir)); exit(EXIT_FAILURE); } dir The original dir, exactly as passed to the readdir(2) system call. Returns: The message explaining the error. This message buffer is shared by all libexplain functions which do not supply a buffer in their argument list. This will be overwritten by the next call to any libexplain function which shares this buffer, including other threads. Note: This function is not thread safe, because it shares a return buffer across all threads, and many other functions in this library. explain_errno_readdir const char *explain_errno_readdir(int errnum, DIR *dir); The explain_errno_readdir function is used to obtain an explanation of an error returned by the readdir(2) system call. The least the mes- sage will contain is the value of strerror(errnum), but usually it will do much better, and indicate the underlying cause in more detail. This function is intended to be used in a fashion similar to the following example: errno = 0; struct dirent *dep = readdir(dir); int err = errno; if (!dep && errno != 0) { fprintf(stderr, "%s ", explain_errno_readdir(err, dir)); exit(EXIT_FAILURE); } errnum The error value to be decoded, usually obtained from the errno global variable just before this function is called. This is neces- sary if you need to call any code between the system call to be explained and this function, because many libc functions will alter the value of errno. dir The original dir, exactly as passed to the readdir(2) system call. Returns: The message explaining the error. This message buffer is shared by all libexplain functions which do not supply a buffer in their argument list. This will be overwritten by the next call to any libexplain function which shares this buffer, including other threads. Note: This function is not thread safe, because it shares a return buffer across all threads, and many other functions in this library. explain_message_readdir void explain_message_readdir(char *message, int message_size, DIR *dir); The explain_message_readdir function may be used to obtain an explanation of an error returned by the readdir(2) system call. The least the message will contain is the value of strerror(errno), but usually it will do much better, and indicate the underlying cause in more detail. The errno global variable will be used to obtain the error value to be decoded. This function is intended to be used in a fashion similar to the following example: errno = 0; struct dirent *dep = readdir(dir); if (!dep && errno != 0) { char message[3000]; explain_message_readdir(message, sizeof(message), dir); fprintf(stderr, "%s ", message); exit(EXIT_FAILURE); } message The location in which to store the returned message. If a suitable message return buffer is supplied, this function is thread safe. message_size The size in bytes of the location in which to store the returned message. dir The original dir, exactly as passed to the readdir(2) system call. explain_message_errno_readdir void explain_message_errno_readdir(char *message, int message_size, int errnum, DIR *dir); The explain_message_errno_readdir function may be used to obtain an explanation of an error returned by the readdir(2) system call. The least the message will contain is the value of strerror(errnum), but usually it will do much better, and indicate the underlying cause in more detail. This function is intended to be used in a fashion similar to the following example: errno = 0; struct dirent *dep = readdir(dir); int err = errno; if (!dep && errno != 0) { char message[3000]; explain_message_errno_readdir(message, sizeof(message), err, dir); fprintf(stderr, "%s ", message); exit(EXIT_FAILURE); } message The location in which to store the returned message. If a suitable message return buffer is supplied, this function is thread safe. message_size The size in bytes of the location in which to store the returned message. errnum The error value to be decoded, usually obtained from the errno global variable just before this function is called. This is neces- sary if you need to call any code between the system call to be explained and this function, because many libc functions will alter the value of errno. dir The original dir, exactly as passed to the readdir(2) system call. SEE ALSO
readdir(2) read directory entry explain_readdir_or_die(3) read directory entry and report errors COPYRIGHT
libexplain version 0.52 Copyright (C) 2008 Peter Miller explain_readdir(3)
All times are GMT -4. The time now is 07:46 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy