Sponsored Content
Top Forums Programming How to turn argv[1] into a string in C? Post 302496106 by fpmurphy on Saturday 12th of February 2011 08:33:50 AM
Old 02-12-2011
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
    char *str = (char *)NULL;
    int len = 0;

    if (argc != 2) return 1;

    len = strlen(argv[1]);
    printf("Length of argv[1] = %d\n", len);

    if ((str = malloc(len + 1)) != NULL) {
       bzero(str, len + 1);
       strncpy(str, argv[1], len);
       printf("argv[1] = %s\n", str);
    }

    free(str);

    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. 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

4. 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

5. 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

6. Programming

Building an argc/argv style structure from a string (char*)

Hello All, First post. I've been struggling with the following: Given a char* string, I need to construct an "int argc, char *argv" style structure. What I'm struggling with most is handling escaped-whitespace and quotes. e.g. the string: char *s = "hello world 'my name is simon'... (10 Replies)
Discussion started by: cbarwise
10 Replies

7. Programming

ARGV help in C

Hi, Can somehelp help how to list file in a dir? (5 Replies)
Discussion started by: Learnerabc
5 Replies

8. 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

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
ARGZ_ADD(3)						     Library Functions Manual						       ARGZ_ADD(3)

NAME
argz_add, argz_add_sep, argz_append, argz_count, argz_create, argz_create_sep, argz_delete, argz_extract, argz_insert, argz_next, argz_replace, argz_stringify - functions to handle an argz list SYNOPSIS
#include <argz.h> error_t argz_add(char **argz, size_t *argz_len, const char *str); error_t argz_add_sep(char **argz, size_t *argz_len, const char *str, int delim); error_t argz_append(char **argz, size_t *argz_len, const char *buf, size_t buf_len); size_t argz_count(const char *argz, size_t argz_len); error_t argz_create(char * const argv[], char **argz, size_t *argz_len); error_t argz_create_sep(const char *str, int sep, char **argz, size_t *argz_len); error_t argz_delete(char **argz, size_t *argz_len, char *entry); void argz_extract(char *argz, size_t argz_len, char **argv); error_t argz_insert (char **argz, size_t *argz_len, char *before, const char *entry); char * argz_next(char *argz, size_t argz_len, const char *entry); error_t argz_replace(char **argz, size_t *argz_len, const char *str, const char *with, unsigned int *replace_count); void argz_stringify(char *argz, size_t len, int sep); DESCRIPTION
These functions are glibc-specific. An argz vector is a pointer to a character buffer together with a length. The intended interpretation of the character buffer is array of strings, where the strings are separated by NUL bytes. If the length is nonzero, the last byte of the buffer must be a NUL. These functions are for handling argz vectors. The pair (NULL,0) is an argz vector, and, conversely, argz vectors of length 0 must have NULL pointer. Allocation of nonempty argz vectors is done using malloc(3), so that free(3) can be used to dispose of them again. argz_add() adds the string str at the end of the array *argz, and updates *argz and *argz_len. argz_add_sep() is similar, but splits the string str into substrings separated by the delimiter delim. For example, one might use this on a Unix search path with delimiter ':'. argz_append() appends the argz vector (buf,buf_len) after (*argz,*argz_len) and updates *argz and *argz_len. (Thus, *argz_len will be increased by buf_len.) argz_count() counts the number of strings, that is, the number of NUL bytes, in (argz,argz_len). argz_create() converts a Unix-style argument vector argv, terminated by (char *) 0, into an argz vector (*argz,*argz_len). argz_create_sep() converts the NUL-terminated string str into an argz vector (*argz,*argz_len) by breaking it up at every occurrence of the separator sep. argz_delete() removes the substring pointed to by entry from the argz vector (*argz,*argz_len) and updates *argz and *argz_len. argz_extract() is the opposite of argz_create(). It takes the argz vector (argz,argz_len) and fills the array starting at argv with point- ers to the substrings, and a final NULL, making a Unix-style argv vector. The array argv must have room for argz_count(argz,argz_len) + 1 pointers. argz_insert() is the opposite of argz_delete(). It inserts the argument entry at position before into the argz vector (*argz,*argz_len) and updates *argz and *argz_len. If before is NULL, then entry will inserted at the end. argz_next() is a function to step trough the argz vector. If entry is NULL, the first entry is returned. Otherwise, the entry following is returned. It returns NULL if there is no following entry. argz_replace() replaces each occurrence of str with with, reallocating argz as necessary. If replace_count is non-NULL, *replace_count will be incremented by the number of replacements. argz_stringify() is the opposite of argz_create_sep(). It transforms the argz vector into a normal string by replacing all NULs except the last by sep. RETURN VALUE
All argz functions that do memory allocation have a return type of error_t, and return 0 for success, and ENOMEM if an allocation error occurs. BUGS
Argz vectors without final NUL may lead to Segmentation Faults. NOTES
These functions are a GNU extension. Handle with care. SEE ALSO
envz(3) ARGZ_ADD(3)
All times are GMT -4. The time now is 07:53 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy